In Java, why do people prepend fields with `this`?

前端 未结 16 1998
广开言路
广开言路 2020-12-20 11:18

When referencing class variables, why do people prepend it with this? I\'m not talking about the case when this is used to disambiguate from method

相关标签:
16条回答
  • 2020-12-20 11:28

    Something else to keep in mind is the language itself. You didn't mention Java specifically (though I'm assuming you didn't really have anything else in mind, so this comment is more FYI), but as the previous posters have mentioned already it is an excellent way of making code self-documenting to prevent mix-ups down the road when someone else starts modifying your code base.

    If you take PHP, though, the use of $this is typically required when referencing class variables. With differing rules between languages, it is often easiest to stick with the pattern that is common between them, a pattern which just so happens to be a very solid coding style throughout. It's easier for me to simply prepend this to everything than try to remember what language requires it and what language simply "prefers" it.

    0 讨论(0)
  • 2020-12-20 11:31

    A slight aside, but it may be worth noting that the "Clean Up" tool in Eclipse can be set to automatically add/remove this. to member accesses according to preference.

    "Java / Code Style / Clean Up" in the preferences dialogue.

    0 讨论(0)
  • 2020-12-20 11:32

    Same reason why some people like to prepend private data members with "m_" or name interfaces "IFoo". They believe it increases readability and clarity. Whether or not you agree with conventions like these is a matter of taste.

    0 讨论(0)
  • 2020-12-20 11:32

    Apart from the disambiguation with method parameters, it buys you nothing. Maybe clarity/readability but that really depends on your style.

    0 讨论(0)
  • 2020-12-20 11:33

    I think there's nothing wrong if you put in this before the property name. This helps disambiguate the property from local variables should you have any declared with the same name (which may happen in a constructor when you initialize properties).

    It doesn't affect performance, it doesn't create any problems for readability (if at all it makes reading easier).

    So I don't think we need to worry about this. Let the programmers make their own choice.

    0 讨论(0)
  • 2020-12-20 11:33

    The only place I use this is in constructors in classes where some/most of the fields of the class can be supplied by the constructor. I use this rather than using short names for the method signature, it just seems more consistent than abbreviating.

    For example in a class "Rational"

    Rather than doing

    class Rational
    {
        int denominator;
        int numerator;
    
        public Rational(int d, int n)
        {
            denominator = d;
            numerator = n;
        }
    }
    

    I do this.

    class Rational
    {
        int denominator;
        int numerator;
    
        public Rational(int denominator, int numerator)
        {
            this.denominator = denominator;
            this.numerator = numerator;
        }
    }
    

    That way callers know more about what the constructors parameters are for.

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