I quite often see the following naming convention used in java code.
class SomeClass {
private final String name;
public SomeClass(final String name) {
this.name = name;
}
}
This seems a little odd to me. First off if you happen to misspell the variable in the method signature it will still compile...
class SomeClass {
private final String name;
public SomeClass(final String nane) {
this.name = name;
}
}
Compiles fine. Possibly flags nane as unused variable but the assignment (which just becomes a self assignment) silently compiles.
I find myself wanting to use 'm' for member variables as such...
class SomeClass {
private final String mName;
public SomeClass(final String name) {
mName = name;
}
}
It is shorter than the .this variant and catches the odd misspelling error shown previously.
However a colleague of mine gave me all kinds of flack when I brought this up as a convention on our new project stating that "in java we don't do that.".
Just curious as to why?
Personally I don't like using prefixes - it makes the code harder to read, IMO. I believe different people read in different ways - I end up reading "aloud in my head" and the prefixes interrupt this process. Obviously you can get used to it, but I'd rather not have to.
However, it would be a mistake to claim that no-one used prefixes like this. I've worked at various different companies using Java - some used prefixes, some didn't.
I'd also point out that most IDEs will give you a warning about a no-op assignment in your typo example. For example, in Eclipse I get:
The assignment to variable name has no effect
If you regularly ignore warnings, I'd say you have bigger problems :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With