0

I'm reading Martin Fowler's Refactoring book. In many of his refactoring examples, he is using variables that start with _varname. Why? Is this some old convention that is before my time? In the past year when I started learning Java, I have not seen anyone at work use this. Please advise as to where and why should this be used?

I'm already seeing differences in answers to this question... Why does martin fowler do this in this code for extract method refactoring?

FROM:

void printOwing(double amount) {
  printBanner();
  //print details
  System.out.println ("name:" + _name);
  System.out.println ("amount" + amount);
}

TO:

void printOwing(double amount) {
  printBanner();
  printDetails(amount);
}
Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • 2
    I think this question belongs to Programmers SE – Savv May 29 '14 at 11:47
  • Possible Duplicate - http://stackoverflow.com/questions/150192/using-underscores-in-java-variables-and-method-names – Ninad Pingale May 29 '14 at 11:49
  • There is a similar topic [In-java-why-do-some-developers-use-the-underscore-for-variable-names] http://stackoverflow.com/questions/150192/using-underscores-in-java-variables-and-method-names – Ashok_Pradhan May 29 '14 at 11:52
  • Yep, distinguishing local variables from class members. `amount` is a local variable so it doesn't have the underscore. `_name` is a class member. – Alvin Thompson May 29 '14 at 12:01
  • the suggested answer by someone claiming this is a duplicate is unrelated to my question. im not asking about underscores in general. I'm referring to specifically when the underscore is placed before the variable name.. – Horse Voice May 29 '14 at 12:04
  • BEFORE variable names. not in variable names is my question!!!!!! This is not a duplicate!! – Horse Voice May 29 '14 at 12:06

3 Answers3

1

The convention is often used for private fields. You don't have to use it, the most important is to be consistent, so if you are working on an existing code base, continue with that style.

Jesper Fyhr Knudsen
  • 7,802
  • 2
  • 35
  • 46
1

It is a convention to start the names of private fields of an object with an underscore in order to distinguish them from local variables in the code. This convention is not universal. Personally, I think that it is a bit superfluous when you have syntax highlighting that also shows the difference.

An alternative (although you could also use both) is to always reference members through this:

package org.foo.bar;

class Baz {
    private String quux;

    Baz (String quux) {
        this.quux = quux;
    }

    String getQuux () {
        return this.quux;
    }
}
Svante
  • 50,694
  • 11
  • 78
  • 122
  • so is it safe for me to think about underscore before a variable name as a member variable that could be referred to by the `this` keyword? Some people are claiming on this post that the convention is used to highlight private variables. – Horse Voice May 29 '14 at 12:07
  • Yep. I've seen it with `protected` and default scope as well as with private class members, though. The key point is it's for class members. – Alvin Thompson May 29 '14 at 12:09
  • Private variables **are** class members. – Alvin Thompson May 29 '14 at 12:15
0

Most likely, to distinguish local variables from class members.

EDIT: Now that you've added sample code, that is indeed the case.

Alvin Thompson
  • 5,388
  • 3
  • 26
  • 39