Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1TBS for long conditional expressions

Important note: this question is not about the superiority of a bracing style above another. I am currently switching styles in order to evaluate for myself which one I think works best in my situation, and I like Allman as much as 1TBS.

Users of 1TBS bracing style, how do you format long conditions in an if statement and the subsequent code?

if ((this_is_the_first_part_of_a_long_condition)
    && (the_second_part_is_shorter__wait_no_it_is_not)
    && (and_one_more_for_the_road)) {
    here_comes_the_block_code();
}

I feel that there must be a better way. My current way is to add an empty line before the first line of the code block. Allman does not look very good in this case either, although more readable in my opinion.

Another example with for loops:

for (int relevant_counter_variable_name = START_VALUE;
    intelligent_expression_that_may_include_the_counter_variable;
    relevant_counter_variable_update) {
    first_code_line_inside_the_block();
}

Not so nice...

KNF (8 spaces indent) would help here, but I would like to avoid that. I have a couple other options, but I'd like to hear if there is some kind of standard way.

like image 466
Gauthier Avatar asked Mar 29 '11 08:03

Gauthier


2 Answers

if (
    (this_is_the_first_part_of_a_long_condition)
    && (the_second_part_is_shorter__wait_no_it_is_not)
    && (and_one_more_for_the_road)
) {
    here_comes_the_block_code();
}
like image 106
Paul Crowley Avatar answered Oct 08 '22 16:10

Paul Crowley


I double-indent continued lines:

if ((this_is_the_first_part_of_a_long_condition)
        && (the_second_part_is_shorter__wait_no_it_is_not)
        && (and_one_more_for_the_road)) {
    here_comes_the_block_code();
}
like image 32
jhenninger Avatar answered Oct 08 '22 16:10

jhenninger