Jump to content

Opening braces on current or next line?

Heya fellas!

 

I've recently been watching some programming tutorials for C and was wondering what the cleanest way of starting your opening braces is.

In the tutorials that I'm watching the instructor uses this method:

#include<stdio.h>main()    {    int a;    printf("Enter a integer less than 10: ");    scanf("%d", &a);    if(a < 10)    {        printf("Integer is less than 10\n");    }else    {        printf("Integer is more than or equal to 10\n");    }} 

But I tend to prefer this way:

#include<stdio.h>main(){    int a;    printf("Enter a integer less than 10: ");    scanf("%d", &a);    if(a < 10){        printf("Integer is less than 10\n");    }else{        printf("Integer is more than or equal to 10\n");    }} 

I actually don't remember where I picked this up, I've never really done much programming.

What method do you prefer and why?

export PS1='\[\033[1;30m\]┌╼ \[\033[1;32m\]\u@\h\[\033[1;30m\] ╾╼ \[\033[0;34m\]\w\[\033[0;36m\]\n\[\033[1;30m\]└╼ \[\033[1;37m\]'


"All your threads are belong to /dev/null"


| 80's Terminal Keyboard Conversion | $5 Graphics Card Silence Mod Tutorial | 485KH/s R9 270X | The Smallest Ethernet Cable | Ass Pennies | My Screenfetch |

Link to comment
Share on other sites

Link to post
Share on other sites

The first its easier to read :P (i have very basic knowledge of java and that's about it)

|| MAELSTROM ||

|| i5 3570K W/ CM 212x @ 4GHz || r9 290 Windforce || Corsair Obsidian 450D || 2tb HDD + 120gb 840 evo SSD || 2x4gb Hyperx Fury Blue

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I prefer the way you do it, because less hitting enter is always good

 

 

Side note, Australians currently dominate this thread, yay

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

I don't use C, but C#, and I would use the second one, one reason its that it's 3 lines shorter, and it helps keep the syntax cleaner looking. In C# I use the first method, it's more easy to read, and when I'm done with the main part I do that to shorten up the code.

Link to comment
Share on other sites

Link to post
Share on other sites

I like the way you do it, but that only matters to people seeing your source code.

Link to comment
Share on other sites

Link to post
Share on other sites

GENERAL GUIDE FOR READABILITY

-------

 

Use real tabs that equal 4 spaces.

 

 

Use typically trailing braces everywhere (if, else, functions, structures, typedefs, class definitions, etc.)

 

if ( x ) {

}

 

 

The else statement starts on the same line as the last closing brace.

 

if ( x ) {

} else {
}

 

Pad parenthesized expressions with spaces

 

if ( x ) {

}

 

Instead of

 

if (x) {

}

 

They are just some of the ways to go about it. Interesting reading linked below!

 

http://fabiensanglard.net/doom3/

   OR

ftp://ftp.idsoftware.com/idstuff/doom3/source/CodeStyleConventions.doc

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

 

Sooo many spaces! Nahh... it's not a problem. I will follow this from now on, it does make it far easier to read :)

Sublime Text has a feature to make all tab indentations 4 spaces, so that's easy. 

export PS1='\[\033[1;30m\]┌╼ \[\033[1;32m\]\u@\h\[\033[1;30m\] ╾╼ \[\033[0;34m\]\w\[\033[0;36m\]\n\[\033[1;30m\]└╼ \[\033[1;37m\]'


"All your threads are belong to /dev/null"


| 80's Terminal Keyboard Conversion | $5 Graphics Card Silence Mod Tutorial | 485KH/s R9 270X | The Smallest Ethernet Cable | Ass Pennies | My Screenfetch |

Link to comment
Share on other sites

Link to post
Share on other sites

There's already been a bit of discussion about this here.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
#include<stdio.h>main(){     int a;     printf("Enter a integer less than 10: ");     scanf("%d", &a);     if(a < 10)     {          printf("Integer is less than 10\n");      }      else      {           printf("Integer is more than or equal to 10\n");      }}

this is how i would format that code. a brace is its own line. and all code within a brace is indented.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×