0

Basically, I have a boolean which is used by like 20% of all my classes on a large API project. Everything, from utility methods to larger classes, uses it. I can set the state (which will not change) on the program start, but don't know the "best" way to access it.

Initially, I went for dependency injection. Quickly, method parameter count grew as almost everything needed it. It was a pain watching the single boolean in almost every parameter in certain classes. I decided to try out a static method. I came up with the following class structure:

public final class MyClass {
    private static boolean neededBoolean;

    public MyClass(boolean bool) {
        MyClass.setBoolean(bool);
    }

    private static void setGems(boolean bool) {
        MyClass.neededBoolean = bool;
    }

    public static boolean getNeededBoolean() {
        return neededBoolean;
    }
}

This felt strange though, most static methods usually don't read internal variables, and any program could easily (purposefully or not) change the variable for everything. I get I could also use a singleton, but I've been told those are best for loggers and other uses, not reading static variables. I also don't want to make the boolean public so any other classes could edit it.

So what's the best choice here? Overusing dependency injection, a singleton, or my current system

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
Big_Bad_E
  • 947
  • 1
  • 12
  • 23
  • Avoid global state at all costs. You almost certainly have bigger design problems. – Tom Hawtin - tackline Mar 24 '20 at 23:40
  • @TomHawtin-tackline, I read your profile blurb. _Tom spent a decade dealing with fallout from global state...". lol, that sounds brutal. You should write that your story down. I'm sure people would love to hear about that! – jmrah Mar 25 '20 at 23:30

1 Answers1

1

If you're using a dependency framework, you should absolutely use it.

E.g. if you're using the Spring Framework, you could inject it like this, so the value is initialized from e.g. the application.properties file:

@Component
class MyComponent {
    @Value("${needed.boolean}")
    private boolean neededBoolean;

    // code here that can use the value
}

You might consider using a property configuration object, though, so the property name isn't replicated all over your code. More typo-safe that way.


If you don't have a dependency framework and are ok with calling a static method (tight coupling), and the value is only initialized on startup, I'd suggest a class like this:

public final class AppConfig {
    private static Boolean neededBoolean;

    public void static initialize(boolean neededBoolean) {
        if (neededBoolean != null)
            throw new IllegalStateException("Already initialized");
        AppConfig.neededBoolean = neededBoolean;
    }

    public static boolean getNeededBoolean() {
        if (neededBoolean == null)
            throw new IllegalStateException("Not initialized");
        return neededBoolean;
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I do not use a dependency framework sadly, but an init method is definitely what I needed. I'm fine with the coupling as it should never be updated. – Big_Bad_E Mar 24 '20 at 23:17
  • Then it sounds like this `AppConfig ` class is what you need, with built-in safeguard to prevent accidentally using the value before the program startup sequence has initialized it. – Andreas Mar 24 '20 at 23:20