2011-03-29 63 views
8

重要注意事項:這個問題不是關於支撐風格高於另一個的優越性。我目前正在切換風格,以便爲我自己評估哪一個我認爲在我的情況下效果最好,並且我喜歡Allman和1TBS一樣多。1TBS長條件表達式

1TBS支撐樣式的用戶,如何在if語句和後續代碼中格式化長條件?

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(); 
} 

我覺得應該有更好的辦法。我目前的方法是在代碼塊的第一行之前添加一個空行。在這種情況下,Allman看起來也不太好,但我認爲更具可讀性。

又如與for循環:

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(); 
} 

不是很好......

KNF(8個空格縮進)將有助於在這裏,但我想避免這種情況。我還有其他一些選擇,但我想聽聽是否有某種標準方式。

+6

這正是我暫時切換到奧爾曼風格,將左大括號與右括號對準自己的線的情況下。有些人會混淆「混合風格」,但我認爲它使得代碼易讀。 (喬治奧威爾的清晰寫作指南的最後一條規則是「違反這些規則中的任何一條,而不是說一些完全野蠻的事情。」)你能說爲什麼你認爲「它看起來不太好」嗎? – librik 2011-03-29 08:52:52

+0

@librik:儘管塊(開頭大括號)的開頭並不是「if」的一部分,但我喜歡這個事實,即Allman將它放在關鍵字的第一個字符下。我的眼睛把它們放在一起,並且速度非常快。如果條件延伸到幾行,「if」和「{」看起來分開,我可以聽到他們哭泣。說真的,他們在同一個專欄看起來並不明顯。 – Gauthier 2011-03-29 08:58:35

+1

我和librik在這裏 - 讓代碼可讀性比機械地遵循風格更重要。您發現「規則」可能被視爲推薦的情況 - 遵循您的首選樣式,除非無效。我個人一直遵循Allman風格,並沒有遇到過這種情況。 :-) – 2011-03-29 09:13:54

回答

9
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(); 
} 
+1

這實際上是我傾向於這樣做的方式。 – Gauthier 2011-05-09 14:39:09

+0

現在我需要告訴'clang-format'符合:'( – Gauthier 2016-09-07 11:16:20

1

混合正賽


我不同意,混音風格是在經常皺眉。
但是,我敢說,在可能的情況下,規則可以爲了可讀性而彎曲。

如遇有風格嚴格執行,(公司編碼的策略)
我通常這樣做:

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(); 
} 

只需使用縮進一個級別的所有條件,
和大括號內代碼的另一個級別。
這是可讀的,不會冒犯任何純粹主義者。

9

我的雙縮進續行:

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(); 
} 
0

單個空格縮進每個級別,使用括號是否需要與否每個條件。

對於複雜條件,Allman式圓括號可以很好地工作。

一般方法適用於延續不適合一行的代碼或函數參數列表。

每個關閉元素與開始元素縮進相同的水平,因此「));」爲「Trace.WriteLine(String.Format(」和獨立「;」爲「返回」)

YMMV。

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) 
    ) || 
    (
    (this_is_the_first_part_yet_another) && 
    (
     (the_second_part_yet_another) || 
     (val < 22) 
    ) 
    ) 
    ) { 
     here_comes_the_block_code(); 

     int bits = 0 
     | O_DEF 
     | CONFIG_THIS 
     | CONFIG_THAT 
     ; 

     FILE *OUPT = fopen(
     "/tmp/oupt.txt", 
     "a+" 
    ); 

     Trace.WriteLine(String.Format(
     "format {0} 0x{1:x8}" 
     ,(eGenericDeviceFeatureEnum)val 
     ,val & 0x7ffffc00 
    )); 

     return 
     (CurrentWort != null) && 
     (CurrentWort.IsFeatureSupported(
     eGenericDeviceFeatureEnum.SupportsTriBromoCarcinogen 
     )) 
     ; 
    } 
2

我只是浪費了絕對的清晰度和可讀性幾個變量:

cond1 = this_is_the_first_part_of_a_long_condition; 
cond2 = the_second_part_is_shorter__wait_no_it_is_not; 
cond3 = and_one_more_for_the_road; 
if (cond1 && cond2 && cond3) { 
    here_comes_the_block_code(); 
} 

有! 1TBS的輝煌。沒有風格混合。沒有醜陋。縮進(1)可以在沒有/* *INDENT-OFF* */作弊的情況下處理它。

你甚至可以給條件有意義的名稱,如

guidance = this_is_the_first_part_of_a_long_condition; 
navigation = the_second_part_is_shorter__wait_no_it_is_not; 
surgeon = and_one_more_for_the_road; 
if (guidance && navigation && surgeon) { 
    capcom_we_are_go_for_powered_descent(); 
} else { 
    agc_alarm(1202); 
}