Brackets coding style in Qlang?

What is common best practice among Qlang coders where to place the curly brackets at the start and end of a function and loop / if statements?

myfunc()
{
...
} 
or 
myfunc(){
...
}

Highly debated by all Qlang practitioners. My preferred is:
myfunc ()
{

}

If you develop by yourself, anything goes of couse. If you develop in a team, as long as everyone use the same style it does not matter. Readable, maintainable and consistent code is the way to go.

3 Likes

This is a subject that’s near and dear to me, not because there’s a single correct answer or even because it’s terribly important (it’s definitely not unimportant either), but rather because it’s so easy to get wrong and simple to get right.

As kim stated, what’s most important is not what style you pick, but that you’re consistent within the team. There’s almost as many styles as there are programmers, who differentiate themselves by perhaps using one braces style for functions, one for if-statements, one for class declarations, etc. And this is all well and good, but in order have a coherent and professional product ‘under the hood’, having a coherent style is the only way; anything else is silly.

I’m personally ambivalent when it comes to adding new lines for braces, there’s readability cases to be made for both styles. For solo projects I tend to prefer new lines for functions and classes, but not for if-statements and for-loops since that can feel a bit verbose and expansive.
Additionally, there are more style choices to be made (they are endless!):

Space or no space after ‘if’ and ‘for’, ie
if (true)
or
if(true)

Space or no space after closing function parameter parenthesis, ie
void main() {
}
or
void main(){
}

There are many such Hamletian questions!

Another thing to note is that in qlang you can choose to omit braces for single line statements, eg

if (true)
    do_something();
else
    do_something_else();

I personally use this frequently, but it can however get quite confusing if you nest for/if-statements. In the example below it is not immediately clear whether the return statement will be reached because it’s at face value ambiguous which if-statement the ‘else’ corresponds to. In the example I’ve guided you with indentation, or have I…

if (true)
    if (false)
        do_something();
else
    return;

It’s worth noting also that braces cannot be omitted for try/catch clauses, even if they are one-liners.

try {
    stupid_question();
} catch {
    stupid_answer();
}
2 Likes

I personally prefer the original Kernighan & Richie style where the opening braces of function-bodies go on their own line, but the opening braces of compound statements go on the same line:

int main(int argc, char **argv)
{
    if (argc != 3)
        return 1;
    else {
        do_work(argv[1], argv[2]);

        return 0;
    }
}

No unnecessary braces for single statements except for do-while loops, or in some cases if-else where the statement would otherwise become “front-heavy”.

For class definitions I use the same brace-style as for function definitions:

class A
{
public:
    A(integer);
    A(number);
};

For try-catch, the catch is deemed sufficiently important to merit its own line (Stroustrup-style?):

void f()
{
    try {
        g();
        h();
    }
    catch {
        handle_error(err);
    }
}
5 Likes

Agree with Kalle. The classic Kernighan style is the most readable one.

Putting the opening brace of a function on a line by itself makes it much easier to spot the start of a function.

I disagree with this one

if (true)  
    if (false)
        do_something();
else
    return;

I prefer

if (true) {
    if (false)
        do_something();
}
else
    return;

I see the inner if-statement as a compound statement, and thus should be surrounded by brackets