« Going retro | Main | A break from the Norm »

field prefixes

Code conventions

I agree with prefixed class fields, but not for the reasons given above. For example this is bad:

public void class Employee {
  private String name;
  public void setName(String n) {
    this.name = name;
  }
}
because the Javasoft conventions do not recommend using a different naming scheme for fields. Consequently, when you read the body of a method, you can never know if you are dealing with a local variable or a field. And this can be deadly.

But what about:

public void class Employee {
  private String name;
  public void setName(String n) {
    itsName = name;
  }
}

I'm failing to see how changing your field naming convention can help when you mistype what's on the rhs of a statement.

Fwiw, I use two prefixes - 'the' for statics and 'its' class fields.

The reasons I use prefixes are 1: ease of reading - I find 'its' less verbose that 'this.' (and naturally 'self.' is my pet hate with Python). And I find it harder to read code where the fields are not marked out in some way or where they are marked out, they are marked out with underscores (which is typographically unpleasant to my eye). 2: having prefix makes search replace operations easy, banal as that sounds.

Definitely not one to get riled up about, I just found the reasoning odd.

On the other hand, when Charles Miller talks about marking interfaces with I, I find myself in strong agreement. There just isn't a good reason to bother distinguishing interfaces from other types.


December 2, 2002 01:31 AM

Trackback Pings

TrackBack URL for this entry:
http://www.dehora.net/mt/mt-tb.cgi/824