News for hackers. Stuff that matters.
Re:Tips for naming variables by mojuba on Wednesday May 13, 2009 @08:08AM (#606759)

  void swap(int* x, int* y)
    { int t = *x; *x = *y; *y = t; }
Small blocks with very obvious code are perfectly Ok to have short variable names in. All other rules in this post can be criticized as easily.

Hence, rule #8: forget about the previous rules, just be a smart coder.

Re:Tips for naming variables by sethg on Wednesday May 13, 2009 @08:27AM (#606784)
Indeed, I think one very useful rule for naming variables is "the length of the name should be proportional to the scope". If you're iterating through a list of objects representing unfilled orders, it's OK to do this:

  for uo in unfilledOrdersIterator():
      oid = uo.id
      status = uo.status
      email = uo.customer.email
      sendEmail(email, "Your order %s is in status %s" % (oid, status))
But if there's a 200-line method that does all sorts of other things with a single unfilled order, then calling the order "uo" is probably a bad idea.
Re:Tips for naming variables by pp on Wednesday May 13, 2009 @09:16AM (#606831)
Shouldn't there NOT be 200-line methods in the first place?
Re:Tips for naming variables by nuclear_eclipse on Wednesday May 13, 2009 @10:15AM (#606908)
There are some cases where 200-line methods are perfectly acceptable, when breaking the logic into smaller functions only complicates the understanding and development of the algorithm. Not everything can be broken into multiple elegant 10-line functions without obfuscating the purpose or control flow.
Re:Tips for naming variables by sethg on Wednesday May 13, 2009 @10:52AM (#606970)
One thing I've occasionally done to tame large functions is this:

  def frobWidget(widget):
      def frobCog(cog):
          ## stuff involving both cog and widget
      ## stuff involving widget
      frobbedCogs = [frobCog(cog) for cog in widget]
      ## more stuff involving widget and frobbedCogs
If frobCog were broken out into a separate function, then it would have to take widget as an argument, and if frobCog is never actually called from anywhere other than within frobWidget, then such separation makes the code harder to understand. Keeping the definition internal lets me take advantage of lexical scope; I can refer to widget within the definition of frobCog.
Re:Tips for naming variables by mojuba on Wednesday May 13, 2009 @11:32AM (#607018)
In the example you showed I'd probably do the same, true, but I think there is no evil in passing widget as an argument.

In general, there's nothing wrong with functions that take everything they use as arguments. This is kind of functional programming (or a good part of it): every function is as independent as possible, which makes them easier to debug, easier to understand, and safer in terms of bugs. Functions that deal with data from outer scopes risk being less readable and less reliable.

Re:Tips for naming variables by mojuba on Wednesday May 13, 2009 @10:38AM (#606945)
Formally speaking, every line and every block (if, while...) can be moved to a separate function. Provided that functions are given fairly descriptive names, it is always possible to go as far as you wish in splitting your functions into smaller ones. I don't see any problem with that except reading/understanding, of course, which depends on your "target audience" so to say.

If you have a good sense of code aesthetics, you can make your code look like pseudo-code, which every decent coder is supposed to understand. So your quicksort may look almost like:

  function quicksort(array)
     var list less, greater
     if length(array) ≤ 1  
         return array  
     select and remove a pivot value pivot from array
     for each x in array
         if x ≤ pivot then append x to less
         else append x to greater
     return concatenate(quicksort(less), pivot, quicksort(greater))
or it may be a 50-liner with no sub-routines. It's a matter of choice, really.
Re:Tips for naming variables by bkudria on Wednesday May 13, 2009 @03:36PM (#607483)
Um, have you ever written code like this in a real program? Honestly? Why would you ever want to swap variables?

(I mean, I remember doing this thing in TI-83+ BASIC, where valid variable names were limited to the letters of the alphabet ...)

Re:Tips for naming variables by mojuba on Wednesday May 13, 2009 @04:42PM (#607596)
Check out some popular sorting algorithms, those that do the job "in place", i.e. without additional memory allocation.
Re:Tips for naming variables by bkudria on Thursday May 14, 2009 @03:51AM (#608399)
Yes... but how many times have you re-written a sort? (Where 'you' is your average programmer, or, even, the above-average programmer who reads technical blogs and toward whom this post is targeted)
Re:Tips for naming variables by mojuba on Thursday May 14, 2009 @10:05AM (#608747)
If you are an inventor of sorting algorithms, I'd guess you do that quite often.

But seriously, does it really matter? Short functions or blocks with variables of local significance do exist. Swap() was just the first thing that popped up, probably because I like messing around with algorithms and re-implementing them. Which every self-respecting programmer, umm, should do, I think.

Re:Tips for naming variables by wlievens on Friday May 15, 2009 @04:14AM (#610248)
Most projects contain at least a handful of utility functions that your base library doesn't offer. These utility functions are largely independent of the problem domain, and often of a mathematical nature. Therefore, short, undescriptive variable names (such as 'x') are perfectly acceptable.