Where to put potentially reusable helper functions?

This is an agnostic language, but I am currently working with Java.

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

They are not directly related to the task and it seems that they can be reused in future projects. Where is the best place to put this code?

  • Making it public is bad, because Odp functionality is not directly related, and these private methods are an implementation detail that should not be in an open interface.

  • Move them to another class - but what would you call this class? MiscFunctionsWithNoOtherHome? There is no unifying theme.

  • Leave it private and copy / paste into other classes, if necessary - BAD

What else could I do?

+6
java language-agnostic oop
source share
8 answers

Here is one solution:

Move the method defining the value of te max in the two-dimensional int array to the public IntUtils class and put the class in the util package.

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

There is nothing wrong with writing static helper classes in Java. But make sure you don't reinvent the wheel; The methods you described may already be in some OS library, such as Jakarta Commons .

+5
source share

Wait until you need it!

Your classes will be better for this, since you have no idea how your future future needs will be.

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


EDIT: I found that test-based development provides code that is easier to use because you think of the API in front.

+3
source share

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

+1
source share

If it were me, I would:

  • create one or more helper classes that contain methods as static publications, naming them as accurately as possible, or
  • If these methods would be used by classes of basically the same type, I would create an abstract base class that includes these protected methods.

In most cases, I end up with 1, although the helper methods that I write are usually a little more specific than the ones you mentioned, so it's easier to come up with a class name.

+1
source share

I don’t know what other languages ​​do, but I have a voice in the Java experience: just go to the end of your class and write what you need (or a nested class if you prefer, since this is accepted by the canonical Java convention)

Moving the file's scope class (the default access class is right there in the file) to it is its own compilation unit (an open class in its own file) when the compiler moans about it.

See other comments about nested classes with the same name if different classes have the same functionality in a nested class with the same name. What happens with larger codebases is that they will diverge over time and create maintainability issues that lead to the Java Name of the class as a type of class typing convention that forces you to somehow resolve the problem.

What else could I do?

Be careful not to give way to novice impulses on this. Your 1-2 blows pierces him, resists temptation.

0
source share

In my experience, most large projects will have some files for "common" functions, which are usually all kinds of helper functions like this one that don't have a built-in language library.

In your case, I would create a new folder (a new package for Java) called "General", and then create a file to group functions (for Java, it will be just a class with a lot of static elements).

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

Do not worry that at the moment this makes the new class (and package) only for one function. As you said in the question, this will be what you will use for the next project, and the next. Over time, this “General” package will begin to grow with all sorts of really cool helper classes, such as MathUtils, StringUtils, etc., which you can easily copy for every project you are working on.

0
source share

Helper classes should be avoided if possible, since it creates redundant dependencies. Instead, if classes using helper methods are of the same type (as kbrasee wrote), create an abstract superclass containing the methods.

If you decide to make a separate class, consider making it local or, at least, methods, as this may not make sense for small projects. If your helper methods are what you will use between projects, then the most usable code is similar to a library, as mentioned by Edan Maor.

You can create a separate project called utils or something else where you add the necessary classes and attach them as a library to the project you are working on. Then you can easily make updates / corrections of the inter-project library with a single modification. You can make a package for these tools, even if they may not be so unified (anyone java.util?).

0
source share

Option 2 is probably the best choice in Java, even though it is unsatisfied. Java is unsatisfactory, so there are no surprises.

Another option might be to use the C preprocessor as part of the build process. You can put some private static functions in a file without a class, and then include this file somewhere inside the class in which you want to use it. This can affect the size of your class files if you go overboard with it, of course,

-2
source share

All Articles