1

I am using Java for creating an application for Android (so I can use many of Java 7 options, but cannot use all of them). I have two classes:

class A{
    static String getScript(String input){
        return "result of functionality_1 on input";
    }
    ...
}

class B{
    static String getScript(String input){
        return "result of functionality_1 on input";
    }
    ...
}

Both classes A, B have getScript() methods which have same functionality and purpose. I can move that method to another class to avoid need to change the code in two places when I need to change that functionality. I saw this question and know that it is not a bad practice to have a Class with only static methods. But creating a class with only one static method seems to be odd. I want to know is it really an odd? and also bad practice? Is there a way to avoid that?

3
  • 1
    /* Sadly, Java did not have a notion of a function up until version 8. */
    – 9000
    Apr 7, 2017 at 19:36
  • What version of Java are you using? Can you use version 8?
    – Darius X.
    Apr 8, 2017 at 3:18
  • @DariusX. Java 7 Apr 8, 2017 at 5:22

2 Answers 2

3

I'd say it's a better thing to not have duplicate functionality floating around in your code base. What happens if you need to update getScript in A and forget to do it in B?

A separate class (call it C) could probably contain this method, but since this function is somehow related to A and B, maybe pull it up to a (new) abstract class. Refactoring could look something like this:

public abstract class ScriptRetrieval
{

    static String getScript(String input)
    {
        return "result of functionality_1 on input";
    }
}
public class A
{
    // No more getScript in here
}
public class B
{
    // No more getScript in here
}

and now instead of this:

String s1 = A.getScript("test1");
String s2 = B.getScript("test1");
assert(s1.equals(s2));

You do this:

String s1 = ScriptRetrieval.getScript("test1");
String s2 = ScriptRetrieval.getScript("test1");
assert(s1.equals(s2));

The benefit is that in the first code example, you have two classes with (presumably) the same implementation of getScript. The issue is that if there is any change to A.getScript, the assertion will fail. In the second example, there is only one implementation of the function so the assertion should never fail.

6
  • @igner Can you explain more in about duplicate functionality floating? Do you mean the sample code in my question has duplicate functionality floating? Also A and B now have different parents. What about an interface? Apr 7, 2017 at 19:04
  • @hasanghaforian: I mean: you have the same function in two places, I understood you to mean it was even the same code. If you ever need to fix a bug in one instance of the function, you need to fix it in the other instance, which sounds easy but you'd be surprised how often developers forget to do that, or don't even know that the other function exists. Simplify it by having just one function. Apr 7, 2017 at 19:10
  • @frustrated For someone this inexperienced, it's better to provide a code example of the inheritance you're describing, rather than assuming he knows what you're talking about. Apr 7, 2017 at 19:15
  • Using inheritance in this way is discouraged. Static imports are preferred. You only get one shot at inheriting so it's not worth wasting on something that's easily done otherwise.
    – JimmyJames
    Apr 7, 2017 at 20:14
  • @JimmyJames: Good point. I figured that if there's one function in common between the two, there might be more later that could go into a parent class (as sometimes happens to me during refactoring). But that's too big of an assumption, so I changed up the example. Apr 7, 2017 at 20:26
1

Java 8 allows static methods in an interface, and if these two classes share some behavior it seems that they might implement at least one common interface.

Since you're using Java 7, I agree with the accepted answer: its fine to create a class with a single method in order to avoid duplication.

However, these questions are not context-free. Looking at mock code that says "result of functionality_1" can point to one generic option, but looking at the actual code may well lead to a different answer.

Some questions one might ask about the code: what is the type of code is in that function...how large...how complex...how likely to change.. and what are your alternatives. Sometimes there is a part that fits naturally into an existing Constants class and you can leave some duplication of code in place.

DRY is a key principle, for sure. So duplication is usually bad. Still, duplication does avoid the one level of indirection in calling something that is in another piece of code. Every time you create a class and a method, you are creating new nouns and verbs for someone who is going to understand the code later. So, every new class adds conceptual mass to the code. The trick is to minimize conceptual mas, while adhering to the motivation behind DRY.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.