LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2010, 02:25 PM   #1
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Preferred coding style?


Hi, all. I know this is kind of a subjective thing, but does anyone have a really "preferred" style of writing their code (particularly C/C++)?

I've seen many different ways of going about structure, for example:

Code:
#include <stdio.h>

int main()
{ //opening bracket goes on newline
    printf("Hello, World!\n");
    return 0;
}

or...

#include <stdio.h>

int main() { //opening bracket goes on same line as func declaration
    printf("Hello, World!\n");
    return 0;
}
Or using/not using spaces in parameter or array lists:

Code:
#include <stdio.h>
#include <SomeOtherHeader.h>

int main()
{
    call_some_function(param1, param2, param3); //spaces between parameters
    return 0;
}

or...

#include <stdio.h>
#include <SomeOtherHeader.h>

int main()
{
    call_some_function(param1,param2,param3); //no spaces between parameters
    return 0;
}
Is there any explanation behind these differing styles, other than preference/visual appeal? I'd just like to see what kind of rationale is behind a particular coding style.

I've always put the opening bracket to a structured statement (e.g. a new function, if/else/switch, while/for/do-while, etc.) on the new line under the statement itself, like the first example above, unless the statement just has one line within it, in which case I simply do away with the brackets altogether.

As for spacing parameters, I don't, because I think of it as being more like a mathematical list (like when you're writing out coordinates on a grid) than a word list.

