Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "this" in Java vs Short Parameter Names

Which do you prefer and why?

public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
}

public void setPresenter(Presenter p) {
    presenter = p;
}
like image 703
Aram Kocharyan Avatar asked May 07 '11 06:05

Aram Kocharyan


1 Answers

I prefer the this-notation, at least in constructors, and compound setter methods, where you have multiple arguments.

  • You don't have to come up with two variable names for each field.
  • It is clear from the "outside", what the argument represents.
  • It is really a standard approach.

In the particular case of a setter, I don't really have an opinion, since the method name is explanatory enough, and the implementation is a single assignment.

like image 128
aioobe Avatar answered Nov 15 '22 13:11

aioobe