Thread: Reducing the length of code

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Reducing the length of code

    According to my teacher I shouldn't have a line of code longer than 80 characters. So if anyone could help me into breaking this up some other way that would be great.

    Code:
    while ((sortedListTrav->next != NULL) && (CompareNodes(empListTrav, sortedListTrav->next, sortField) >= 0))
                sortedListTrav = sortedListTrav->next;
    I was thinking of changing it to an if statement but wasn't sure...

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i think this is more appropriate than an if statement.

    the only reason your teacher would say that is to make the code more readable by other programmers. just use line breaks and/or whitespace in your code when a situation like this arises, such as:

    Code:
    while ((sortedListTrav->next != NULL) && 
           (CompareNodes(empListTrav, sortedListTrav->next, sortField) >= 0))
                sortedListTrav = sortedListTrav->next;
    this still doesnt look the greatest but oh well. only other thing to do to make it shorter is to use shorter variable names, which is inappropriate so my opinion is its fine.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Ok... Thanks

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    while ((sortedListTrav->next != NULL) && 
           (CompareNodes(empListTrav, sortedListTrav->next, sortField) >= 0))
                sortedListTrav = sortedListTrav->next;
    this still doesnt look the greatest but oh well. only other thing to do to make it shorter is to use shorter variable names, which is inappropriate so my opinion is its fine.
    You can also remove those highlighted parentheses. Comparison operators (!=, ==, <=, >=, <, >) all have higher precedence than || and &&. Thus, you can say if(i >= 0 && i < 10) Of course, that's only four bytes, so I'd still break it into two lines. You can place whitespace (spaces, tabs, newlines) almost anywhere in code as long as it doesn't breakup function names, variable names, etc.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If all you're worried about is an 80 character line limit, then you can break the line in half where ever you can put spaces. "Whitespace" means newlines, tabs, spaces, etc; you can have whitespace almost where ever you want. (<ws> is where whitespace can go.)
    Code:
    if<ws>(<ws>x<ws>==<ws>7<ws>)<ws>{
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM