Slashdot Mirror


Best and Worst Coding Standards?

An anonymous reader writes "If you've been hired by a serious software development house, chances are one of your early familiarization tasks was to read company guidelines on coding standards and practices. You've probably been given some basic guidelines, such as gotos being off limits except in specific circumstances, or that code should be indented with tabs rather than spaces, or vice versa. Perhaps you've had some more exotic or less intuitive practices as well; maybe continue or multiple return statements were off-limits. What standards have you found worked well in practice, increasing code readability and maintainability? Which only looked good on paper?"

19 of 956 comments (clear)

  1. Some of those examples by AndGodSed · · Score: 4, Funny

    Sound an awful lot like coding in C... no bad coding practice needed...

    1. Re:Some of those examples by mazarin5 · · Score: 3, Funny

      Spot the bug

      The elseif block never runs, even if you put the brace back in? :)

      --
      Fnord.
  2. It's easy by krkhan · · Score: 5, Funny

    First off, I'd suggest printing out a copy of the /. comments, and NOT read it. Burn them, it's a great symbolic gesture.

  3. Re:developer buy-in by Minwee · · Score: 5, Funny

    ask your developers to create the standard together.

    Why don't you just give them all giant Q-tips and play the Star Trek fight music every time they meet?

    Surely that would be at least as productive as asking them to all agree on coding standards.

  4. Re:braces by fictionpuss · · Score: 5, Funny

    True - but at least it keeps thousands of otherwise dangerous PHP developers safely occupied.

  5. Re:Keep it simple! by 4D6963 · · Score: 5, Funny

    Make it "cut and paste" friendly, and as small as possible.

    Cut and paste causes code cloning, which is among the most difficult maintenance problems. Code should be designed, when possible, in small chunks (methods, functions, etc.).

    Wait.. are you trying to say that copying the same lines of code over and over again must be avoided? So tell me genius, how else would you implement such a function without copying?

    int multiply(int a, int b)
    {
    int x=0;

    if (a==1)
    {
    x+=b;
    }

    if (a==2)
    {
    x+=b;
    x+=b;
    }

    if (a==3)
    {
    x+=b;
    x+=b;
    x+=b;
    }

    // Damn lameness filter, wouldn't let me paste my code in the entirety of its 132,356 lines

    return x;
    }

    --
    You just got troll'd!
  6. Re:badness abounds in visual basic by Splab · · Score: 4, Funny

    I love abbreviations, not being native English speaker I used to think POS was "piece of shit" since its usually being used when talking about software (failed software).

    (Or the always common "IANAL", well good for you buddy, but we are talking about legal issues here - the arse pounding is for when you get behind bars)

  7. Re:braces by Rakshasa+Taisab · · Score: 3, Funny

    Yes I totally

        agree,

    It is

    so much better to

        put

            a lot of vertical

    space

        between lines.

    --
    - These characters were randomly selected.
  8. Re:Keep it simple! by Haeleth · · Score: 5, Funny

    Duh, you so need to learn about this little thing called structured programming, which can totally help cut down on code duplication like that crap.

    Here's a hint:

    int multiply(int a, int b)
    {
        int x = 0;
      loop:
        if (!( a --> 0 )) goto done;
        x += b;
        goto loop;
      done:
        return x;
    }

    See? Much easier to understand than your spaghetti code, and much more maintainable too.

  9. Re:braces by russotto · · Score: 4, Funny

    Real coders write code that you can take a ruler from any given close brace and draw a vertical line right up to the matching open brace, every time. Everybody else gets fired.

    Messrs Kernighan and Ritchie and their no-necked associates would like to have a word with you out back.

  10. Re:Keep it simple! by LavaDog · · Score: 3, Funny

    I'm sorry, but that code goes against our coding standard by having non-const parameters and a goto. I suggest the following before submitting your changes:

    int multiply(int const a, int const b)
    {
      assert(a >= 0 );
      int x = 0, aNc = a;
      while (aNc--) x += b;
      return x;
    }

  11. Re:Keep it simple! by Anonymous Coward · · Score: 3, Funny

    -1, Dense

  12. Re:Keep it simple! by Anpheus · · Score: 4, Funny

    Try multiplying it by -1 and see if your stack is large enough.

  13. Re:Space Usage by Tony+Hoyle · · Score: 4, Funny

    Nah me neither... maybe it's a US thing? It's generally assumed that if you're a half decent programmer you'll follow what is already there and write clear concise code as much as possible.

    One place I was in did try to come up with a coding standard, but it was abandoned as nobody could actually agree on much other than 'don't fuck it up'.

  14. Dual widescreens by MarkusQ · · Score: 4, Funny

    You have dual widescreens? What's a dual widescreen?

    For one thing, they is grammatically plural.

    --MarkusQ

  15. Re:Keep it simple! by 4D6963 · · Score: 3, Funny

    When you copy code you also copy whatever bugs exist in that code. If something needs to be reused several times then it should be made into a function.

    Crap, you're right! Fortunately there's an easy way to fix this :

    :%s/x+=b/x = addition(x, b)/

    --
    You just got troll'd!
  16. Re:Keep it simple! by 4D6963 · · Score: 3, Funny

    How about just x = a * b;

    How about just WHOOOOOOSH!!

    --
    You just got troll'd!
  17. Re:braces by wkitchen · · Score: 5, Funny

    YOU FORTH LOVE IF HONK THEN

  18. Whitespace is your friend by wmbetts · · Score: 3, Funny

    Personally I write stuff like
    if

    (

    a > b

    )

    {

    doSomething();

    }

    else

    {

    zomg();

    }

    Then I charge the client 5 * the number of lines in a all source and header files.

    --
    "Ubuntu" -- an African word, meaning "Slackware is too hard for me". - stolen from Dan C alt.os.linux.slackware