Thread: proper indentation

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    proper indentation

    How can I better indent this?


    Code:
    //preprocessor directives 
    #define _CRT_SECURE_NO_DEPRECATE
    #include<stdio.h>
    
    int main()
    {	
    	//declare and initialize variables
    	int someInteger = 0;
    	double doubleValue = 0;
    	double doubleResult = 0;
    	double result = 0;
    	
    	FILE *myFile;		// pointer..............to numbers.txt
    	FILE *myOtherFile;	// pointer..............to results.txt
    
    		myFile = fopen("numbers.txt", "w");			//opens a file numbers.txt to write to it (in this case numbers)
    			
    			printf("\n\tWriting these six numbers\n\t%d\n\t\t%d\n\t\t\t%d\n\t\t\t\t%d\t\t\n\t\t%d\n\t%d\n\tto the file numbers.txt...\n\n\n", -7, 31, 44, 8, -31, 55);		//this line of code is totally unnecessary
    				fprintf(myFile, "%d\n\t%d\n\t\t%d\n\t\t\t%d\t\t\n\t%d\n%d\n", -7, 31, 44, 8, -31, 55);		//this line writes six numbers to the file numbers.txt
    					fclose(myFile);			//closes the numbers.txt file so we can read from it
    
    		
    		fopen("numbers.txt", "r");						//opens numbers.txt to read a number
    			fscanf(myFile, "%d", &someInteger);			//reads the first number from the list
    
    			printf("The first integer in the file is: \n\n\t\t\t%d\n\n", someInteger);
    				printf("Enter a double to be multiplied by:\n\n\t\t\t%d\n\t\t\t", someInteger);
    					scanf("%lf", &doubleValue);			//scans for user input
    
    	
    		myOtherFile = fopen("result.txt", "w");					//opens a file result.txt to write to it (in this case the results from the calculation)
    			doubleResult = doubleValue * someInteger;			//calculates for the result
    				fprintf(myOtherFile, "%lf", doubleResult);		//stores the result into double Result
    
    		return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have an extra tab for separating the "real code" from the declarations of variables... Not that usual, I wouldn't do it.

    Also, for example, I don't see any reason for this
    Code:
     myOtherFile = fopen("result.txt", "w");               
                doubleResult = doubleValue * someInteger;
    the doubleResult seems like a nested line of code, but it is not.

    Also, notice that there is not optimum way of indenting your code. It has to do with what you are used to see. However, there is a common axis where all indentations styles divert from. Take a look here for styles of indentation. The link was found in the signature of Codeplug.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by CASHOUT View Post
    How can I better indent this?
    Normally all lines at the same control level are at the same column, and extra indentation is used for continuing long statements onto the next line
    Also if you have comments I think its usually easier to read and edit to put them on their own line
    Code:
    int main()
    {   
        //declare and initialize variables
        int someInteger = 0;
        double doubleValue = 0;
        double doubleResult = 0;
        double result = 0;
    
        FILE *myFile;       // pointer..............to numbers.txt
        FILE *myOtherFile;  // pointer..............to results.txt
    
        //opens a file numbers.txt to write to it (in this case numbers)
        myFile = fopen("numbers.txt", "w");         
    
        //this line of code is totally unnecessary
        printf("\n\tWriting these six numbers\n\t%d\n\t\t%d\n\t\t\t%d\n\t\t\t\t"
                "%d\t\t\n\t\t%d\n\t%d\n\tto the file numbers.txt...\n\n\n", -7, 
                31, 44, 8, -31, 55);
        //this line writes six numbers to the file numbers.txt      
        fprintf(myFile, "%d\n\t%d\n\t\t%d\n\t\t\t%d\t\t\n\t%d\n%d\n", -7, 31, 44,
                8, -31, 55);      
        //closes the numbers.txt file so we can read from it
        fclose(myFile);         
    
        //opens numbers.txt to read a number
        fopen("numbers.txt", "r");                  
        //reads the first number from the list    
        fscanf(myFile, "%d", &someInteger);         
    ...
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the indenting, I would

    1) Name variables in ways that reflects what they mean. A variable named doubleValue tells the reader nothing about what what the value of the variable means. A variable named SpeedOfCarInKPH is more informative. Similarly the names myFile and myOtherFile are uninformative of anything (except of the fact that your code has grown, rather than been constructed for clarity).

    2) Break the code into smaller functions that do some specified task. Choose the names of those functions carefully. An fopen() call, a series of fprintf()'s, and a fclose() can (often) logically be placed into their own function. Named, something like WriteSixDefaultValuesToFile(). Similarly, choose names of function arguments carefully, so they give useful information about what the argument represents. An argument named OutputFileName is more informative than an argument named fname.

    3) Don't comment things unless absolutely necessary to explain something that is not self-explanatory in the code. In fact, strive to have your functions, variables, constants, etc named well enough that they document themselves. Comments like "preprocessor directives" or "declare and initialize variables" will be superfluous if you do such things right. The goal is to provide enough information that you (or someone else) knows what the code achieves, not to add comments that state the obvious.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program for indentation
    By ajitnair in forum Contests Board
    Replies: 1
    Last Post: 11-09-2012, 10:51 AM
  2. indentation o indentation!
    By rogster001 in forum C++ Programming
    Replies: 18
    Last Post: 09-23-2009, 05:53 AM
  3. indentation error
    By cc870 in forum C Programming
    Replies: 3
    Last Post: 06-24-2005, 11:06 AM
  4. Dev C++ Compiler, Indentation?
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:13 AM