- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Maybe in a fit of worthless programmer time someone wrote a tool to verify coding standards, and stuck it in every compile. It probably took $1000 to develop this pointless annoying nitpicking deviation from common sense as well, yet that programmer uses it as example code at job interviews.
So regardless of the obviousness, a bunch of warnings would pop up each time:
warning : variable 'T' violates coding standards variable name cannot differ by one character warning : variable 'T' violates coding standards no comment for variable warning : variable 'L' violates coding standards variable name cannot differ by one character warning : variable 'L' violates coding standards no comment for variable warning : variable 'W' violates coding standards variable name cannot differ by one character warning : variable 'W' violates coding standards no comment for variable warning : variable 'H' violates coding standards variable name cannot differ by one character warning : variable 'H' violates coding standards no comment for variable
Admin
Someone else has already pointed out that you can do away with some commenting if you spell the name out.
Admin
So you think you only need to comment the "how" aspect?
What happened to the "why?" aspect? Why are you doing that? Why that particular way? Why that order? Why is that tweak to that integer there? Oh, it's for compatibility with xyz!
Quite often in complex code it just makes sense to describe the "what" as well, because the system is so complex that the new hire would not be able to see at a glance what was going on. By complex I don't mean "iter += 1;", that is the code that can remain uncommented (if earlier there's a good why and what for the overall block of code).
And the "where"? Yes, comment those database query methods, comment what each index is for populating that prepared statement or querying the result set is. Some day someone will alter the prepared statement and things will go wrong elsewhere, and the comments will let you see quickly where it went wrong.
Admin
I would have just added the commment
Admin
I disagree. In fact, even in functional languages, I find myself doing this.
Instead of:
(formatted any way you like)
I would rather see this any day of the week:
Now imagine that with readable, meaningful names for t1-t5.
(Sometimes you could break up such deep nestings with auxiliary functions, but now you have a huge proliferation of those.)
Admin
Indeed, those are three of the only five reasons why I ever write comments.
My #1 commenting reason is the stupid policy decisions and insane technical practices. No amount of variable naming can express that a solvable problem remains in a shipping product because of an uninformed decision-making process. Not that I haven't tried--I've thrown exceptions of type FixmeHaveToStopLoadingFileNOWSeeBug953WeWillNotFixInThisRelease, for instance.
My second commenting reason is workarounds in my dependencies (e.g. // this library function crashes when passed 42, so handle that case differently).
My third reason is for code that is not written in the most straightforward possible form because the most straightforward possible form is too costly. This covers hand-optimized code, because after several passes through a compile-test-profile-edit cycle, even the clearest algorithms become impenetrable.
My fourth commenting reason is bug fixes, although I lump many of these in with reason #1. Generally if a bug is truly fixed there is no need to write a comment to that effect (the BTS and SCM systems both have this historical data, and fixed bugs are irrelevant to future code maintainers). If the the bug fix is incomplete due to a policy decision, then it's technically an unfixed bug. It is worthwhile to bring unfixed bugs to the attention of future code maintainers; however, not all policy decisions are stupid, so unfixed bugs due to non-stupid policy decisions get their own category.
My fifth reason for commenting is the boilerplate copyright statements at the beginning of each file. ;-)
For the purposes of this comment ;-) I'm counting other code documentation (theory of operation, maintenance hints, etc) separately from comments that appear interleaved with the code itself.
Admin
Lies, all lies. It doesn't forbid single-character variables, it forbids MORE THAN ONE single-character variable. The first one is fine. (Although the first one promptly forbids all two-letter variable names containing the utilized letter...)
Admin
Admin
But try searching a large body of text for t, l, x and y tt, ll, xx, yy is much easier to work with.
Admin
Reminds me of a Victor Borge comment:
What's the difference between a violin and a viola?
A viola burns longer.
Admin
[quote user="htg"][quote user="Vector"] Maybe in a fit of worthless programmer time someone wrote a tool to verify coding standards, and stuck it in every compile. It probably took $1000 to develop this pointless annoying nitpicking deviation from common sense as well, yet that programmer uses it as example code at job interviews.
So regardless of the obviousness, a bunch of warnings would pop up each time:
warning : variable 'T' violates coding standards variable name cannot differ by one character [/quote] Some 20 years ago I had a summer job at a company that did Ada consulting. Basically, companies that got big DoD contracts would fire my employer to tell them how to write Ada code.
Part of the services was a Style Guide, and, yes, there was a StyleChecker tool (that I worked on) that enforced the guidelines and produced lengthy reports on violations in the code. (The company that hired us would presumably use these reports to determine which programmers got perks like parking spots, chairs, and lunch breaks.)
One of the rules was that every control structure had to have a comment. That doesn't strike me as ridiculous (overkill but not a total WTF), and it should be easy to come up with an example that shows the value of following this practice. Yet the example included in the Style Guide totally missed the boat:
Then there were some style guidelines that struck me as bizarre. In variable declarations, the colons all had to line up:
Some consideration was given to requiring that the longest variable be listed first, but this never did get adopted.
My captcha is nobody else's business.
Admin
Which do you like better? Personally I use the second one.
int numberOfDaysSinceGeorgeRanTheReportLastTime; int days; //How many days has it been since running the report
Admin
I smack someone for writing a function several hundred lines long.
Admin
bind, mind, find, may, day ram, rem, rom, roi
who can confuse that? Figtht silly standards!
I would have named those variables ta, lb, wc, hd; // abcd - next charcter: e
Admin
Greyhound: That's easy. Just enable the "whole word only" option in your search dialog box.
Admin
Admin
Are we missing the point? These names do not say Top etc OF WHAT?
You'll have more than one 'thing' that has a Top: the window, the bitmap, the dynamically-inserted content, the Panic button etc etc etc.
May I also suggest that for each of those, you would document what the measurement is relative to (Window_Top is relative to and contained within Desktop) and maybe what the unit is: pixels, centimeters, NASA_Confusion_Units).
Admin
And why do you think Fortran chose those?
Using i-n to store natural numbers predates programming by at least 60 years. Probably more like 120 years, though I don't have access to any original, c. 1800 books to verify. They're used as indices for countable sequences in mathematics.
Admin
I like the fact that the coder couldn't be bothered to name the variables Top, Left, Width, Height, but then put that text in the comments.
The comments could have been left out if they had.
Admin
The whole key to this is scope. If these weren't public variables and the routine using them was 6 lines long - what the hell is the point? Variables should almost always be actual words but if the effect is localized to a small space it is extreme nitpicking to pick standards over common sense.
Admin
Admin
Width of WHAT? Height of WHAT?
The names Top, Bottom, Width, Height are not very descriptive. Probably some window or other, but who knows? And which window? Any window?
Admin
Oops. GreyWolf beat me to it.
Admin
One of my personal pet peeves are unfilled comments like those below - takes up 80% of a file and adds nothing:
Admin
The comment should have said:
Admin
I always code in Comic Sans, personally.
Admin
Try /\b[tlhw]\b/ or /<[tlhw]>/, depending on the flavour you're using. If your environment doesn't support any form of zero-width word-edge match, delete it and install something better.
(Of course, the real WTF is that you're using POSIX character classes...)
Admin
I once had a Russian write some code that used c for "sequence number"
c in the Cyrillic alphabet has the "s" sound.
That person also didn't indent code and used single character variable names, and got upset when told to change.
Fortunately for me, someone else indented the code. Visual Studio refused to do so for me.
Admin
Somebody in the 17th or 18th century mutated I into J. Latin has no J. "Jehova" in Christian texts starts with I.
Mathematician's notes have early J references. 3D unit vectors are (i, j, k). Complex numbers can be written with sqrt(-1) as +i or +j.
Admin
Admin
I knew that the "i" for integers was from an old programming language. Note that Fortan didn't have named data types until Fortran 66: http://en.wikipedia.org/wiki/Fortran
Admin
Admin
We have a very consistent naming convention at our company. We like to prefix all method level variables with "m" and all instance variables with "i". Then we use a form of hungarian notation where the name of the variable specifies the type such as "str" or "int". Then we just increment the variables with a number 1,2,3 and put comments next to the declaration.
This gives us very consistent variable names such as:
iStr1 //firstname iStr2 //lastname iStrBuilder1 //string builder to send email iBool1 //Is this user active? iBool2 //Does this customer have sex on a regular basis? iInt1 //what is the average wing velocity of a laden sparrow? iMyEnum1 // african or european? iInt2 //how many fingers am i holding up? mInt1 //local iterator for this for loop mLong //number of times this method has thrown an out of memory exception
... and so on...
Admin
I preferred using X and Y for my loops
Admin
No way. Do you keep the variable comments everytime you use any variables?
Admin
The real WTF is the stupid rule here. I am pretty sure the new guy was doing it out of protest.
Admin
Admin
Admin
I dunno, I always learned to use x, y, z for loops. Although my current employer has convinced me to move to i,j,k if only to fit in with existing code. I guess x, y, z could be a problem for apps which use a 3-d coordinate system.
Admin
Only for global, static, and member variables. Within a function, method, or block, there may easily be exactly one thing in local scope that has a top, left, bottom, or right.
For member variables, it can depend on the context. For example, if the four variables are members of a class named "Rectangle", it is reasonable to assume that they refer to the extents of the Rectangle stored at this. Any other Rectangle would need a prefix, like window.Left or bitmap_ptr->Left. If you really want to be clear, you could use this->Left, but at the risk of losing optimization due to pointer aliasing rules.
Ironically, just last month I had to audit two different software packages because they use Qt's QRect class, which has a very slightly different definition of "bottom" and "right" than the code that was using them expected. In QRect, width() == right() - left() + 1, which is different from the way many other rectangle implementations do it.
At one point we had banned the words "left", "right", "top", "bottom", "up", and "down". As far as I know they're still banned (unless you're talking about members of the QRect class).
The systems we deal with involve cameras, objects being photographed, and robots to move the objects and cameras around so different areas on the object's surface can be photographed. Our software moves things around taking pictures, then assembles the images back together. Cameras can be oriented in any of 4 90-degree rotations, and in some cases optional mirrors or lenses can be installed in the optical paths which flip one or both image axes. The robots all have their own axis orientations and units.
Part of the calibration of this system involves figuring out how "move motor #2 forward by 300 steps" translates into a change in the area on the object observed through the image coming from the camera. You can get exactly the same result if the object moves to the left, or the camera moves to the right. The polarity of the axis controller might be reversed (so right becomes left) or some compound lens might flip the image (so top becomes bottom), or both might happen at the same time (so top is still bottom, but left is left again) and the object ends up moving to the left again but it's upside-down.
Phrases like "moving to the left" have one and only one crystal clear meaning in our group's specialized technical jargon. The phrase, roughly translated, means "please give me a wedgie immediately, and make sure you pull hard enough to tie a bow with my underpants around the back of my ears this time."
Admin
T, L, W, H in the right context are obvious to a native English speaker. Top, Left, Width, Height can be resolved by anyone with an English to [Elbonian] dictionary.
I am a native English speaker, so it's not a problem for me. But, I still get bitten all the time with one and two-letter abbreviations for jargon. Worse, I work in a place where there are multiple sources (software + subject domains + company internals) all contributing acronyms to the same code.
Between touch typing and IDEs written in the past decade or more, what the heck are you saving? Would you like to buy a vowel?
Captcha: kungfu. An appropriate reaction to programmers who thing 12 of their keystrokes are worth 5 minutes of my time.
Admin
One could say that if there is actually more than exactly one thing in local scope that could have a top, left, bottom, or right, you need to refactor until those things belong to distinct objects.
This problem is endemic in crappy UI toolkits, where a whole pile of state of what should be different objects ends up in a single struct or class. Point-and-drool UI design tools make this worse, by exposing all of these as if they were properties of a single object.
Why have windowLeft, desktopLeft, buttonLeft, iconLeft, etc, when you could simply have window, desktop, button, and icon, each with their own Left? window.Left is clear enough--we're talking about the left of a window. Have more than one window? Rename the window, but keep the Left.
Admin
IMHO, one-letter variable names are reserved for loop vars.
Yes, all of them! In case I ever need to nestle 26 loops, why do you ask?
;-)
Admin
A bit of math history is due here. In the "olden" days, before computers were even a twinkle in Woz's eye, the letters i,j,k were used to represent integers, x,y,z were used to represent real numbers, and m,n were used for limits on sums. Think Algebra & Calculus.
Then, when computers came to be, mathematicians were among the first to spread into the field. Since symbolic logic fits well in both fields, the symbols i & j became hits.
Yes, I'm a mathematician who writes too much code & reads too many books.
Admin
How about this? The variables are descriptive, but the comment is now no longer redundant:
The best variable name I have ever seen has to be:
My friend in college used it in a substring replace function in qbasic.
Also I found that MS Visual Studio .net 2003 is incapable of using the Bitstream Vera Sans Mono font, which is a crying shame.
Admin
In math it's almost always "i" for complex numbers. Unfortunately when the damn EEs got involved they couldn't use "i" because it was already used for current (like that is so important). So they used the next best thing.
Admin
Should it be something like
foreach (Day day in workingDays) {...
instead? It's actually much more descriptive.
Admin
Admin
Addendum (2007-08-02 23:08): edit: Of course, nearly everyone beat me to it, but what the heck. Teaches me not to reply without reading all the comments, even when I'm that close to the end...
Admin
Comments are unnecessary. That's why I never post any.