- 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
In general, if your coding standards are sensible, it'll be pretty rare when you run into a situation where you really can't write code that performs just as efficiently without breaking them.
Admin
The wrong image was used with this article. The correct image would be something from the episode of Star Trek TNG entitled "The Ensigns of Command" where Picard gets beaten repeatedly by a several thousand page treaty with the Sheliak. The ONLY episode in all of Star Trek where the Captain dusts off the ship's plaque.
Riker: "You enjoyed that." Picard: "You're damned right I did."
Admin
I think that's the point that was attempted to be made with this article: coding standards aren't always sensible.
Admin
The flaw to your reasoning here is that you're assuming that the coding guidelines are well-thought out and consistent, and that enforcement is based on the spirit of the rules rather than mindlessly following the rules to the letter.
In real life, none of those assumptions are likely to be valid. Very likely you can find some obscure language feature that they forgot to ban, and if there is no rule against it, then, by definition, you can use it. "But that would be stupid" is not a thought that occurs to the legalist. The only question is, "Does it violate the rules?"
Admin
That's actually tricky because the semantics aren't actually identical in the presence of continue.
In a for loop, using continue will cause a jump to the post-block operation (e.g. the i++) then continue the loop back from the conditional. If a while loop, it will just jump back to the conditional. Much of the time the generated code will be identical, but non-trivial loops, non-trivial for loops in particular, can generate different code. The conversion is not always a mechanical process.
Admin
Once when I wrote coding standards for a development team, I included a clause that said, If you believe one of these rules to be counter-productive in a particular program, you may break the rule provided that you include a comment explaining why you found it necessary to break the rule. If writing the comment is more trouble than obeying the rule, then obey the rule.
Admin
I think many stupid rules come about because someone says, "It is usually better to do X than to do Y. Therefore, we will make a rule requiring people to always do X and never do Y."
Stated that way, perhaps the flaw in the reasoning is obvious: Just because something is USUALLY better does not mean that it is ALWAYS better. But people rarely state it that way.
Stupid rules are enforced because someone is put in charge of enforcing the rules whose only incentive is to make sure that the rules are enforced, not that the organization's overall goals are reached.
It is usually pointless to say to the policeman, "Yes, I went through the red light. But presumably the point of traffic lights is to prevent collisions, and I could see that there were no other cars for at least a mile in any direction, so there was no point sitting here waiting for the light to change. Indeed, if I waited another car might have come along and the danger of an accident would have been greater." The policeman's response is likely to be, "That may be so, but the law says you cannot go when the light is red. Here is your ticket."
Admin
Yeah beans. Heard of them? They make you fart.
Admin
Admin
I'll agree with the second reason, and if it were given as a cause to require braces I likely wouldn't have any argument. Sadly that's the first time I've ever seen it presented (at work or in books). It's always a purported "less chance of mistakes".
The first reason I think has less weight, but probably depends on the person somewhat. I think
is quicker to grok than (our coding standard):
but that this is a good middle ground:
PS: Wow, you'd think this site would handle code blocks better.
inside of the
Admin
Admin
Allowing people to make clueless modifications without code reviews is the best way to get a horrible codebase.
Admin
BTW: some submissions are only a couple of lines and we really have to fill in the details from our own similar experiences. In this case, the submission was fairly detailed, and all I really added was the whole resistance-is-futile prelude. Also, sometimes folks expressly ask us to distort the submission, presumably to protect themselves; but we ALWAYS keep the central WTF in-tact!
Admin
I really hate the braces on their own line thing. The important point of the block is not that it's in a block, but that it is tied to a condition. I want my ending braces to match up to the reason for the indentation in the first place, and avoid the starting brace that has to be there anyway. Brace lines are just noise.
I do agree with the brace everything approach though. It's just less hassle to not worry about it ever. When someone checks in a small change, but formats the entire file, it's annoying to check diffs (especially since you can't avoid it by ignoring whitespace in the diff).
Admin
http://thedailywtf.com/Comments/Coding-Practices-MUST-Be-Followed.aspx?pg=2#415698
Admin
That being said, there definitely is a fine art in anonymization, and I try to not let the details that are changed be the gospel truth of the story.
I do hate when people here leave comments about a very precise detail that was most likely changed to protect the innocent, such as assuming a person's heritage based on their obviously fake name.
Admin
Admin
“Comments in response to another comment must quote the latter comment. Please see 'User-Submitted Content Practices' document, 'Comments' on page 23, part 2, paragraph 2.”
Admin
Interesting to note... that had they not had those coding practices in there, the best this developer would have given us was a break statement. Ergo, all the rules resulted in forcing the developer towards the best solution!
RESISTANCE IS FUTILE!
Admin
If you're going to "break", "continue", or "return" from inside a loop, and if I'm the one with the rulebook, the rule would have to be two lines of comments, and the loops itself can't be longer than 50 lines so it all fits on the screen most people's editor so you can see at a glace where the jump is. Otherwise, yeah, stick with the top-to-bottom flow.
Admin
Although I happily break from for loops I have been caught out doing it. Say I wanted to look through a number of files until I find something. Here's some rough C code:
for (i = 0; i < number_paths; ++i) { FILE *fp = fopen(filepaths[i], "r");
}
When I first did something like this it actually works. It returns the correct result and does so moderately better that going through the entire list of file paths.
But as the program kept running it would start to act odd. It took me a long time to spot this error because the application's failure wasn't actually in this part of the code.
Obviously such a small code snippet makes the issue really obvious, but when that for loop was about the size of the screen it was easy for me to forget that I was breaking from a loop without first closing a file.
I'm not against breaks in for loops but I can see why it might leave a sour taste in people's mouths.
Admin
TRWTF is that coding practices didn't include code walkthroughs. EVERYONE's code needs extra eyes.
Admin
Probably Ada.
The For loop is indexed off of the array or subtype that you're working with and cannot modify the index. (In Ada 2012 there's For loops on collections, but the basic idea remains the same.) In the While loop the condition may be freely modified in the body of the loop itself.
So there's a fundamental difference there. (Though, granted, it may use the same sort of code generation on both loops -- Ada compilers have a reputation for being quite clever.)
Admin
Have you seen how most people write unit tests?
Admin
Methods on page 22, part 4, paragraph 1
A method should have a single exit point.
You're clearly not familiar with the guidelines ;)
Admin
As Carlo Cipolla once said: you cannot defend against morons, because morons are very witty. Well, he was talking about idiots, but the reasoning still works. There are so many ways of being stupid that you cannot cover all possible cases.
captcha: dolor = pain, how appropriate!
Admin
This police story happend to a friend of mine. To avoid a collition from someone behind he drowe through red. He did not got a fine.
Admin
This generally results in commends dotted over the place in our system with stuff like 'OH GOD, I FEEL DIRTY FOR THIS HACK, SORT OUT WHEN WE HAVE TIME' only to come back to it a year or so later, and it hasn't been touched, lol
Admin
I'm going to add my weight to the opinion that using break to exit a for loop is in general bad practice. Even if you can program the damn thing right (a pretty big if a lot of the time) then ensuring that the maintenance moron following you actually understands what you're doing requires copious documentation.
There was a programming course I went on which gave us the following rules:
a) Do you know exactly how many times you need to iterate? (This includes where the indices are variables which are not amended in the body of the loop.) Then use a For loop.
b) Is the condition on which you're looping something which is indeterminate when starting the loop? (This includes the case where the condition for exit may be calculated during the execution of the loop.) Then use a While loop.
If you are breaking out of the for loop using a break then you should be using a while, and rather than use break, set a flag and test it when re-entering the loop.
But what if I want to break out of the loop half way through the execution of that loop? Then you may do well to re-analyse your process, but in such a circumstance I would nest the section after where you want to break out inside an if statement on that same flag signalling end of processing.
And if the code inside the loop is so long and complicated that you can't see the top and bottom of it on the same page, then break out what's inside it into a separate method / procedure / subroutine / function / whatever.
I suspect that the guidelines in question arose from certain misapplication of break statements, and insistence on using a while loop on a specifically-incremented variable at the bottom, such as:
... for which the developer needs to be given the opportunity to take up a refresher course.
Neither of these things should detract from the true solution (as explained in the article) that you really should not be looping through a list to find an matching element, you should indeed be using a lookup table.
Admin
I totally disagree. The middle examples are the easiest to read, and your middle ground ones are the worst.
Admin
Another practice is to add @TODO into the code at the point where you now you need to return to it to do it properly, and set up your IDE to search for such tags and alert you to them. Every so often you leave yourself time to do "maintenance".
You mean you don't have the freedom to organise your own working practices so as to allow a certain amount of time during the week (e.g half a day) for routine exercises like housekeeping and maintenance? There's TRWTF, right there.
Admin
Coding standards
How very 1990s. these days we hire people who kow what the fuck they are doing (or train them) and don't need coding standards. OUr coding standards doc is buried in some repository somewhere and hasn't been looked at forprobably over 5 years, in that time I have hired over a dozen developers and coding varience is tiny. The biggest problem is framework choice and whatever the latest way the workd is supposed to run according to the Java/M$ bods ie. which techs to adopt silverlight/html5/ajax/xaml/wpf/hibernate/odata etc etc etc.
Admin
Admin
Admin
our client have a practice where we are not allowing to be using LINQ queries. this is because our client side architect is not yet learned LINQ. any case c# suck if you cannot use LINQ.
best stick to high level language like java.
Admin
When I was in school in the early 80's, avoiding goto was a really hot topic. But some languages still in use in that era (FORTRAN 77, ALGOL 60) did not have a BREAK statement. They didn't need one - that is what GO TO is for.
Since we were explicitly forbidden to use goto in our assignments, the recommended way to exit a loop early was to manipulate the variable values for the termination condition. This was considered "more readable" than "GO TO LOOPEND;"
Admin
Oh man, your so wrong! Or missing the point?
Why would a sane person continue looking at all the other "straws" in the "haystack" when he/she already found the "needle" he/she was looking for?
Am I missing you applying irony or something?
(Note: I threw in the "your" vs. "you are" to put some "oil on the fire".)
Admin
I bated last night, thinking of Jessica Alba.
Wait, what?
Admin
...now, what exactly did you say a sane person would do?
Skipping the needles after the one you're looking for means you're looking at every needle that came before, which is almost just as stupid as looking at all of them.
Admin
I don't see a problem with break's, but there is a way around it. Many think of the classical for (int i=0; i < 10; i++) and forget that you can actually check for more than just "i < 10)
Here's a way to avoid break's, but it's less clear in my opinion.
boolean wtf = true:; for (int i=0; i < 10 && wtf; i++) { if (a == b) { wtf = false; } }
Admin
Ha Ha Ha! PHP rulez! You can specify how many nested levels to break or continue.
Admin
I guess that's why the US needs to invade so many countries. Can't sit in one place too long because of the butt-cheek scratches inflicted by 1-ply TP.
Admin
I work in a coding house where the only code guideline I object to is that all functions returning non-void must have only and exactly one RETURN statement. I pointed out cases actually in the codebase where this means two or three temporary variables whose only function is to carry the result to the end of the function without falling into other cases, but nope; readability apparently requires exactly one entry and one exit point for any function.
Considering they allow THROW statements wherever convenient, it can be a bit frustrating.
So I forced them to, wherever a RETURN statement returns a non-constant value, it must be from a FINAL variable (i.e. one that can be initialized only once). It at least solved our frequent initialization errors caused by their strange practice.
Admin
A bit besides the point. You are making big assumptions about the problem we are trying to solve here. No one defined size of haystack or the number of needles to find. But hey, make up your own problem spec so we can argue endlessly. ;)
When binary search is apropriate, use it. When a hash table is apropriate, use it. When a break is apropriate, use it.
I completely disagree with the "break is evil" crowd. In many cases "break"ing out of a loop is perfectly legit. (Oh and don't let me get started about: "continue is evil".)
Admin
So someone checked it against the part of the coding-practices document that said HOW to use loops, but not the part that said when NOT to use them at all. They didn't see the forest for the trees, which is why bringing in new blood once in a while is a good idea. WTWTF?
Admin
So... if the rules didn't forbid him from using breaks inside a loop, or substituting a for loop for a while loop, he would have committed the 30 minute code and would have never bothered implementing the 1 minute hash based one?
Yay for rules, I say!
Admin
Admin
My experience with "Best Coding practices" is usually one person has an idiosyncratic coding style that they then foist onto everyone else. This might be OK if that person had developed 90% of the code but often it is someone who is a terrible coder and I think creates the coding standard to make themselves feel that they are in control of the situation.
The key being that while everyone else is coding they are politicking to promote their "Standard"
Admin
Admin
Another case where the problem could have been solved simply by doing the work in a database. One UPDATE statement. I guarantee that will be easier to maintain than trying to explain to a "senior" dev what a HashMap is and how it works.