Results 1 to 9 of 9
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question [SOLVED] Presentation of code, to indent or not to indent

    :confused:

    I have been trawling though books and over the web, can the more experienced programmers give me their opinion please

    if else statements are generally not indented?

    all other if's are?

    are there any other good techniques in programming regarding presentation of code.

    I am in my first year at college and they just say indent, but with all the information out there its kind of confusing with conflicting opinion.

    Thanks in advance!!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Thumbs up

    Thanks, I was getting very confused

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    I can't say I'm an estemed programmer yet, but from my experience I typically indent any block of code. If you have a lot of nested if statements or loops, you may want to reconsider how much you indent or whether doing it at all is necessary (especially if you want to print your code on paper, or paste it somewhere (like these forums) that has width limits). For those cases, I sometimes just wrap the lines around after commas and the like.

    Java Code:
    for(int i = 0; i < 10; i++)
    {
    	for(int i = 0; i < 10; i++)
    	{
    		if(true)
    		{
    			if(true)
    			{
    				if(true)
    				{
    					//more code here
    				}
    			}
    		}
    	}
    }
    
    //In my opinion, that usually looks more presentable than this:
    
    for(int i = 0; i < 10; i++)
    {
    for(int i = 0; i < 10; i++)
    {
    if(true)
    {
    if(true)
    {
    if(true)
    {
    //more code here
    }
    }
    }
    }
    }
    I've noticed a lot of people put the { right after the statements, rather than on the next line. I personally prefer putting it on a new line, just makes things easier for me to read.

    Java Code:
    for(int i = 0; i < 10; i++)
    {
    
    }
    
    //I use that, rather than this:
    
    for(int i = 0; i < 10; i++){
    
    }
    
    //Just personal preference, I suppose.

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Thanks I am a little clearer now

  6. #6
    Natrix is offline Member
    Join Date
    Mar 2009
    Posts
    40
    Rep Power
    0

    Default

    I think everyone should do it as they like it.
    Its a personal style. Its what makes us unique.

  7. #7
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Maybe but advice from someone with more programming experienced never hurt.

    Thanks

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I much prefer spaces to tabs when indenting as the result is more predictable and a tab is often rendered as a very wide space in a browser.

    Also, tangential to your question but it bears repeating: indented blocks should be marked off with braces. Choose any of the placement conventions, but use braces even for one line blocks.

  9. #9
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Actually, I might start using like 2 spaces rather than tabs. Hmm...

Similar Threads

  1. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  2. Upload a jstl presentation
    By yuval_zi in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-27-2008, 10:40 PM
  3. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  4. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •