yazz.com
yazz.com

Reputation: 58796

Java setters and "this"

I have noticed in alot of people do in Java setters:

1)

public void setX(int x) {
    this.x = x;
}

Personally I don't like this and I think it should be something like:

2)

public void setX(int newX) {
    x = newX;
}

Are there any reasons the first would be better?

Isn't 1) easier to make an error with. On a few occassions I have tracked bugs in code down to people doing:

x = x; 

by mistake, maybe because they were typing to fast and just wanted to get the getters and setters out of the way.

Upvotes: 5

Views: 435

Answers (8)

slim
slim

Reputation: 41263

It's entirely subjective, but the first form provides a nice clear method prototype in javadoc etc.

I don't feel there's much scope for error using the 'this.x = x' form. Certainly nothing that won't get caught by unit tests. You do have unit tests, right?

Upvotes: 3

BalusC
BalusC

Reputation: 1109112

It's just a coding convention. You're free to choose, but use it consitently. Also, an average IDE can autogenerate all getters/setters in a few keystrokes/clicks. In Eclipse for example, do Alt+Shift+R and then S and then select the properties you'd like to generate getters/setters for and click OK. Also, an average IDE will display a warning when you do x = x instead of this.x = x.

I myself prefer the this.x = x. Eclipse and many reverse-engineer tools (like Dali and Hibernate Tools) also generates it that way by default.

The x = newX convention (the Hungarian notation) is just inherited from the (old-fashioned) procedural programming world and simply doesn't fit nicely in the OO ideology.

Upvotes: 3

reccles
reccles

Reputation: 5224

If it's your own code or not part of an existing base pick the one you like and stick with it. If however you are working with existing code follow whatever convention is already being used.

Upvotes: 2

fastcodejava
fastcodejava

Reputation: 41117

I prefer this.x = x;. There is really no right way on this. In an IDE, it will look good for the extra color on this.

Upvotes: 2

Michael Easter
Michael Easter

Reputation: 24498

I assume that setters are auto-generated by the IDE. So my preference is the form that is used by default in the IDE. That is to say, I'm fairly neutral on it and feel that it does not merit changing the default IDE settings.

Upvotes: 1

Bill the Lizard
Bill the Lizard

Reputation: 405955

It's a matter of style. The argument in favor of

public void setX(int x) { this.x = x; }

is that you don't need to think of a new and meaningful name for the input argument, since you already have one.

Pick a style and use it consistently.

Upvotes: 14

Avi
Avi

Reputation: 20142

Reasons to prefer this.x = x:

  1. Using this.x = x in constructors and is a known pattern, which Java programmers recognize.
  2. The x in both the field and parameter represent the same value.
  3. With x = newX there is no syntactic indication that one is a field and the other a parameter.

Upvotes: 7

user8681
user8681

Reputation:

I don't think there's a "better" or "worse" here. It's just a matter of style.

Upvotes: 4

Related Questions