views:

337

answers:

5

I do a lot of data analysis scripting, comparing two objects or strings to determine differences or come up with gaps that need to be filled.
When you're in a loop, comparing object a to object b, do you have a preferred coding standard that makes sense (i.e., it is self-documenting) and can travel nicely to other code? Or are variable names irrelevant in this type of code:

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update a  
    }  else {
        TreeMapvar.add(b);
}

A different example:

TreeMap<String, MyObject> TreeMapvar = new TreeMap<String, Object>();
File f = "Somefile";
// Open the file with bufferedReader br  
while ((line=br.readLine)!=null)) {  
    //insert code parse line to get object a  
    if (TreeMapvar.containsKey(a)) {  
        if (TreeMapvar.get(a).somefield.equals(a.somefied)) {  
            //insert code to update a  
        } else {  
            //insert code to insert new object  
        }  
     }  
}
+2  A: 

I think variable names are not at all irrelevant, but also that there's no good generic name that would fit all possible cases in such a loop. It depends on what is stored in the TreeMap and what does the data source contain. Nevertheless, we could say something like

for (String localData : TreeMapvar) {  
    // Read remoteData from data source  
    if (localData.equals(remoteData)) {  
        // Update or add to a counter  
    }  
}

if I got your drift.

The thing is that the comparison is made evident by the use of .equals() so we shouldn't need to use variable names (in general) to say that the values are going to be compared, what's not evident (and thus more important) is what values are being compared.

EDIT: Your second example changes nothing. You are still using lousy varnames a and b where you should be using descriptive ones showing what a and b actually represent. I still see the comparison clear, so that's not relevant to naming.

EDIT (re, your comment): That's easy. The only needed thing is some context, let's say the Map contains usernames associated to whatever you like and you are comparing them against existing user information.

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update or add to a counter  
    }  
}

for (String userName : TreeMapvar) {  
    String existingUserName = getNextUserName();
    if (userName.equals(existingUserName)) {  
        // Update or add to a counter  
    }  
}
Vinko Vrsalovic
I worded my question poorly for the results I was hoping to get. It should have been something like, "What examples do you have of changing variables names that helped readability?"
Scott Hoffman
A: 

If the language in question implements operator overloading, then I would implements the == operator. If it doesn't, then you can either use equals as above or implement "Compare" function

Your lanuage or framework probably already implements both of these for strings. You have to keep in mind that the "Equals" function in most languages actully compares references (shallow) verses instancs (deep).

The compare function looks like this:

int Compare(string a, string b)
{
  if(a < b) return -1;
  else if (a == b) return 0;
  else return 1;
}
Charles Graham
A: 

In short, context (the name of the variable speaks for what it holds) & readability is what I think matters.


foreach(Order order in Orders)
{
    if (order.ID == queriedOrderID)....
}

shahkalpesh
+1  A: 

Well, if you need 2 variable names, let's call them a and b, then you probably have 3 objects involved: self, a, b.

Now, that's quite a bunch for me, I try to keep things smaller.

So, consider your first example:

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update a  
    }  else {
        TreeMapvar.add(b);
}

So, why not have your whole snippet be part of the b class definition and rename b to "this"?

lookAtYourTreeMap() {
    for( String a : TreeMapvar) {
      this.accomodateTreemapIfNecessaryTo(a);
    }
}

And then I would have a method

accomodateTreemapIfNecessaryTo(a) {
  if(a .equals( this)) {
     a.update();
  } else {
    TreeMapVar.add(a);
  }
}

Notice that the inventor of the modern computer, Kent Beck, says that, methods need to be short.

Start the whole thing in your third component like this:

B b = loadBFromSource();
b.lookAtYourTreemap();

Loops like yours I would call "structural code".

nes1983
I've never heard of Kent Beck being described as the inventor of the modern computer before. As far as I know his claim to fame is formalising test-driven development and Xtreme programming.
RickL
Yeah--inventor of modern _programming_, maybe, but definitely not the computer.
Michael Myers
YEEEHAW! MY ANSWER WAS ACCEPTED!!!1
nes1983
A: 

Variable names should enhance the code readability, in your example (without trying to modify the code):

  1. a could be named something like baseValue
  2. b could be named something like compareToValue or testValue

so if (a.equals(b)) {
becomes if (baseValue.equals(compareToValue)) {

meade