[SOLVED]Advice for code indentation style

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Hello guys!

I hope code indenting would be very important for professional programmer. I need some advice from experts on how to indent code properly . I want to indent C code properly so that someone can understand easily.

There are different style, When writing program which style should be followed

This style puts the brace associated with a control statement on the next line,
C:
while (x == y)
{
    Dosomething();
    Dosomethingelse();
}
This style puts the opening brace at the end of the line
C:
while (x == y) {
    Dosomething();
    Dosomethingelse();
}
Which style do you follow @MrChips ?
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Control statement style

C:
#define   X    5

int main (void)
{
    if (X)
    {
        printf(" Yes ");
    }
    else
    {
        printf(" No ");
    }
    return 0;
}
I prefer the below style
C:
#define   X    5

int main (void)
{
    if ( X == 5)
    {
        printf(" Yes ");
    }
    else
    {
        printf(" No ");
    }
    return 0;
}
 
Last edited:

Ya’akov

Joined Jan 27, 2019
9,069
Different programming communities use different conventions from cases like Python where whitespace is significant to perl where it could be written nearly any way but isn't among perl programmers.

Sometimes there is no convention to fall back on and so choosing something functional and readable for you but where the is a community convention, sticking to it even if you don't like it is really important.

As far as choices, one thing I think is best is keeping tabs to 2 spaces to keep the code from shifting too far.
 

click_here

Joined Sep 22, 2020
548
You should always follow your companies style or the style of existing code on the project.

If you don't code professionally, or you are a solo trader, you need to choose one based on what you usually see.

For example, we do a lot of C# at work which is uses "Allman style", so we use the same for our C code. It is really good for commenting out lines (for debugging) and conditional compilation.
Code:
// from wikipedia
// while (x == y)
{
    something();
    somethingelse();
}

/*** OR ***/

    int c;
#ifdef HAS_GETCH
    while ((c = getch()) != EOF)
#else
    while ((c = getchar()) != EOF)
#endif
    {
        do_something(c);
    }
"K&R style" (aka Egyptian brackets) is the other popular one, you see it (or variants of it) everywhere (like linux and Arduino sketches) - It reduces the amount of lines used

Code:
// from wikipedia
void checknegative(x) {
    if (x < 0) {
        puts("Negative");
    } else {
        nonnegative(x);
    }
}
But in the end it's just what is more readable to you and the people who will read your code - Long story short: choose a style and be consistant
 

BobaMosfet

Joined Jul 1, 2009
2,110
Hello guys!

I hope code indenting would be very important for professional programmer. I need some advice from experts on how to indent code properly . I want to indent C code properly so that someone can understand easily.

There are different style, When writing program which style should be followed

This style puts the brace associated with a control statement on the next line,
C:
while (x == y)
{
    Dosomething();
    Dosomethingelse();
}
This style puts the opening brace at the end of the line
C:
while (x == y) {
    Dosomething();
    Dosomethingelse();
}
Which style do you follow @MrChips ?
I use this style (this is original, standard 'C' style):

Code:
while(x==6)
   {
   dosomething();
   dosomethingelse();
   };
The reason I do this, is because the braces line up vertically, and in a listing across several printed pages, it is easier to find the end of a statement grouping.

The other style you mentioned, came later- this was seen a lot by Pascal programmers, and then began wider adoption. I think it looks sloppier, but that really is just my opinion- compiler doesn't care.

1622031841957.png

People think somehow they save space vertically but not putting braces on their own lines, but this is ridiculous. The compiler doesn't care which way you do it. I prefer the method I use for legibility, as stated above. But then again, I tend to comment copiously and have comment blocks above every function, and at the head of every file, detailing the logic of what the file does, replete with tables, character-drawings, etc. This go into a printed folder as well, along with flow-charts for any complex logic or processes.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I apricate any advice to improve code indenting style

I write indent code like this.

C:
/*
* Program:   Find largest number in List
* Author:    Pushkar
* Created on May 26, 2021, 6:31 AM
*/


/*Include the contents of the standard header stdio.h */
#include<stdio.h>

#define     Size          5              // Maixmum size of numbers in List

int main (void)  //Strat main
{
    int I = 0; 
  
    int List[Size] = {5, 4, 9, 2, 3};   // Given numbers in list
  
    int Largest = List[0];              // Assume first number is largest number in list
  
     // Loop strat from 0 to 5
    for ( I = 1; I < Size; I++ )     
    {
        if ( Largest < List[I] )        // Is largest number smaller than list number ?
        {
            Largest = List[I];          // Yes -> Update largest number with list number
        } 
    }// Exit Loop
  
    // Show largest number in list
    printf (" Largest Numer = %d", Largest );
  
   return 0;
} // End Main
 
Last edited:

Deleted member 115935

Joined Dec 31, 1969
0
Im old, so excuse me

my things on code style

a) don't use tabs, use white spaces and a fixed point font,
b) Comments, dont put "this is an adder" , put why you want an adder .
c) pick a style and stick to it, till company you work for has a different standard, then copy it,
 

MrChips

Joined Oct 2, 2009
30,708
I notice that you are placing comments on the closing brace.
I put the same comment on both opening and closing brace.
When your code structure gets huge and you start editing and modifying your code you will quickly lose track of which braces are paired.
 

click_here

Joined Sep 22, 2020
548
The style you are doing is "Allman Style", that's all good

Those comments are not good - As Andrew said, never say things like "//loop between foo and fah", the code already does that. Never comment "//end loop" "//Start main", the code/indentation tells you that

If it is starting to get too long, you should be thinking about using functions
 

BobTPH

Joined Jun 5, 2013
8,809
I prefer this style:

Code:
while x = 6 loop
     do_something;
end loop;

and

if x < 5 or x > 10 then
    do_one_thing;
else
    do_another_thing;
end if;
Bob
 
Top