长条件表达式为 1TBS

发布于 2024-10-27 11:38:33 字数 810 浏览 1 评论 0原文

重要提示:这个问题并不是关于某种支撑样式相对于另一种支撑样式的优越性。我目前正在转换风格,以便自己评估哪种风格最适合我的情况,我喜欢 Allman 和 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();
}

我觉得一定有更好的办法。我当前的方法是在代码块的第一行之前添加一个空行。奥尔曼在这种情况下看起来也不是很好,尽管在我看来更具可读性。

另一个使用 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 个空格缩进)在这里会有所帮助,但我想避免这种情况。我还有其他几个选择,但我想听听是否有某种标准方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

送君千里 2024-11-03 11:38:33
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();
}
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();
}
一张白纸 2024-11-03 11:38:33

我双缩进连续行:

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 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();
}
烟─花易冷 2024-11-03 11:38:33

为了绝对的清晰度和可读性,我只是浪费了一些变量:

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 的所有荣耀。没有风格混合。没有丑陋。 Indent(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);
}

I'd simply waste a few variables for absolute clarity and readability:

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

There! 1TBS in all its glory. No style mixing. No ugliness. Indent(1) can deal with it without /* *INDENT-OFF* */ cheating.

You could even give the conditions meaningful names, such as

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);
}
轮廓§ 2024-11-03 11:38:33

混合搭配


我确实同意混合风格经常被人皱眉。
但是,我敢说,在可能的情况下,可以为了可读性而改变规则

如果样式严格强制执行,(公司编码政策)
我通常这样做:

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

只需对所有条件使用一级缩进,
以及大括号内代码的另一个附加级别。
这是尽可能可读的,不会冒犯任何纯粹主义者。

Mix-n-match


I do agree that mixing styles is often frowned upon.
But, i dare say that where possible, the rule can be bent for readability.

In cases where the style is strictly enforced, (company coding-policies)
I usually do this:

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

Just use one-level of indents for all the conditions,
and another additional level for the code inside the braces.
This is as readable as it gets, without offending any purists.

好听的两个字的网名 2024-11-03 11:38:33

每个级别使用单个空格缩进,无论需要与否,每个条件都使用括号。

对于复杂的情况,奥尔曼式括号可以很好地工作。

一般方法适用于无法容纳在一行中的代码的延续,或函数参数列表。

每个闭合元件与打开元件在同一水平处缩进,因此出现“));”对于“Trace.WriteLine(String.Format(”)和独立的“;”对于“return”

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

Single space indenting for each level, use parentheses for each condition whether needed or not.

For complicated conditions Allman-style parentheses can work well.

General approach works for continuation of code that won't fit on one line, or for lists of function arguments.

Each closing element is indented at same level as the opening element, hence the "));" for "Trace.WriteLine(String.Format(" and the freestanding ";" for "return".

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
       ))
      ;
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文