Thread: Tidy code...

  1. #1
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44

    Tidy code...

    Evening:
    Besides neatness, are there any other practical reasons for indentation of code? I find indented code actually quite difficult to read (human eyes) Before finishing a project I always write code left-indented; I like the flush edge look, but that is just me. When I post code on this board people must think "What unruly junk!" (and at times it is indeed ) Any thoughts are really appreciated and will help me develop along a choice path as a programmer. Thanks.
    "Nay! But you love the present life!"

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I find indented code actually quite difficult to read
    You're a member of the .000000001% of the population that's crazy like that. Everyone else seems to think otherwise.

    >When I post code on this board people must think "What unruly junk!"
    No, they probably think "Feh, another one forgot code tags".
    My best code is written with the delete key.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    There actully are practal reasons for indentaion of code. As for the amount of indenation and what to indent I prefer to code like this.
    Code:
    #include <iostream>
    
    int main(void)
    {
      int variable = 0;
    
      if(variable == 0)
      {
         std::cout<<"Variable equals 0"<<std::endl;
      }
      
      return 0;
    }
    same kind of spacing for loops and I always use braces plus the extra two spaces. but by no means am I the standard. The reasons for indentation and such is to see your program flow as with all left justified it might get confusing to see what if statement is doing what and such.
    eg.
    Code:
    #include <iostream>
    
    int main(void)
    {
    int a = 0;
    int b = 0;
    
    if(a == 0)
    {
    if(b == 0)
    {
    std::cout<<"a and b equal 0"<<std::endl;
    }
    }
    
    return 0;
    }
    Woop?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You have a bright future in obfuscated C code contests.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    conventional programming, from naming conventions to format conventions, are important if you ever do programming with a team. Everyone relies on each other to do their job perfect and they won't be too happy if you don't follow conventions. Otherwise, if you are just doing one man projects, be sloppy if thats what you want. Especially in C. The only one it can hurt is you lol.

  6. #6
    Hello,

    Indentation is very important to the readability of your code. Proper indentation can show the reader the structure of the code at a glance, but inconsistent indentation can mislead the reader.

    Proper, consistent indentation is required to emphasize program structure. It is also important to maintain a consistent format to aid in searching for the beginning and end of a control structure. Indentation is used to highlight the block structure of the code and should be used consistently. The proper use of whitespace and indentation makes the structure of the program evident from the layout of the code.

    To help the understanding of your C/C++ code, whitespace (spaces, tabs, new lines) are all ignored by the compiler, which means you can scatter this whitespace all over your code and make it readable by everyone. This is especially helpful when asking for help on forums. Very few want to read code that has no formatting. It makes the code hard to understand. Also, lack of formatting make some errors nearly invisibe, like mismatched braces and the like.

    Code blocks are defined by their indentation. By "code block", I mean functions, if statements, for loops, while loops, and so forth. Indenting starts a block and unindenting ends it. There are no explicit braces, brackets, or keywords. This means that whitespace is significant, and must be consistent. The number of spaces used for indentation is a matter of taste (usually about 3 or 4 spaces is normally used), but whatever number you use you must be consistent.

    You can adapt to your own Coding Style. Just remember, a consistent mark for indentation is advised.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Properly formated code is good for other people to follow your work. good indentation and commenting is crucial to writing for buisnesses that want to be able to give your code to another and have them capable of picking up were you left off with minimal delay.


    But unformated code is good if you are trying to hide what is happening. for instance if you were coding for internet (html, xml, etc.) and you wanted to hide how everthing works.
    i.e.
    Code:
    #include <iostream>int main(void){int variable = 0;if(variable == 0){std::cout<<"Variable equals 0"<<std::endl;}return 0;}
    But any good programmer would still be able to read it after some time.
    Last edited by xviddivxoggmp3; 11-28-2004 at 06:53 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, you do need one new line in that code (for the header file).
    Anyway, check out the IOCCC

    And on the serious side, everyone's points are valid:
    Easy to read = Less bugs + Easier to maintain + Happier coders who have to maintain = More efficient coding process + Happier users
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    opps about the mistype.

    anyway.

    Zach, that website has some pretty intense examples of Obfuscated code.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    i like indentation because it gives me a cause and effect type visual:
    Code:
    if(a == 1) // cause
      b = 2;    // effect
    But i find in some other languages indenting can get in the way. But that is usually specific to doing one task. As a whole, I think indenting just gives most people a way to read code fast. When reading if statements sometimes you have to do some retracing your steps when everything is unindented.

  11. #11
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    If you don't like to indent you can always use Alt-F8 in Visual C++ after you have selected your code press alt-F8 and it will automatically indent everything for you. I am sure there are other code formatters out there you can check into. I am not sure what compiler your using.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > are there any other practical reasons for indentation of code?
    No one will help you
    No one will work with you
    No one will hire you
    On a line-for-line basis, your code will always have more bugs than well-formatted code.

  13. #13
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Just about the only reason to NOT use indentation is to save a bit of diskspace for your sourcefiles.
    A properly indented source of say 1000 lines would be maybe 3k larger than a source without any indentation whatsoever.
    If you're really strapped for diskspace and can't afford to add another disk, remove indentation as a last resort. But beware of doing so in Python code as there indentation IS relevant and determines program flow.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >Just about the only reason to NOT use indentation is to save a bit of diskspace for your sourcefiles.

    Well...yes...you drive a hard argument there. However take HTML for example. The code that I work with is pretty and indented. The code that I post on the web will be stripped of extra spaces, new lines and indentation. Still no reason I see giving up readable code for a marginal gain.

    >Python code as there indentation IS relevant and determines program flow.

    Very good point on this one. Laguages such as python do not use { } they go strictly by the indentation. But even then I'd hardly use this as driving reason to use indentation.

    Long story short. I think we should all send you a very large source files that are indented but just to keep you on your toes delete random closing braces so if statements and loops are not properly closed. A very fun problem to fix without proper indentation.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Just about the only reason to NOT use indentation is to save a bit of diskspace for your sourcefiles.
    Or just delete some of the other crap on your machine, like programs no longer used, music never listened to, pictures never viewed.

    Saving disk space has never been an argument in favour

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Application to tidy up you Code
    By sumit_rai in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2007, 12:24 PM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM