• (cs)

    Bad coding practices are still coding practices.

  • Paul (unregistered)

    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

  • (cs)

    +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.

  • (cs)

    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

  • (cs) in reply to TheCPUWizard
    TheCPUWizard:
    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

    I fully agree. But don't let the standards minions explore the source to the hashmap routines.

  • Auditor of Reality (unregistered)

    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.

  • (cs)

    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.

  • Dave (unregistered)

    In the end the correct refactor was made. Does logic dictate these draconian rules "work" - oh please no!

  • drake (unregistered)

    Coding guidelines that specific and broad reaching did not include any guidelines about nesting loops?

  • (cs)

    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.

  • Icy (unregistered)

    My first post was reverted because of Posting Practices section 'Firsts', page 13, paragraph 5. :(

  • Falc (unregistered)

    I'm wondering if that reference was actually valid or whether Derek just pulled some numbers out of his head...

  • (cs) in reply to eViLegion
    eViLegion:
    Just "it's more efficient" is not justification.

    It's not even sane in this article's story...

  • ZoomST (unregistered) in reply to tin
    tin:
    eViLegion:
    Just "it's more efficient" is not justification.
    It's not even sane in this article's story...
    And it is quite popular, I saaw it multiple times. Somebody tried it time ago, yielding to a difference of 1.3 microseconds -- too much! But the company fails later to realize that the compiler was changed three times since then, and doing now the test will yield to less-to-none difference. But enforcing drones never question what is written in the book ("Coding Practices" documentation). Cargo cult at is maximum expression, indeed.
  • Chronomium (unregistered)
    Please see 'Coding Practices' document, 'Collections' on page 42, part 4, paragraph 1.
    I think you mean page 47.
  • VictorSierraGolf (unregistered) in reply to Chronomium
    Chronomium:
    Please see 'Coding Practices' document, 'Collections' on page 42, part 4, paragraph 1.
    I think you mean page 47.

    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

  • Smug Unix User (unregistered)

    How about jumping out of the loop with a "clever" try catch? Or how about goto?

  • Krunt (unregistered)
    “While loops are not as efficient as the for..each loop, and should be avoided whenever there is an opportunity to use a for..each loop.

    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.

  • trololo (unregistered) in reply to Smug Unix User
    Smug Unix User:
    How about jumping out of the loop with a "clever" try catch? Or how about goto?

    Stop with this nonsense, you are plaguing our minds.

  • Popeye (unregistered)

    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...

  • Dave (unregistered)

    TLDR

  • Joel (unregistered)

    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!

  • Joel (unregistered)

    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!

  • It's Pat (unregistered) in reply to Joel
    Joel:
    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!

    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.

  • Chris Q (unregistered)

    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"!

  • Plz send me teh rulz (unregistered)

    Malicious compliance anyone?

  • (cs)

    Something tells me that "'Collections' on page 42, part 4, paragraph 1" doesn't exists.

  • mizchief (unregistered) in reply to Dogsworth
    Dogsworth:
    Bad coding practices are still coding practices.

    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.

  • mizchief (unregistered) in reply to Paul
    Paul:
    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

    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.

  • Mike (unregistered) in reply to trololo

    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 :)

  • Hannes (unregistered) in reply to Mike
    Mike:
    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 :)

    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.

  • Martin (unregistered)

    TRWTF is that without the annoying enforcement of coding practices, our hero would not have reached the ideal solution.

  • luptatum (unregistered) in reply to mizchief
    mizchief:
    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.
    Nope:
    He’d just saved at least an hour for most people, if not more. Derek then began to dig further into the code, but before getting anywhere significant, an email hit his box
    Maybe he'd have got to the hashtable quickly enough to make the initial improvement irrelevant, but if not, then the standards pedants prevented anyone using the program in the meantime from benefiting from the partial improvement.
  • golddog (unregistered)
    1. I believe the term is, "baited breath."

    2. Derek decided updating his resume would be a good idea before checking to confirm that his code had been reverted yet again.

    FYS

    1. eViLegion:
      Unless someone is actually capable of justifying WHY a specific coding standard is in place, they have absolutely no place enforcing them.

    This. Rules just to have rules are bad.

  • Paul Neumann (unregistered)

    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?

  • Walky_one (unregistered)

    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...)

  • nitePhyyre (unregistered) in reply to Joel
    Joel:
    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!
    2+2=4 2x2=4

    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?

  • Jeff Ong (unregistered) in reply to golddog

    [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.

  • boppןob (unregistered) in reply to golddog
    golddog:
    1. I believe the term is, "baited breath."

    You believe wrong, it's correct in the article.

  • boppןob (unregistered) in reply to boppןob
    Jeff Ong:
    No, it's bated, as in "abated." It's an archaic truncation which has become idiomatic.
    boppןob:
    You believe wrong, it's correct in the article.

    guess i owe you a coke

  • On a Break (unregistered) in reply to nitePhyyre
    nitePhyyre:
    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?
    The issue is probably the "break" statement itself.

    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.

    return_type get_result_from(list)
    {
      foreach(thing, list) {
        result = check_stuff(thing);
        if (got_answer) return result;
      }
      return bad_result;
    }
    
    * "Never" means "unless I really need to"
  • VictorSierraGolf (unregistered) in reply to nitePhyyre
    nitePhyyre:
    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?

    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...

  • Chelloveck (unregistered)
    [...] or how many sheets of TP to use for #1 vs. #2, and whether you should choose one ply or two ply.

    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!

  • ANON (unregistered)

    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!

  • ztrem (unregistered)

    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.

  • (cs) in reply to ztrem
    ztrem:
    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.
    If they've got a rule that says you can't break out of a loop early, then they've probably also got a rule that says methods must only have a single return.
  • ANON (unregistered) in reply to Mike
    Mike:
    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 :)

    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.

  • Trevor D'Arcy-Evans (unregistered)

    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.

  • PendanticProgrammer (unregistered) in reply to Dave
    Dave:
    TLDR

    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

  • ceiswyn (unregistered) in reply to golddog
    golddog:
    1. I believe the term is, "baited breath."

    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?)

Leave a comment on “Coding Practices MUST Be Followed”

Log In or post as a guest

Replying to comment #415710:

« Return to Article