(Mods, if this belongs more in General, go ahead and move it. I didn't know if this was the right place for a thread like this.)

Last edited by MrCode; 03-25-2010 at 02:28 PM.
 
Old 03-25-2010, 02:43 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Very controversial, and not really easy to change but all are acceptable but the last; No spaces in function arguments reduces readability alot.
--edit--
And do not put it like:
Code:
void somefunction(hello, world)
char *hello;
int world;
{ }
That is a very outdated style.
My 1991 compiler warns its outdated and if its outdated in 1991 well today its ...

Last edited by smeezekitty; 03-25-2010 at 02:48 PM.
 
Old 03-25-2010, 03:04 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I think that putting spaces between lists increases readability and butting the opening brace on the same line makes the code more compact, like this:

Code:
int main() {
    call_some_function(param1, param2, param3);
    return 0;
}
I also try to avoid "shortcuts" like these if at all possible, because they are almost unreadable when used improperly and (especially example #2) can cause bugs later on:

Code:
//Example #1

if(something) do_something();

// Example #2

if(something)
    do_something();
There is one exception to this rule, though, where Example #1-style is more readable:

Code:
if(something1) do_something_a();
if(something2) do_something_b();
if(something3) do_something_c();
if(something4) do_something_d();
if(something5) do_something_e();

Last edited by MTK358; 03-25-2010 at 03:09 PM.
 
Old 03-25-2010, 03:11 PM   #4
Vrajgh
Member
 
Registered: Aug 2005
Posts: 68

Rep: Reputation: 33
I like to put the opening brace on a new line so that if I scan up the left side of source file I can see opening and closing braces aligned so it is more obvious if I miss one (although the text editor make it pretty obvious at time!)
 
Old 03-25-2010, 03:12 PM   #5
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864

Original Poster
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by MTK358
butting the opening brace on the same line makes the code more compact
Well, yeah, I suppose it can reduce the overall number of lines. It's just harder for me to read. When the braces are on their own lines, it's easier for me to tell when a structure opens and closes.

For example if you have multiple nested loops/conditions:

Code:
void SomeFunction(param1,param2,param3)
{
    if(param1 <= param2)
    {
        while(param3 > param2)
        {
            printf("Parameter 1 is currently less than parameter 2")
            param3--;
            param1++;
        }
    }
}
That same function, with opening braces on the same lines as the declarations:

Code:
void SomeFunction(param1,param2,param3) {
    if(param1 <= param2) {
        while(param3 > param2) {
            printf("Parameter 1 is currently less than parameter 2")
            param3--;
            param1++;
        }
    }
}
Granted, it reduces the number of lines, but IMO it kills readability. It almost makes the ending braces look nonsensical (even though they most definitely are not).

Last edited by MrCode; 03-25-2010 at 03:16 PM. Reason: changed code to make a little more sense :-P
 
Old 03-25-2010, 03:16 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by MrCode View Post
Granted, it reduces the number of lines, but IMO it kills readability. It almost makes the ending braces look nonsensical (even though they most definitely are not).
I don't know, maybe it is a better way.
 
Old 03-25-2010, 03:35 PM   #7
hockeyman_102
Member
 
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Rep: Reputation: 15
There is definitely a limit to returns - I've seen some programs that are waaay too spaced out and it's hard to see much on the screen. But that being said, i am a fan of returns and tabs - basically if you can look at your code 8 months later, and figure out what you were doing and fix it quickly... it's fine.
 
Old 03-25-2010, 04:08 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
One of the worst things i have ever done looked like this:
Code:
for(int a = 0;a < 255;a++){for(int b = 0;b < 255;b++){
for(int c = 0;c < 255;c++){for(int d = 0;d < 255;d++){
if(cond1){goto j0;}
if(cond2){goto j1;}
}}}j1:;}j0:;
 
Old 03-25-2010, 04:59 PM   #9
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864

Original Poster
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by smeezekitty
Code:
for(int a = 0;a < 255;a++){for(int b = 0;b < 255;b++){
for(int c = 0;c < 255;c++){for(int d = 0;d < 255;d++){
if(cond1){goto j0;}
if(cond2){goto j1;}
}}}j1:;}j0:;
Ack, I've seen über-concatenated code like that before — although it might have just been to obfuscate it to discourage copying...?

Last edited by MrCode; 03-25-2010 at 05:00 PM.
 
Old 03-25-2010, 06:36 PM   #10
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
I've always thought the best style is a consistent style - so long as it's not unreadable, you soon pick up a style if you read large blocks of code in it.

For example, I use braces-on-the-same-lines-as-declarations when programming in Java, because that's Java style - I'll also use camelCaseVariables.

However, most C code (at least most that I read) tends to use braces-on-the-next-line style and underscore_separated_variable_names, so I use that when programming in C.

And when contributing to a project, just follow their coding style, even if you think it's a bit stupid - there's nothing more strikingly distracting as reading a function with three different coding styles used!
 
Old 03-25-2010, 07:19 PM   #11
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864

Original Poster
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Quote:
However, most C code (at least most that I read) tends to use braces-on-the-next-line style and underscore_separated_variable_names, so I use that when programming in C.
I tend to use camelCase when naming variables, too. I'll also use a SimilarThing when naming functions. But that's in C

Quote:
And when contributing to a project, just follow their coding style, even if you think it's a bit stupid - there's nothing more strikingly distracting as reading a function with three different coding styles used!
And this is ⅓ of the reason I don't do any coding on FOSS. The other ⅔ are lack of motivation and shyness.

Last edited by MrCode; 03-25-2010 at 07:31 PM. Reason: cured RAS syndrome :-P
 
Old 03-26-2010, 09:05 AM   #12
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by JohnGraham View Post
... the best style is a consistent style ...
I agree and that is why I use formatters, such as:
Code:
       Artistic  Style  (or astyle) is a reindenter and reformatter for the C,
       C++, C# and Java programming languages.
and
Code:
       The  indent  program  can  be used to make code easier to read.  It can
       also convert from one style of writing C to another.
and I use formatters for shell and perl scripts as well. Some exist for other languages like Fortran, but there are many chronologically different versions for Fortran -- 66, 77, 90 ... -- that no one code covers them all.

You may be able to find sets of guidelines such as Perl Best Practices for other suggestions.

There are a lot of knobs to twist in astyle and indent.

Have fun, good luck ... cheers, makyo

Last edited by makyo; 03-26-2010 at 09:07 AM.
 
Old 03-26-2010, 12:28 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by MrCode View Post
I tend to use camelCase when naming variables, too. I'll also use a SimilarThing when naming functions. But that's in C
I prefer underscore separated as it seems both faster to read and type.
Code:
void hello_world(){
printf("Hello, World");
}
int main(){
hello_world();
}
...
Code:
void HelloWorld(){
printf("Hello, World");
}
int main(){
HelloWorld();
}
First one is easier to type
 
Old 03-26-2010, 01:14 PM   #14
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
makyo, what do you use for formatting shell scripts?
 
Old 03-26-2010, 01:38 PM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by smeezekitty View Post
I prefer underscore separated as it seems both faster to read and type.
Code:
void hello_world(){
printf("Hello, World");
}
int main(){
hello_world();
}
...
Code:
void HelloWorld(){
printf("Hello, World");
}
int main(){
HelloWorld();
}
First one is easier to type
Indentation would make it even better
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
e.g., BSD style (Slackware) vs. SystemV style startup scripts haertig Slackware 5 01-03-2009 10:52 PM
Compiling kernel Debian style or Native style ? Raynus Debian 1 06-16-2008 06:56 AM
C++ coding style question re: random access iterators spursrule Programming 10 03-03-2008 09:21 PM
gnu style coding...Do u agree? alaios Programming 18 09-01-2005 07:28 AM
Indian Hill C Style and Coding Standands liguorir Programming 0 05-25-2004 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration