One more word about Hungarian Notation and then I’ll let it drop, honest. (Maybe.)
If you’re going to uglify your code with Hungarian Notation, please, at least do it right. The whole point is to make the code easier to read and reason about, and that means ensuring that the invariants expressed by the Hungarian prefixes and suffixes are actually invariant.
Here’s some code I found once in the “diagram save” code of a Visio-like tool:
long lCountOfConnectors = srpConnectors->Count();
while( --lCountOfConnectors)
{
// [Omitted: get the next connector]
// [Omitted: save the connector to a stream]
}
OK, first of all, that should be cConnectors
, c
for “count of”. But that’s just a trivial question of what lexical convention we use. There’s a far more serious problem here. The number of connectors is not decreasing as we iterate the loop, so cConnectors
should not be decreasing.
Hungarian makes it easier to reason about code, but only when you make sure that the algorithm semantics and the Hungarian semantics match. Seriously, when I first read this code I naturally assumed that it was removing connectors from the collection for some reason, and therefore decreasing the count so that the count variable would continue to match reality. But in fact it was just using the count as an index, which is semantically wrong. The code should read something like:
long cConnectors = srpConnectors->Count();
for(long iConnector = 0 ; iConnector < cConnectors ; ++iConnector)
{
// [...]
}
In other words, the name of a variable should reflect its meaning throughout its lifetime, not merely its initialization.
Why not just name the original variable `iConnector`?
Because ideally we want the semantics of an index to be that its value is always a valid index; if you initialize it with the count, that invariant is violated. Remember, the whole point of this naming convention is that the semantics of the value are accurately captured by the name. It should always be legal to use iSomething as an index into a rgSomething.
I wish there was some good way to express this with Hungarian, especially since this both simplifies (although this is somewhat subjective) and optimizes the code…
Since the variable has been initialized with the count, if it’s used as an index, then you need to decrement it before you use it.
I have no idea why that comment ended up here. I was replying to another question.
“Why not just name the original variable `iConnector`?”
Because a count is not an index. The whole point in this article is that variable names should reflect the semantics of their values.
Also, shouldn’t the while loop’s condition use post-decrement?
Probably, yes. Don’t overthink it; that’s not the point of the article.
But someone was wrong on the internet! It just bugs me when there is wrong code that is not shown as such from a reputable source, especially for something so easy to mess up, such as this off-by-one error. I do agree that that wasn’t the point, but it was wrong!
FWIW, any chance you could edit my first comment (as there doesn’t seem to be any way for me to)?
Since the variable has been initialized with the count, if it’s used as an index, then you need to decrement it before you use it.
Pingback: Porting old posts, part 1 | Fabulous adventures in coding