views:

123

answers:

6

Hi all. After years of programming, we all have a set of small functions used as helpers utilities that we wish it comes build-in so we can use it in any project and have ti taken care by more people (test and optimized).

I have quite a collection of these functions. I wonder how do you guys organize them? Do you have any tips?

This is how I do it. I put it in a separate project (an eclipse project) let say "MyUtils" and it referred to by other projects. This works but because the utils collection are getting bigger and bigger something it is kind of weird that the utils are bigger than the project code (for small projects). And to ship it in Jar, you have to select them all by hand (or include them all). Is there a better way?

Also, as Java requires all functions to be in a class so I have ton of static functions (those that does not fit in OOP) for example a function read text file from a file name. Like this:

 package nawaman.myutil;

public class UText {

    static public String ReadTextFile(String pFileName) {
        ...
    }
    static public String[] ReadLines_fromFile(String pFileName) {
        ...
    }
    static public String ReadLine_fromFile(String pFileName, int pLineNumber) {
        ...
    }
    ...
}

So when I need to include all the functions goes when though it is not used.

Is there a better way to do this?

I use eclipse on Linux anyway if there is special technique for it but fell free to share if you have techniques with other tools.

+1  A: 

This works but because the utils collection are getting bigger and bigger something it is kind of weird that the utils are bigger than the project code (for small projects). And to ship it in Jar, you have to select them all by hand (or include them all). Is there a better way?

For my projects I use javac to select all the classes from my util libraries. For this I compile all classes from my project to an empty output directory. javac automatically resolves the dependencies to the util libraries because I added the util library pathes as source pathes. Now I can create a jar that contains all classes of my project and only the needed classes of the util libraries.

Also, as Java requires all functions to be in a class so I have ton of static functions (those that does not fit in OOP) for example a function read text file from a file name.

I do it the same way. But I try have a lot of small util classes instead of a few big ones, so that I don't have to include tons of unneeded methods to my jars.

tangens
Instead of using this approach to "avoid unneeded methods in jars" (I'm not saying the method is bad, just not so meritorious for that particular goal) consider a tool that can analyze the jars/code and perform a full removal of "unneeded methods". See ProGuard <http://proguard.sourceforge.net/> as an example.
pst
What do you do if you find bug in your util class? You release a new version of whole your project?
cetnar
It depends on the bug of course. If it affects some important functions in my project, then yes, I'll release a new version.
tangens
A: 

I don't use Eclipse, but in Visual Studio you can add a reference to file without it being physically moved or copied. This allows you to define a file in the root of your source control that all of your projects can reference without it being included in every project or having to deal with the copying problem. With this kind of solution you can intelligently split your util methods into different files and selectively include them based on what individual projects need. Also you can get rid of the extra .jar.

That said, I have no idea if Eclipse supports this kind of file referencing, but it might be worthwhile to look.

popester
It does. However this simple approach doesn't really cover distribution or versioning by itself. I would consider using an automated tool to "remove extra methods" instead of trying to do it by hand. The structure should fit the problem; it should not be slave to worrying about optimization of the JARs.
pst
+1  A: 

My "utilities" have their own package namespace and SVN repository. They are, in essence my own libraries: distinct projects which may be pulled in, shared, tagged, updated, whatever.

The organization used within each of these "libraries" depends on the scope and function in question.

Because I disagree with the structure being a slave to some potential class/JAR output: If you are concerned about "method bloat" in the classes and/or JARs, please use an automated tool to combat this. ProGuards is just one example and, while it can obfuscate, it can work equally well at just "dead code elimination".

pst
+1  A: 

I treat such utility classes just like other components external to the software that I develop:

  • For each component I create a Eclipse project and build it to a jar.
  • Classes are grouped logically in packages, e.g. [domain].util.net, [domain].util.text etc.
  • In a project I include the dependencies I need. Maven can help you here.

You write that utility classes have a lot of static methods. That's something I don't use a lot. For example the text functions you show can be refactored to a class or set of classes that extend or implement classes and interfaces from the collections framework. That makes it easier to integrate my code with other libraries.

Kwebble
Thanks for responding. Just to be sure that I get it :D. So you separate one project for one utility package? I mean. Is [domain].util.net is in a package and [domain].util.text is in another?
NawaMan
Yes, mostly. But sometimes several packages are combined if their functions depend on each other.
Kwebble
+1  A: 

Split your utils module into smaller subprojects. Use Maven or other build system to track versions of all your util modules. They are crucial to your systems because I think they used are in almost all your projects. Use tools like Findbugs or PMD to mesure quality of your code.

Every project need to know which version of utils module is using. It unacceptable in my opinion to add to binaries/sources of one of yours 'nonutils' project some loosely coupled util classes.

Please, revise yours classes with other commons projects like Apache Commons. I assume that lot of your utility code is similiar. Think better of rewriting yours static metods, because they obstruct testing (I'm sure that Findbugs will be complaining a lot too).

To sum up - creating a utils library is a hard stuff and a lot of responsability. So requirements in area of code quality are very high. I hope that my advice will help.

cetnar
+1  A: 

You should be very careful with removing classes after compilation - you may end up in a class not found situation at runtime. If you never use reflection or Class.forName() you should be safe, but those introduce runtime dependencies which the compiler cannot help you with (like it can with "new").

Remember - those classes not used do not use memory in the running program, only uses bytes on disk.

Personally I've ended up at saying disk space is cheap, and the risk of accidntially removing a class defintion used causing a runtime break, is not worth it to me, so I say - all code used for compilation must be shipped.

Thorbjørn Ravn Andersen