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

38 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 BronsCon · · Score: 2, Funny

      I think using python at all results in a love/hate relationship with python.

      --
      APK quotes people (including myself) without context and should not be trusted. Just thought you should know.
    2. Re:Some of those examples by kv9 · · Score: 2, Funny

      one true brace to rule them all, heathen!

    3. 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. The best coding standard is... by Anonymous Coward · · Score: 1, Funny

    the one that follows the code I write. Every other standard is crap.

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

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

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

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

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

  10. Re:braces by KostasPlenty · · Score: 2, Funny

    The code is important. The braces are syntactical sugar.

    Guido van Rossum is that really you?

  11. Re:braces && 3 year olds by sciop101 · · Score: 2, Funny

    The 3 year olds were at test at Stephens College (Columbia Mo) in their preschool education classes around 1986 or so. There are published papers but I've got no idea where to find them to cite them.

    These 3 year olds were given a 10 minute time out and a grape juicebox. During the timeout, the 3 year olds developed time travel, went back, deleted the test and grape juiceboxes.

    --
    The only thing new in this world is the history that you don't know.[Harry Truman]
  12. 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.

  13. Re:developer buy-in by Anonymous Coward · · Score: 1, Funny

    400 quatloos on "Minwee".

  14. 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;
    }

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

    -1, Dense

  16. Re:braces by Anonymous Coward · · Score: 1, Funny

    Imperative programming assholes! There should no "flow" in programs.

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

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

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

  19. Re:braces by maxume · · Score: 2, Funny

    Twitter would like to hire you, so that they can go on to fire you.

    --
    Nerd rage is the funniest rage.
  20. Re:Keep it simple! by Anonymous Coward · · Score: 1, Funny

    My eyes! They burn!

  21. Re:braces by Bloater · · Score: 2, Funny

    Coders that write code that needs a ruler to find the start of a block get fired much more frequently than those who don't. How many inches is the ruler you use? More than one and something is very wrong.

    All these formulaic alignment and whitespace techniques are workarounds for the fact that your code is too complicated and not structured enough.

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

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

    For one thing, they is grammatically plural.

    --MarkusQ

  23. Re:Dumb as coder by 4D6963 · · Score: 2, Funny

    Only bad coders/dumb asses write 20 lines of code for basic arithmetic.

    Only bad Slashdotters (if there can be any such thing)/dumb asses miss entirely such an obvious joke.

    I gotta say, nice code snippet though. I would have never thought of that one!

    --
    You just got troll'd!
  24. 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!
  25. Re:Keep it simple! by 4D6963 · · Score: 3, Funny

    How about just x = a * b;

    How about just WHOOOOOOSH!!

    --
    You just got troll'd!
  26. Re:Keep it simple! by Anonymous Coward · · Score: 1, Funny

    That's the effect of global warming: frosted structured programmers resurface :-)

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

    Am I missing something, is the International Sarcasm Missing Day today??

    --
    You just got troll'd!
  28. Re:Keep it simple! by Ibn+al-Hazardous · · Score: 2, Funny
    No, no, no! It's bad practice to use the post-decrement operator. The loop should read:

    do { x +=b; } while (--aNc);

    That way you won't create an extra copy of the variable when returning the value, it saves like an entire registry cell!!!1!

    --
    Yes, I am a biological organism. All rumors to the contrary are just that, rumors.
  29. The correct way to indent... by JeffAMcGee · · Score: 2, Funny

    Most people's ... programs should be indented six feet downward and covered with dirt.
    —Blair P. Houghton

    --
    This sig cannot be proven true.
  30. Re:Space Usage by Anonymous Coward · · Score: 1, Funny

    I'm trying to kill off one of these beasts right now - the users hate it, the only developers left are $1000/day "consultants," and it's 5 years out of date with respect to business needs.

    So you are trying to get rid of Windows XP?

  31. Re: BEHOLD! by Anonymous Coward · · Score: 1, Funny

    /* gcc -W -Wall -ansi -pedantic basic.c -D_STFU_MINUS_W -O3 */
    /* gcc -E -D_NO_INCLUDES basic.c | indent */
     
    #ifndef _NO_INCLUDES
    #include <stdio.h> /* use -D_NO_INCLUDES if you're going to use -E */
    #endif
     
    /* C-BASIC */
    #define SUB(_x) int _x (
    #define DIM int
    #define DiM , int
    #define BEGINSUB ) {
    #define LINE(_x) ; /* silence unused label warning */ goto _##_x; _##_x
    #define LET
    #define GOTOLINE(_x) goto _##_x
    #define INC ++
    #define UNLESS(_x) if (!(_x))
    #define RETURN return
    #define ENDSUB ; }
     
    /* multiply two numbers */
    SUB(multiply)
      DIM a
      DiM b
    BEGINSUB
      DIM i0, i1, i2
      LINE(100): LET i0 = 0
      LINE(110): LET i2 = 0
      LINE(120): GOTOLINE(140)
      LINE(130): INC i0
      LINE(140): UNLESS (i0 < a) GOTOLINE(210)
      LINE(150): LET i1 = 0
      LINE(160): GOTOLINE(180)
      LINE(170): INC i1
      LINE(180): UNLESS (i1 < b) GOTOLINE(130)
      LINE(190): INC i2
      LINE(200): GOTOLINE(170)
      LINE(210): RETURN i2
    ENDSUB
     
    int
    main (int argc, char *argv[])
    {
      int result = multiply (30011, 50021);
      printf ("Result = %d\n", result); /* note: 30,011 * 50,021 = 1,501,180,231 */
      return 0;
    #if _STFU_MINUS_W
      argc = argc; argv = argv; /* use -D_STFU_MINUS_W to get rid of warnings */
    #endif
    }

  32. Re:braces by kv9 · · Score: 2, Funny

    However, I don't know anyone else in the Universe who codes like this, and I can't figure out why.

    they all got shot.

  33. Re:braces by wkitchen · · Score: 5, Funny

    YOU FORTH LOVE IF HONK THEN

  34. Re:Keep it simple! by Anonymous+Brave+Guy · · Score: 2, Funny

    You'd better hope so. For loops and sparse data usually make a pretty inefficient combination...

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  35. 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