- 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
Bad coding practices are still coding practices.
Admin
Coding guidelines should be reviewed for the edge case scenarios before being made 'law' and should have notes for said edge cases that allow things to pass that don't quite fit.
But then again last time I wrote code guidelines I'd seen how bad it could be ;D
Admin
+1 for well written entertaining article.
I'm currently working for an international quasi-governmental firm that has decided in the interest of stability and risk management to keep with as old a version as possible of every kind of technology. For software, that means Java 5, Struts 1.0 (modified), and the earliest implementations of each kind of web service. To minimize risk, they also keep all code that has been corrected or superceded. In the codebase. Without comments. They have a revision system (Clearcase) that keeps track of it all, defeating the purpose of revision control. And even though there are whole unused packages still lying around after ten years, they are still part of the codebase that gets promoted to production every two weeks.
So although there is no written rule, it is part of the cargo cult culture to pass this piece of wisdom down through the years to each new developer.
Admin
100% agreement about "stupid" rules, and the dangers of following them blindly. However, in this case, the end result was better than the intermediate attempts and were driven by the coding standards. Seems like the end result was a WIN rather than a WTF
Admin
I fully agree. But don't let the standards minions explore the source to the hashmap routines.
Admin
Hashmaps depend on containing elements implementing the .equals and .hashCode method properly and are thus inhernetly unsafe to use. See coding guidelines page 35. Reverted.
Admin
Unless someone is actually capable of justifying WHY a specific coding standard is in place, they have absolutely no place enforcing them.
Just "it's more efficient" is not justification. If they haven't profiled the code and provided actual data, then it may or may not be more efficient... the only thing that is certain is that they don't know what the fuck they're talking about.
In this case, I would be sorely tempted to write a scathing 10 page review of both the coding standards, the individual enforcing them, etc, and a analysis of exactly how much that douchebag is costing the company per week, in time, money and employee goodwill. And I would send this directly to the board. The two possible outcomes (1. they respect you, or 2. they fire you) are both preferable to continuing to work in such sub-optimal conditions.
Admin
In the end the correct refactor was made. Does logic dictate these draconian rules "work" - oh please no!
Admin
Coding guidelines that specific and broad reaching did not include any guidelines about nesting loops?
Admin
I'm pretty sure sorting and binary search would also create a huge benefit.
But that would also be reverted: complex algorithms with non trivial preconditions are prone to unpredictable behavior. Please see 'Coding Practices' document, 'Algorithms' on page 32, part 3, paragraph 3.
Admin
My first post was reverted because of Posting Practices section 'Firsts', page 13, paragraph 5. :(
Admin
I'm wondering if that reference was actually valid or whether Derek just pulled some numbers out of his head...
Admin
It's not even sane in this article's story...
Admin
Admin
Admin
Opens document at page 47, reads part 4, paragraph one ... Reads page 42, part 4, paragraph one again Waaaait a second... Opens page 47 again
WTF... They totaly contradict each other! Oo
Admin
How about jumping out of the loop with a "clever" try catch? Or how about goto?
Admin
But he had not violated that standard. This was not an opportunity to replace the While loop with a for..each loop precisely because they were not performing the same function; the While loop was conditional whereas the for..each was not (thus it required the 'illegal' use of a break).
Sounds like their standards Nazi (apologies, Mr Godwin) doesn't understand code. Sounds about right.
Admin
Stop with this nonsense, you are plaguing our minds.
Admin
I can relate to this story. I worked at a shitty place that was a small, poorly run shop that created it's own software using technologies that were 10 years out of date. That shop was taken over by a multi-billion dollar foreign company. Coding standards were enforced by the "senior" developers who've been stuck in a rut for at least 10 years, throwing tantrums if you missed a comment, broke out of a loop (heaven forbid), or optimized some garbage code from the dinosaur age. Worse than a PMSing bitch from hell. The large foreign company enforced it's own "standards" that soon created this spongy, greasy soup that was now the codebase.
Exit strategy implemented...
Admin
TLDR
Admin
See? Once he followed the specified coding practises, he was able to produce an optimal solution for the users. When he insisted on not following the coding guidelines suboptimal fixes resulted. Another win for guidelines!
Admin
See? Once he followed the specified coding practises, he was able to produce an optimal solution for the users. When he insisted on not following the coding guidelines suboptimal fixes resulted. Another win for guidelines!
Admin
I was actually thinking this. Instead of having to make a half-arsed fix to a half-thought out solution, he made an efficient solution that cut the processing time from an hour or more down to a couple of minutes.
Granted, I think being forced not to break from a loop is pretty stupid. And quibbling between using a for-each verse a while loop is also pedantic.
Admin
I once worked on a system used for Police recruitment in the UK. One of the options provided stats on applications - breaking them up by sex, age, qualifications etc and calculating percentages. It was written in an old 4GL called Dataflex, running on MSDos
The guy who wrote it thought he was being flash, and looped through all of the records in the database, the for each one adding 1 to each of the relevant categories shown on scree, the calculating the percentages before moving on to the next record.
The client liked the feature, but complained about the time it took to run. I moved the percentage calculations outside the loop, so instead of being recalculated for each record, they were calculated once at the end of the process. The client still saw the individual figures clocking up, but only got to see the percentages at the end of the run. This speeded up the process by approximately 80%.
I did try to persuade the client to have it done as a reports - click the button, then wait for the results, but they actually liked to see the figures moving - "at least we can see it's doing something"!
Admin
Malicious compliance anyone?
Admin
Something tells me that "'Collections' on page 42, part 4, paragraph 1" doesn't exists.
Admin
In all fairness if it weren't for the guidelines he would have only cut the time in half using a cheap hack vs. reworking into a proper hashtable and cutting it down to 1/60th of the time it normally took.
Admin
I drive our HR guy nuts. Every time I hit the wall against some asinine rule I always ask what the process is for over turning that rule.
The only way to save your self from bureaucracy it to add a bureaucratic process for overriding the bureaucratic rules.
Admin
Haha. How about set the loop conditional to a value that will stop it (say max + 1). I didn't call break but the condition in the for loop did :)
Admin
My pre-predecessor actually manipulated the loop conditional inside the loop (setting it to value-1) based on some conditions... I'm just glad he apparently never heard of the goto statement.
Admin
TRWTF is that without the annoying enforcement of coding practices, our hero would not have reached the ideal solution.
Admin
Admin
I believe the term is, "baited breath."
Derek decided updating his resume would be a good idea before checking to confirm that his code had been reverted yet again.
FYS
This. Rules just to have rules are bad.
Admin
Today's TRWTF is the article hinting towards a useful refactoring pattern: Lookups can be performed more efficiently by table than by while than by for.
However I'm unclear about the prefer for to while mandate. Clearly, it leads to more critical thinking for cases in which a tree (hashtable in this case) is more appropriate, but there are cases in which a for cannot replace a while such as an application pump. In which language is a for loop not compiled to a while loop?
Admin
The point about coding guidelines is: They are GUIDELINES. Not LAW paragraphs that need to be enforced mindlessly
There is probably a good reason to break almost any guideline at some point. Question is only: Do you comment WHY you have broken the guidelines?
In fact I've made some "dirty hacks" myself from time to time: Testing finds an ugly bug just one or two days short of release date. Solving the problem according to the guidelines would need a redesign that simply doesn't fit into the remaining time. --> Solution: you make a hack (if possible) and solve the problem later on once you've got the time again.
But you still need to comment the reasons and potential pitfalls in the case you got run over by a truck... (or simply don't have time to fix it in the next few months... Happy bug-tracking if you didn't...)
Admin
Therefore multiplication and addition are the same operation?
Just because you get the right answer in this one particular case doesn't mean that the guidelines are correct. If they hadn't been able to use a different structure and were stuck with loops, they'd be boned.
Also, where does this idea that breaking out of a loop is bad come from? It seems like absolute non-sense to me. In what world does looping and checking variables against conditions when you know you've already found what you are looking for make any lick of sense?
Admin
[quote user="golddog"]1. I believe the term is, "baited breath."/quote]
No, it's bated, as in "abated." It's an archaic truncation which has become idiomatic.
Admin
You believe wrong, it's correct in the article.
Admin
guess i owe you a coke
Admin
It's not always obvious what it's going to do due to nested loops etc. When one of them gets unrolled or removed later, there is a higher chance that a break gets left in and breaks the next thing up the chain than if you werne't using break. That's why I never* use them in loops.
Presumably they want you to pull the loop into its own method and return early.
* "Never" means "unless I really need to"Admin
BUT THE LOOP SHOULD ONLY BE TERMINATED WHEN THE CONDITION ISN'T MET ANYMORE!!!11oneone YOU CAN'T JUST STOP A LOOP IF IT'S CONDITION IS STILL MET!!!111onehundredeleven!!1 YOU DON'T MESS AROUND WITH CONTOL STRUCTURES!!!1111oneoneonethousandonehundredandone!!!11
Or something like that...
Admin
A few years ago I visited Switzerland. In the apartment where I was staying I found not 1-ply, not 2-ply, but... 5-ply toilet paper. I kid you not. 5 separate pieces of paper layered in each square.
And right then I knew why Switzerland has never been successfully invaded. Forget the military service requirements and the mountains riddled with bunkers. The rest of us just have no chance against any group of people who need 5-PLY TP!
Admin
I once developed a website for a company which had a guideline for their corporate design. Unfortunately this guideline was too confidential to be read by code monkeys like me...
These were funny times!
Admin
For all of you trying to find "clever" solutions based on exiting the loop: They'd almost certainly also be against the standards.
If breaks are prohibited, I'm sure that setting the conditional in a for loop is also prohibited. There's NO WAY they would allow a try/catch to do program logic. Goto would probably get him fired on the spot.
The only one I saw that MIGHT be allowed is factoring the loop into its own method and then returning mid-loop. It depends on how the rule is worded.
Admin
Admin
I suppose that's what the hero in our story wanted to do when he replaced the for-each-loop with a while-loop, but while-loops were not allowed and for-each-loops don't have this possibility.
Admin
In a previous project, my build failed due to capitalisation of "Grandchild" vs "GrandChild" in a unit test. The irony is that "ChildOfChild" would have been acceptable.
There were also rigorously enforced coding standards which required documentation to be present. It didn't have to be correct, sensible or relevant, it just had to be present.
We ended up building the best (unit) tested, best documented system that nobody wanted.
Admin
This must be "tl;dr"
Page 12: Only constants may be in all caps.
Page 38: Semi-colon must follow a qualitative adjective phrase.
*Pendantic - See: Skeptics Guide to the Universe
Admin
Only if you've eaten a bunch of worms in the hope that trout will swim into your mouth.
(Do people never think about what the words they're using actually mean?)