What is the good programming practices you think in Java?
The reson I ask this question:
I always wonder if I am doing in the wrong way so I want to know more about the good practices.
One thing I am always confused with:
Should I always use this.xxxto refer the instance variable in order to distinguish them from local variables?
There are several ways to look at ‘good coding practices’
In the first case, programmers adopt certain styles to guard themselves against introducing ‘accidental’ errors that can sneak into their code:
for Example in C, the following code is valid (but has a subtle bug)
so, in order to force the compiler to catch this error, you could adopt a style such as
Such styles/idioms are regarded as ‘good’ practices and there a quite a few that can be found for Java in books like ‘Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin‘
Regarding the second case, writing ‘this.xxx‘ enables the reader of the code to differentiate between a local/instance variable, so in the spirit of code readability, it is marginally better than directly referencing the variable as ‘xxx’.