views:

3033

answers:

15

Even nowadays I often see underscores in Java variables and methods, an example are member variables (like "m_count" or "_count"). As far as I remember, to use underscores in these cases is called bad style by Sun.

The only place they should be used is in constants (like in "public final static int IS_OKAY = 1;"), because constants should be all upper case and not camel case. Here, the underscore should make the code more readable.

Do you think using underscores in Java is bad style? If so (or not), why?

+35  A: 

If you have no code using it now, I'd suggest continuing that. If your codebase uses it, continue that.

The biggest thing about coding style is consistency. If you have nothing to be consistent with, then the language vendor's recommendations are likely a good place to start.

Tanktalus
We do use coding conventions without using underscores. Anyway, looking at frameworks and older code, I often saw underscores. The question that consistency rules over convention is clearly to be answerd for consistency but not the point I thought of while asking the question.
Georgi
A: 

using 'm_' or '_' in the front of a variable makes it easier to spot member variables in methods throughout an object.

As a side benefit typing 'm_' or '_' will make intellsense pop them up first ;)

Nicholas Mancuso
If you're programming Java, most likely you will have an IDE that will colour your member variables in a different colour. "m_" is just nasty.
JeeBee
I prefer "its" as it reads well: `if (itsEmail.equals(email))`
davetron5000
I prefer this. for member names. Absolutely unmistakable.
S.Lott
A: 

I think it's bad style. I only use it for private variables (e.g. _count)

Although just use a style you like the most and stick with it!

daddz
If you really think it's bad style, why do you use it then?
Georgi
+13  A: 

Rules:

  1. Do what the code you are editing does
  2. If #1 doesn't apply, use camelCase, no underscores
davetron5000
A: 

It's a blend of coding styles. One school of thought is to preface private members with an underscore to distinguish them.

setBar( int bar)
{
   _bar = bar;
}

instead of

setBar( int bar)
{
   this.bar = bar;
}

Others will use underscores to indicate a temp local variable that will go out of scope at the end of the method call. (I find this pretty useless - a good method shouldn't be that long, and the declaration is RIGHT THERE! so I know it goes out of scope) Edit: God forbid a programmer from this school and a programmer from the memberData school collaborate! It would be hell.

Sometimes, generated code will preface variables with _ or __. The idea being that no human would ever do this, so it's safe.

chris
In your case I use the following:setBar( int aBar) { bar = aBar;}Readable, without this. or _bar ...
Georgi
That's fair enough, but then aBar shows up in the method signature in the API, and I think it looks messy.
chris
+2  A: 

I think any style that breaks a language's own style guidelines (without due reason) is ugly and therefore "bad".

No doubt the code you've seen was written by someone who used to work on a language where underscores were acceptable.

Some people just cannot adapt to new coding styles...

Joe R
+2  A: 

"Bad style" is very subjective. If a certain conventions works for you and your team, I think that will qualify a bad/good style.

To answer your question: I use a leading underscore to mark private variables. I find it clear and I can scan through code fast and find out what's going on.

(I almost never use "this" though, except to prevent a name clash.)

Christophe Herreman
Like you said, style is very subjective. I tend to use `this` quite liberally to indicate a member variable if I think that it needs attention drawn to it. However, I'm not a zealot about it.
chris
A: 

The reason people do it (in my experience) is to differentiate between member variables and function parameters. In Java you can have a class like this:

public class TestClass {
  int var1;

  public void func1(int var1) {
     System.out.println("Which one is it?: " + var1);
  }
}

If you made the member variable _var1 or m_var1, you wouldn't have the ambiguity in the function.

So it's a style, and I wouldn't call it bad.

In this scenario I usually rename the parameter as "aVar1". By contrast with "the var1".
Lluis Martinez
+8  A: 
sunDoesNotRecommendUnderscoresBecauseJavaVariableAndFunctionNamesTendToBeLongEnoughAsItIs();

as_others_have_said_consistency_is_the_important_thing_here_so_chose_whatever_you_think_is_more_readable();
Anders Sandvig
The question that consistency rules over convention is clearly to be answered for consistency but not the point I thought of while asking the question. Anyway, there are times you should leave old traces, eh?
Georgi
If a "conflicting" naming convention is already in use, I think it depends on how much code we are talking about. I wouldn't recommend rewriting thousands of lines of code just to go from old_convention to newConvention, given that the old convention is used consistently.
Anders Sandvig
A: 

Personally, I think a language shouldn't make rules about coding style. It is a matter of preferences, usage, convenience, concept about readability.
Now, a project must set coding rules, for consistency across listings. You might not agree with these rules, but you should stick to them if you want to contribute (or work in a team).

At least, IDEs like Eclispe are agnostic, allowing to set rules like variable prefixes or suffixes, various styles of brace placement and space management, etc. So you can use it to reformat code along your guidelines.

Note: I am among those keeping their old habits from C/C++, coding Java with m_ prefixes for member variables (and s_ for static ones), prefixing booleans with an initial b, using an initial uppercase letter for function names and aligning braces... The horror for Java fundamentalists! ;-)
Funnily, that's the conventions used where I work... probably because the main initial developer comes from MFC world! :-D

PhiLho
+1  A: 

Here's a link to Sun's recommendations for Java. Not that you have to use these or even that their library code follows all of them, but it's a good start if you're going from scratch. Tool like Eclipse have built in formatters and cleanup tools that can help you conform to these conventions (or others that you define).

For me, '_' are too hard to type :)

Matt
+1  A: 

It's nice to have something to distinguish private vs. public variables, but I don't like '_' in general coding. If I can help it in new code, I avoid their use.

Fry
+1  A: 
  • I happen to like leading underscores for (private) instance variables, it seems easier to read and distinguish.Of course this thing can get you into trouble with edge cases (e.g. public instance variables (not common, I know) - either way you name them you're arguably breaking your naming convention:
  • private int _my_int; public int myInt;? _my_int? )

-as much as I like the _style of this and think it's readable I find it's arguably more trouble than it's worth, as it's uncommon and it's likely not to match anything else in the codebase you're using.

-automated code generation (e.g. eclipse's generate getters, setters) aren't likely to understand this so you'll have to fix it by hand or muck with eclipse enough to get it to recognize.

Ultimately, you're going against the rest of the (java) world's prefs and are likely to have some annoyances from that. And as previous posters have mentioned, consistency in the codebase trumps all of the above issues.

Steve B.
A: 

I don't think using _ or m_ to indicate member variables is bad in Java or any other language. It my opinion it improves readability of your code because it allows you to look at a snippet and quickly identify out all of the member variables from locals.

You can also achieve this by forcing users to prepend instance variables with "this" but I find this slighly draconian. In many ways it violates DRY because it's an instance variable, why qualify it twice.

My own personal style is to use m_ instead of _. The reason being that there are also global and static variables. The advantage to m_/_ is it distinguishes a variables scope. So you can't reuse _ for global or static and instead I choose g_ and s_ respectively.

JaredPar
This question was about asking about Java underscores in general, not about asking them only at member variables (though this was an example in the question).
Georgi
So you mark me down for commenting on a subset of the question? Seems a bit extreme
JaredPar
A: 

it's just your own style,nothing a bad style code,and nothing a good style code,just difference our code with the others.