Readability and Naming Things
codesimplicity.com
codesimplicity.com
- Refactoring is often for eliminating temporaries, or for segregating them into their own call.
- A more functional style leads to fewer intermediate variables.
This isn't a rule, or a metric to be optimized to the exclusion of all else. Often the way to clean up code is to get rid of names that don't help you read, coalescing code blobs, until new names occur to you. Then you tease out code blobs again.
If your names are merely for binding (e.g: f(x) = .. x ..) and don't contribute much, then getting rid of them can be helpful. In Haskell, you can use "points-free" style to get rid of names like that. For example, you can replace:
f x = 1 + 2 * x
With: f = (1+) . (2*)But still, I don't feel like this approach would become useful unless/until you reached the point that it could condense a two-line formula into one line (ei, five or six applications further).
I strong suspect that in that case you'd have the kind of formula you only grasp at a glance after you'd working with it intensively for a while.
And this gets into the realm of "write only code" - code too compressed for anyone but it's original author to approach.
"There are only two hard problems in Computer Science: cache invalidation, off-by-one errors and naming things."
"I prefer minimum-length but maximum-information names, and then let the context fill in the rest. Globals, for instance, typically have little context when they are used, so their names need to be relatively evocative. Thus I say maxphysaddr (not MaximumPhysicalAddress) for a global variable, but np not NodePointer for a pointer locally defined and used. This is largely a matter of taste, but taste is relevant to clarity."
[1] or seperated by underscores or dashes in up- or lowercase wearing earmuffs
The temptation then becomes to re-use the name but I avoid that to make it easier to see which declaration belongs to which variable.
Declaring all the variables at the top of a function was a pretty hard to break habit.
Which is much more preferable than scattered around the function. Especially due to hoisting[1].
My functions typically go in the order of:
declare variables; declare any local functions if necessary (most of the time these eventually get refactored out to somewhere else, because they tend to be one-off utility functions that can be abstracted); do stuff; return value.
1. http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-...
if (cond) {
or not to if( cond ){
That is the question.I agree that spacing matters a lot. Another interesting idea is that long lines are less readable and that whatever is at the end of a line is less readable than what is at the beginning.
To
a = something_long +
something_else
or not to a = something_long
+ something_else
That is another question.Style matters, develop your own.
But maybe it really only matters because of my recent 'suck it and see' approach to SQL, where I've been exploring an unknown database, trying to find the queries that work...
IE:
int anInt = 7;
String aString = "My string value";
ClassName aClass = new ClassName();
In this example it's not the bad because the typedefs are few, trivial and short and there is actually a leader between the words (.). However, in real code where you have longer class names used in tandem with standard types like int or String as well has some times a dozen definitions--suddenly you wish you had a ruler you can put on the screen. int anInt = 7;
Service_Ups Svc = new Service_Ups();
ClassNamesCanGetLong variableNamesToo = new ClassNamesCanGetLong();Side note: there's a typo in sentence one, paragraph two: "changnig" => "changing"
But since the punch card went out of fashion a while ago and bits are nearly free (screen width otoh is not) it doesn't hurt to name all your variables descriptively.
A global variable named 'i' is likely going to get you i trouble, then again, jQuery does most of it's magic using a single global that doesn't even have a name...
The bits are free but there's still a mental cost to reading them.
I once rigorously used long-names for everything. But now, I'm working on an algorithm where the operations are what matter, short names actually help me grasp everything at once. As you say, these would be short local variable. Having i, j, k be the standard indexing variable can make some situations clearer.
It would actually be less readable if you suddenly started to use long names for loop variables and such.