ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.)
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
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.
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:
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!)
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
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).
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.
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!
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.