Tips for naming variables
makinggoodsoftware.com
makinggoodsoftware.com
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.
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.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. 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.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.
(I mean, I remember doing this thing in TI-83+ BASIC, where valid variable names were limited to the letters of the alphabet ...)
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.
you are absolutely right with your observation, that variable name its just a bad example, I have just edited the post and I have changed its name to daysDateRange.
Thanks for your feedback
Doesn't this violate his rule #5?
Thanks for the comment
Obviously written by a software weenie who lives far, far away from the actual hardware. ;-) (Digital logic often uses active-low signals for good electrical reasons. And some purely historic reasons: do memory chips actually need to power up in the activated state?)