views:

243

answers:

8

This is language agnostic, but I'm working with Java currently.

I have a class Odp that does stuff. It has two private helper methods, one of which determines the max value in an int[][], and the other returns the occurrences of a character in a String.

These aren't directly related to the task at hand, and seem like they could be reused in future projects. Where is the best place to put this code?

  1. Make it public -- bad, because Odp's functionality is not directly related, and these private methods are an implementation detail that don't need to be in the public interface.

  2. Move them to a different class -- but what would this class be called? MiscFunctionsWithNoOtherHome? There's no unifying theme to them.

  3. Leave it private and copy/paste into other classes if necessary -- BAD

What else could I do?

+1  A: 

A lot of people create a Utility class with a lot of such methods declared as static. Some people don't like this approach but I think it strikes a balance between design, code reuse, and practicality.

BobbyShaftoe
A: 

Option 2 is probably your best bet in Java, despite being unsatisfying. Java is unsatisfying, so no surprise there.

Another option might be to use the C Preprocessor as a part of your build process. You could put some private static functions into file with no class, and then include that file somewhere inside a class you want to use it in. This may have an effect on the size of your class files if you go overboard with it, of course.

kwatford
+1  A: 

If it were me, I'd either:

  1. create one or more Helper classes that contained the methods as static publics, naming them as precisely as possible, or
  2. if these methods are all going to be used by classes of basically the same type, I'd create an abstract base class that includes these as protected methods.

Most of the time I end up going with 1, although the helper methods I write are usually a little more specific than the ones you've mentioned, so it's easier to come up with a class name.

Kaleb Brasee
+3  A: 

Here's one solution:

Move the method that determines te max value in a two-dimensional int array to a public class called IntUtils and put the class to a util package.

Put the method that returns the occurrences of a character in a String to a puclic class called StringUtils and put the class to a util package.

There's nothing particularly bad about writing static helper classes in Java. But make sure that you don't reinvent the wheel; the methods that you just described might already be in some OS library, like Jakarta Commons.

Kaitsu
A: 

I not know what the other languages do but I have the voice of experience in Java on this: Just move to the end-brace of your class and write what you need ( or nested class if you prefer as that is accepted canonical convention in Java )

Move the file scope class ( default access class right there in the file ) to it's own compilation unit ( public class in it's own file ) when the compiler moans about it.

See other's comments about nested classes of same name if differing classes have the same functionality in nested class of same name. What will happen on larger code bases is the two will diverge over time and create maintainability issues that yield to Java's Name of class as type of class typing convention that forces you to resolve the issue somehow.

What else could I do?

Be careful not to yield to beginner impulses on this. Your 1-2 punch nails it, resist temptation.

Nicholas Jordan
message edit: per Bobby Shaftoe see Interfaces can do work as a web search. This simplifies the matter for most stressors.
Nicholas Jordan
A: 

In my experience, most large projects will have some files for "general" functions, which are usually all sorts of helper functions like this one which don't have any builtin language library.

In your case, I'd create a new folder (new package for Java) called "General", then create a file to group together functions (for Java, this will just be a class with lots of static members).

For example, in your case, I'd have something like: General/ArrayUtils.java, and in that I'd throw your function and any other function you need.

Don't worry that for now this is making a new class (and package) for only one function. Like you said in the question, this will be something you'll use for the next project, and the next. Over time, this "General" package will start to grow all sorts of really great helper classes, like MathUtils, StringUtils, etc. which you can easily copy to every project you work on.

Edan Maor
+2  A: 

Wait until you need it!

Your classes wil be better for it, as you have no idea for now.

When you are ready, in Eclipse "Extract Method".

Thorbjørn Ravn Andersen
A: 

You should avoid helper classes if you can, since it creates redundant dependencies. Instead, if the classes using the helper methods are of the same type (as kbrasee wrote), create an abstract superclass containing the methods.

If you do choose to make a separate class do consider making it package local, or at least the methods, since it may not make sense for smaller projects. If your helper methods are something you will use between projects, then a library-like approach is the nicest to code in, as mentioned by Edan Maor.

You could make a separate project called utils or something, where you add the classes needed, and attach them as a library to the project you are working on. Then you can easily make inter-project library updates/fixes by one modification. You could make a package for these tools, even though they may not be that unified (java.util anyone?).

Jesenko Mehmedbasic