How important are naming conventions for getters in Java?

前端 未结 6 1972
孤城傲影
孤城傲影 2021-02-08 10:12

I’m a huge believer in consistency, and hence conventions.

However, I’m currently developing a framework in Java where these conventions (specifically the get

相关标签:
6条回答
  • 2021-02-08 10:44

    Josh Bloch actually sides with you in this matter in Effective Java, where he advocates the get-less variant for things which aren't meant to be used as beans, for readability's sake. Of course, not everyone agrees with Bloch, but it shows there are cases for and against dumping the get. (I think it's easier to read, and so if YAGNI, ditch the get.)

    Concerning the size() method from the collections framework; it seems unlikely it's just a "bad" legacy name when you look at, say, the more recent Enum class which has name() and ordinal(). (Which probably can be explained by Bloch being one of Enum's two attributed authors. ☺)

    0 讨论(0)
  • 2021-02-08 10:50

    Consider this: Lots of frameworks can be told to reference a property in object's field such as "name". Under the hood the framework understands to first turn "name" into "setName", figure out from its singular parameter what is the return type and then form either "getName" or "isName".

    If you don't provide such well-documented, sensible accessor/mutator mechanism, your framework/library just won't work with the majority of other libraries/frameworks out there.

    0 讨论(0)
  • 2021-02-08 10:56

    I actually hate this convention. I would be very happen if it was replaced by a real java tool that would provide the accessor/modifier methods.

    But I do follow this convention in all my code. We don't program alone, and even if the whole team agrees on a special convention right now, you can be assured that future newcomers, or a future team that will maintain your project, will have a hard time at the beginning... I believe the inconvenience for get/set is not as big as the inconvenience from being non-standard.


    I would like to raise another concern : often, java software uses too many accessors and modifiers (get/set). We should apply much more the "Tell, don't ask" advice. For example, replace the getters on B by a "real" method:

        class A {
          B b;
          String c;
          void a() {
            String c = b.getC();
            String d = b.getD();
            // algorithm with b, c, d
          }
        }
    

    by

        class A {
          B b;
          String c;
          void a() {
            b.a(c); // Class B has the algorithm.
          }
        }
    

    Many good properties are obtained by this refactor:

    • B can be made immutable (excellent for thread-safe)
    • Subclasses of B can modify the computation, so B might not require another property for that purpose.
    • The implementation is simpler in B it would have been in A, because you don't have to use the getter and external access to the data, you are inside B and can take advantage of implementation details (checking for errors, special cases, using cached values...).
    • Being located in B to which it has more coupling (two properties instead of one for A), chances are that refactoring A will not impact the algorithm. For a B refactoring, it may be an opportunity to improve the algorithm. So maintenance is less.
    0 讨论(0)
  • 2021-02-08 10:59

    If you follow the appropriate naming conventions, then 3rd-party tools can easily integrate with and use your library. They will expect getX(), isX() etc. and try to find these through reflection.

    Although you say that these won't be exposed as JavaBeans currently, I would still follow the conventions. Who knows what you may want to do further down the line ? Or perhaps at a later stage you'll want to extract an interface to this object and create a proxy that can be accessed via other tools ?

    0 讨论(0)
  • 2021-02-08 10:59

    The violation of the get/set convention in the Java library classes is most certainly a mistake. I'd actually recommend that you follow the convention, to avoid the complexity of knowing why/when the convention isn't followed.

    0 讨论(0)
  • 2021-02-08 10:59

    The get-less schema is used in a language like scala (and other languages), with the Uniform Access Principle:

    Scala keeps field and method names in the same namespace, which means we can’t name the field count if a method is named count. Many languages, like Java, don’t have this restriction, because they keep field and method names in separate namespaces.

    Since Java is not meant to offer UAP for "properties", it is best to refer to those properties with the get/set conventions.

    UAP means:

    • Foo.bar and Foo.bar() are the same and refer to reading property, or to a read method for the property.
    • Foo.bar = 5 and Foo.bar(5) are the same and refer to setting the property, or to a write method for the property.

    In Java, you cannot achieve UAP because Foo.bar and Foo.bar() are in two different namespaces.
    That means to access the read method, you will have to call Foo.bar(), which is no different than calling any other method.
    So this get-set convention can help to differentiate that call from the others (not related to properties), since "All services (here "just reading/setting a value, or computing it") offered by a module cannot be available through a uniform notation".
    It is not mandatory, but is a way to recognize a service related to get/set or compute a property value, from the other services.
    If UAP were available in Java, that convention would not be needed at all.

    Note: the size() instead of getSize() is probably a legacy bad naming preserved for the sake of Java's mantra is 'Backwardly compatible: always'.

    0 讨论(0)
提交回复
热议问题