0

I am currently designing a graphics library in Java and now it's come to making the frontend I am curious why I have never seen libraries using global variables for their settings/properties - in-fact some go out of their way to protect them such as JavaScript libraries, and of course choose to use getters and setters such as:

Viewport.VSyncEnabled(true)

instead of simply

Viewport.VSyncEnabled = true;

I assume that it is to let the library know what is happening, but there are so many variables that simply don't need monitoring such as output debug data.
I would really like to use global variables however if it very against the convention and there are benefits (that I don't know of right now) then I'll be happy to use methods. My reason for wanting to use global variables instead is by my benchmarks it is faster to access directly like that.

1 Answer 1

3

Yes, of course assigning a variable directly is faster than calling a method. However, there are good reasons why using a method is better.

  1. The Uniform Access Principle says that all data of an object (or here: library) should be accessed via the same interface. This would be violated if some properties could be set directly, and others could only be set via methods. At least some properties will use methods, because of …

  2. Validation. It may be that some config variable has the type int, but perhaps it only accepts values >= 0. With a public variable, there's no way to check that. With a method, we can perform arbitrary validation.

    Obviously, this does not apply to final fields. Making constants public is no problem, except possibly for the other two reasons here. It depends on how they are used.

  3. Encapsulation is important. If our public API consists of variables, we can never remove them without breaking backwards compatibility. This affects the inner workings of our library. When we use methods to set/get values, we can rework the internals of our library without having to change the public-facing API. For example, executing some hook when a property has been set is no problem with methods.

Together, these are three good reasons to use methods. It's just so much more flexible, and far cleaner and extensible than the alternative.

There is another issue with global variables, or more precisely with global anything: We can only have one “instance” of your library at a time. This can make testing or other advanced usage very difficult. Instead, offer your API via normal objects with the understanding that normally one would only use one instance. Using static anything is not object-oriented, it is just thinly veiled procedural programming.

Summary: public variable is very bad, public static method() isn't great either. private variable is good, as is public setVariable(…).

1
  • Realy constructive post!
    – RMachnik
    Feb 20, 2014 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.