Is this poor format?

This is a discussion on Is this poor format? within the C Programming forums, part of the General Programming Boards category; For an assignment I have to format a basic C program that I can make myself and make sure it ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Is this poor format?

    For an assignment I have to format a basic C program that I can make myself and make sure it is well indented. Is this ok from what I have done? Can I add that on my editor, the code in the if statement braces are closrer to the ident than they appear when I posted it, I dont know why C boards editor moves code around like this... Is there anything I can do to increase readabilty?


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main()
    {
     	char let;
     	int num;
    
    	printf("Enter a letter( a - g ): ");
    	 	  
        scanf("%c", &let);
    	 	  
    	 	  
        if (( let >= 'a' ) && ( let <= 'g' ))
        {
    	   	  printf("\nYou entered %c", let);
    		   	 	
     	 	  printf("\nEnter two a number between 1 and 10: ");
    		   	 	
       	 	  scanf("%d", &num);
        }
         	   	 	
        if (( num >= 1 ) && ( num <= 9 ))
        {
    	   	  printf("\nYou entered: %d", num);		 		 
        }
    
        else
        {
     		 printf("\nListen to commands!");
        } 
    	          
        getch();
        
        return 0;
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I'm not sure which is better spaces or tabs, but my compiler uses tabs and cboard doesn't mess with it

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main()
    {
    	char let;
    	int num;
    	printf("Enter a letter( a - g ): ");	  
    	scanf("%c", &let);  
    	if (( let >= 'a' ) && ( let <= 'g' ))
    	{
    		printf("\nYou entered %c", let);	 	
    		printf("\nEnter two a number between 1 and 10: "); 	
    		scanf("%d", &num);
    	}	   	 	
    	if (( num >= 1 ) && ( num <= 9 ))
    	{
    		printf("\nYou entered: %d", num);		 		 
    	}
    	else
    	{
    		printf("\nListen to commands!");
    	}        
    	getch();
    	return 0;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use spaces, then it doesn't matter where you view the code, because regardless of tab size, it will already be lined up correctly.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    true, but spaces can really slow you down. personally, I'd rather use tabs and just convert to spaces, if needed.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by Sebastiani
    true, but spaces can really slow you down. personally, I'd rather use tabs and just convert to spaces, if needed.
    Shouldn't. I believe most editors have a setting to "translate TABs into SPACEs".
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by WaltP
    Shouldn't. I believe most editors have a setting to "translate TABs into SPACEs".
    If I had to guess, you quoted the wrong person in your post. Depending on my IDE, I tend to either use spaces, or a terrible mixture of tabs and spaces. The latter usually on Dev C++ because I keep smart tabbing on which works well half the time and the other half it's just ridiculous.

    Also I've never heard of an assignment that was based about how well your formatting is. I personally think it's very programmer specific and if he were to grade you on anything but whitespace, I'd be worried, because depending on his feeling of certain formatting techiniques, he may give bad grades to acceptable code.
    Sent from my iPadŽ

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > I'm not sure which is better spaces or tabs, but my compiler uses tabs and cboard doesn't mess with it
    True, but you have to be ultra-consistent when using tabs - any hint of using spaces to adjust the formatting will totally mess up when you post it to the forum.

    Spaces will always be correct, whether viewed in your editor, posted on a message board, sent in an email, printed on paper, .....

    Tabs on the other hand can be variously programmed to mean anything from 1 to 8 spaces (or even more). Whilst this may not be a problem for your own code, when dealing with code from many different sources, which has been indented with many different tab stops, the whole thing can start to look like a mess.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    I indent my code using tabs such that it will line up no matter what the tab stop size. Some people like looking at code with 8 character tabs, some with 4, etc. My code will still line up nicely independent of the tab depth.

    I dislike using spaces because it forces people to my preferred level of indentation (I like 4).

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> Spaces will always be correct, whether viewed in your editor, posted on a message board, sent in an email, printed on paper, .....

    I don't know about that, aren't there features on forums and the like which stop people using too many of the same character in a row? like if someone writes

    "how are you????????????????????????????????????"
    some forums will translate that to

    "How are you?"
    (perhaps without correcting the capitalisation error), (unless it's a google product ), so I think that in that respect tabs are the way to go!!!! odd though that they are so big on a forum which would regularily have quite a bit of tabbing ... that's the one thing that annoys me about the forums

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by SlyMaelstrom
    Quote Originally Posted by WaltP
    Quote Originally Posted by Sebastiani
    true, but spaces can really slow you down. personally, I'd rather use tabs and just convert to spaces, if needed.
    Shouldn't. I believe most editors have a setting to "translate TABs into SPACEs".
    If I had to guess, you quoted the wrong person in your post.
    Nope. I took this statement to mean the 'slow down' is caused by repeatedly hitting the SPACE bar to line up the code properly.


    Quote Originally Posted by SlyMaelstrom
    Depending on my IDE, I tend to either use spaces, or a terrible mixture of tabs and spaces. The latter usually on Dev C++ because I keep smart tabbing on which works well half the time and the other half it's just ridiculous.
    Aren't your IDEs smart enough to have a convert setting? This seems a little odd.

    Quote Originally Posted by SlyMaelstrom
    Also I've never heard of an assignment that was based about how well your formatting is. I personally think it's very programmer specific and if he were to grade you on anything but whitespace, I'd be worried, because depending on his feeling of certain formatting techiniques, he may give bad grades to acceptable code.
    Yes, it is programmer specific. But structure is very important to readability. Just look at some of the crap that's posted on these forums before the newbie learns proper formatting techniques. And how about code posted where they use TABs and it ends up looking like:
    Code:
    int calcGold(int tot)
    {
    	if (tot == 5)
    		{
    			gold = 23 * pricePerOunce;
    				if (gold > 1000)
    					{
    						gold += tot * 3;
    					}
    		}
    		return gold;
    }
    Looks almost readable in my editor with TABs set to 4, but here?!?!? Granted it's poor formatting, but some people use it -- at least at first.


    Quote Originally Posted by twomers
    >> Spaces will always be correct, whether viewed in your editor, posted on a message board, sent in an email, printed on paper, .....
    I don't know about that, aren't there features on forums and the like which stop people using too many of the same character in a row?
    Not that I'm aware. It is true that multiple SPACEs are trimmed to one space (an HTML situation) but that's why CODE tags were invented
    Last edited by WaltP; 03-16-2006 at 11:37 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21