Naming field fields with the name of the method parameter name

What is considered best practice when it comes to class field names and method parameters in the same class?

For example, if I have a field with a name transactionType, you should also specify a parameter in my setter method transactionTypeand just access the field with this.transactionType?

I am not asking for an opinion, I just want to know if this is acceptable, or it will be considered confusing since there are two uses of the same name (although they clearly differ by area).

+4
source share
3 answers

Java, , , setter, . IDE, , , .

this. ; . transactionType , this.transactionType .

+6

, this. . , , , (, IntelliJ), .

; Java-. , . . .

+2

Yes. It is good practice to use a meaningful parameter name rather than a short and obscure name.

It would be better if you use the same name as the instance field name to make it more clear.

Using this, you can benefit from Auto JavaDoc , which uses parameter names.

+1
source

Source: https://habr.com/ru/post/1540441/


All Articles