Hacker Newsnew | comments | ask | jobs | submitlogin
edw519 1666 days ago | link | parent

Papa Bear:

  for(i=ii;i<iii;i++){
    for(j=jj;j<jjj;j++){
      for(k=kk;k<kkk;k++){
        doSomething();
      }
    }
  }
Mama Bear:

  for(YearCounter=FirstYearOfCycle;YearCounter<LastYearOfCycle;YearCounter++){
    for(MonthCounter=FirstMonthOfYear;MonthCounter<LastMonthOfYear;MonthCounter++){
      for(DayCounter=FirstDayOfMonth;DayCounter<LastDayOfMonth;DayCounter++){
        doSomething();
      }
    }
  }
Baby Bear:

  for(Year=FromYear;Year<ThruYear;Year++){
    for(Month=FromMonth;Month<ThruMonth;Month++){
      for(Day=FromDay;Day<ThruDay;Day++){
        doSomething();
      }
    }
  }


mkyc 1666 days ago | link

The moral of the story is not to use shorter names, but to write better code.

  Date start = ;
  Date end = ;
  Iterator i = new DayIterator(start, end);
  while (i.hasNext()) {
    doSomething(i.next());
  }
Your Papa Bear example iterates over a cube in a 3d array. Your Mama Bear example is some seriously broken date iteration code (so you change lastDay from within doSomething?). The Baby Bear is equally broken 'time period' iteration code.

Writing self documenting code isn't about giving everything complete and proper names. It's about choosing comprehensible and trackable names. You introduce Mr. Joseph Harrison as such, but you may thereafter call him Joe.

  BufferedInputStream bufferedInputStream = ... // bad, too formal
  BufferedInputStream bis = ... // bad, too casual
  BufferedInputStream stream = ... // ok
  BufferedInputStream input = ... // good
The Baby Bear code's naming is a bit off too. Is it inclusive or exclusive? I would not be bothered if I saw:

  for (y=firstY; y<=lastY; y++) {
The letter 'y' is obvious (yy/mm/dd), and its scope is small.

-----

edw519 1666 days ago | link

The moral of the story is not to use shorter names, but to write better code.

The scope of the post was variable naming. I used dates and "The Three Bears" for a light example of variable naming that almost anyone could understand. I did not even begin to address "better code". I'll save that for another (or hundreds of other) posts.

Your Mama Bear example is some seriously broken date iteration code (so you change lastDay from within doSomething?).

The code is fine. It only becomes "seriously broken" if you change the loop variable within the iteration, in which case you seriously broke it with poor practice.

The Baby Bear code's naming is a bit off too. Is it inclusive or exclusive?

The naming is fine. So is the code. Your question of inclusivity or exclusivity is meaningless without understanding the assignment of the iteration variables, which again, was outside the scope of the post.

The letter 'y' is obvious

No it's not obvious. That's the whole point. It's extremely poor practice (See Papa Bear example.) You cannot assume the poor sucker who maintains your code will know what "y" means. And Heaven help him if he tries to do a global search for that variable in 2,000 lines of code. Which may lead him to reuse it inside the iteration. Which is just about the only way to "seriously break" it as in your Mama Bear complaint.

Your revisions will cause exactly the problem I was demonstrating to avoid.

-----

mkyc 1665 days ago | link

My point is that naming verbosity depends on the code. This is vague but better than saying "cf. Goldilocks" ("choose anything between two crazy extremes"). My other point is that everyday use of names should be a guide for naming in code. The reason Mama Bear is wrong is that she's too formal. She gives a full explanation of the variable each time, but the 'full explanation' is the responsibility of the var setters, not of the name.

Your Momma Bear either misses the last days of several months, or counts extra days. It also fails to count the LastMonthOfYear. Your Baby Bear is nothing like your Momma Bear, though you seem to be refactoring. Momma counts entire months (look at the variable names, last day of month), Baby does something like "sum the totals from the first five days of Feb-May". These are naming bugs, they aren't irrelevant.

The naming depends on the code. If we're iterating a 3d array and the vars are set directly above, Papa Bear is readable and I wouldn't dick around with it (it might be a convention in the codebase).

  // baby bear
  for(y=firstY; y<=lastY; y++)
    for(m=firstM; m<=lastM; m++)
      for(d=firstD; d<=lastD; d++)
        sum += getDailyTotals(y,m,d); // line 4
I'm not writing tutorials here. If you don't understand y, m, d in lines 1-3 (especially having seen line 4), you probably haven't been working with dates enough to be messing with this code anyway.

I'm not saying you should choose single variable names, I'm saying they're sometimes ok, and that it's not as simple as "choose names with 4-8 characters".

-----

joe_the_user 1665 days ago | link

I'm shocked the above viewpoint has not gotten the majority of votes, it should.

-----




Lists | RSS | Bookmarklet | Guidelines | FAQ | DMCA | News News | Feature Requests | Bugs | Y Combinator | Apply | Library

Search: