Thread: Clarity vs Brevity

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Lightbulb Clarity vs Brevity

    I know it depends on you style of coding, but when you write a program, what's more important to you: Clarity or brevity. For example, some people find it very rewarding when they get a lot of 'bang' out of a short amount of code, while others prefere to extend this code for easier readability. What's your oppinion/style?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If I can explain the short code so that I can understand it a month later, I'll do that. Otherwise I'll use the longer method.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Long code unless resources are limited. Or write the long code first, and when the app is finished, replace it with the short code, but keep the long version in a comment.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Brevity and obfuscation is basically for fun.

    For work, clarity is much more important.

    Code:
    int foo ( int a, int b ) {
      int c;
      if ( a > b ) {
        c = a;
      } else {
        c = b;
      }
      return c;
    }
    
    int bar ( int a, int b ) {
      return a > b ? a : b;
    }
    With gcc at least, these produce identical optimised code

    If it's obvious to you, then its obvious to the compiler as well.

    Take this example
    Code:
    int foo ( int a, int b ) {
      int   t;
      t = a;
      a = b;
      b = t;
      printf( "%d %d\n", a, b );
    }
    
    
    int bar ( int a, int b ) {
      a ^= b;
      b ^= a;
      a ^= b;
      printf( "%d %d\n", a, b );
    }
    My compiler completely eliminates the swap in the former case, and just passes b,a to printf instead. All you've managed to do in the latter is confuse what is actually going on, so the compiler has to take the conservative approach and do what you asked.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what's more important to you: Clarity or brevity.
    I enjoy one-liners as much as anyone else, but for production code you should err on the side of clarity. The goal isn't to astound people with your brilliance by writing code not even you can understand a day after writing it, but to write code that anyone can follow and easily maintain.
    My best code is written with the delete key.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Prelude
    >what's more important to you: Clarity or brevity.
    I enjoy one-liners as much as anyone else, but for production code you should err on the side of clarity. The goal isn't to astound people with your brilliance by writing code not even you can understand a day after writing it, but to write code that anyone can follow and easily maintain.
    Unless you're worried about job security. Then obfuscation guarantees you're the only one that can work on the code. It rarely works though
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Brevity and obfuscation is basically for fun.

    For work, clarity is much more important.
    Of course this first example is very extreme. Any programmer with a bit of experience in C or C++ would probably look at you as if you're insance if you tell him to use the first variant over the second because of clarity, in this simple form.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Unless you're worried about job security.
    If you're worried about job security then you probably write bad code anyway, so you've nothing to lose by trying to improve yourself.
    My best code is written with the delete key.

  9. #9
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    aww yee!, really cool guys. Thanks for you input. lol, i know there's sometimes that i wish i could take 50 lines of code and compress it into 5, and say "look how great i am everyone!" hehe. But your right, clarity is much more important in most cases. cheers

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    IMO,


    Code:
    int bar(int a,int b)
    {
        return (a>b?a:b); //return which is bigger (a or b)  
    }
    is clearer than

    Code:
    int foo(int a,int b)
    {
       int c;
    
       if(a>b)
         c = a;
       else
         c = b;
    
      return c;
    }
    is clearer than

    Code:
    int foo ( int a, int b ) {
      int c;
      if ( a > b ) {
        c = a;
      } else {
        c = b;
      }
      return c;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Quote Originally Posted by major_small
    IMO,


    Code:
    int bar(int a,int b)
    {
        return (a>b?a:b); //return which is bigger (a or b)  
    }
    is clearer than

    Code:
    int foo(int a,int b)
    {
       int c;
    
       if(a>b)
         c = a;
       else
         c = b;
    
      return c;
    }
    is clearer than

    Code:
    int foo ( int a, int b ) {
      int c;
      if ( a > b ) {
        c = a;
      } else {
        c = b;
      }
      return c;
    }
    Right, that doesn't prove much except for that brevity and clarity aren't always mutually exclusive. I think what he meant was something like:
    Code:
    if(x>57&&j<3&&q!=12||!strcmpi(q,d)&&(SuperBit>0x0FF?SuperBit<<9:SuperBit^2)-92&&OMGWTFBBQ)

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I know that, I was just pointing out that in that case, I disagreed...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Prelude
    >Unless you're worried about job security.
    If you're worried about job security then you probably write bad code anyway...
    Or you are in the computer field.

    No matter how great your code is, layoffs happen. Remember a couple years ago? Job security doesn't exist in today's market.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >No matter how great your code is, layoffs happen.
    My point being that if you know your stuff you'll likely find yourself gainfully employed regardless of what happens.

    >Remember a couple years ago?
    Ugh, don't remind me.

    >Job security doesn't exist in today's market.
    That's because your definition of job security is too narrow.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 09-09-2005, 12:34 AM