关于如何格式化长 if 语句的愚蠢问题

发布于 2024-11-09 15:20:33 字数 267 浏览 0 评论 0原文

对于占用多行的长 if 语句,您是否将 AND 或 OR 等条件放在新行上,如下所示:

               if (something
                   && something else)

或者像这样:

               if (something &&
                   something else)

On long if statements where they take up more than one line, do you put the conditions like AND or OR on a new line like this:

               if (something
                   && something else)

Or like this:

               if (something &&
                   something else)

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

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

发布评论

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

评论(6

梦言归人 2024-11-16 15:20:33

对于复杂的条件,请考虑将其提取到函数或变量中:

if (complexCondition(foo)) { ..

作为奖励,函数或变量的名称可用于传达条件的含义。这使您的代码更易于阅读。

For complex conditions, consider extracting it into a function or a variable:

if (complexCondition(foo)) { ..

As a bonus, the name of the function or variable can be used to communicate what the condition means. This makes your code easier to read.

雪化雨蝶 2024-11-16 15:20:33

我通常采用第二种方式,因为我可以排列语句。然而,在编写代码时,只要保持一致,任何一种方法都可以。

I typically do it the second way, since I can line up the statements. However, either way is fine when you're writing code, as long as you're consistent.

爱的十字路口 2024-11-16 15:20:33

我更喜欢第一首的演绎。我的理由是,出于任何测试目的通过剪切/粘贴/注释删除条件更容易。注释掉一行比从上面的行中删除 and 并注释掉一行要容易得多。当我在 SQL 中执行 where 子句时,这种情况比在任何其他给定语言中的 if 语句中发生的情况更多,但很相似。

I prefer a rendition of the first. My reasoning is that deleting a condition via cut/paste/comment for any testing purposes is easier. It's a lot easier to comment out a line than it is to delete the and from the line above and comment out a line. This is more when I'm doing where clauses in SQL than in an if statement in any other given language, but is similar.

段念尘 2024-11-16 15:20:33

鉴于我的建议,我首先会避免进行长时间的 if 测试。我宁愿做这样的事情:

bool fTest1 = A == B ;
bool fTest2 = C ;
bool fTest3 = f(1,2,3) ;
bool fSuccess = ( fTest1 | ftest2 ) & fTest3 ;
if ( fSuccess )
...

否则就是这样的事情:

if (  A == B
&& (  C == D
   || E == F
   )
&&    Z >  Y
) {
    ...
  }
else
  {
    ...
  }

YMMV,当然。

前者更容易调试、测试、记录等。

Given my druthers, I'd avoid long if tests in the first place. I'd rather do something like:

bool fTest1 = A == B ;
bool fTest2 = C ;
bool fTest3 = f(1,2,3) ;
bool fSuccess = ( fTest1 | ftest2 ) & fTest3 ;
if ( fSuccess )
...

Otherwise something like this:

if (  A == B
&& (  C == D
   || E == F
   )
&&    Z >  Y
) {
    ...
  }
else
  {
    ...
  }

YMMV, of course.

The former is far easier to debug, test, log, etc.

浪荡不羁 2024-11-16 15:20:33

我通常使用 IDE 格式化程序进行格式化,然后重新排列一下以使其看起来更漂亮。

I usually format using the IDE formatter and then rearrange a bit to make it look beautiful.

骑趴 2024-11-16 15:20:33

我在 VSC 工作,最近不仅成功地在嵌套 if 语句中编写了非常长的条件,而且使其可读。只需使用括号和新行即可。它应该自动缩进:

foreach ($panstwa as $key => $value) {
                            if (is_object($value)) {

                                if (
                                    (
                                        ($value->checkbox1 === true) && (is_string($value->panstwoZListy)) && ($value->panstwoZListy !== 'none') && ($value->panstwo === '') && ($value->panstwoZListy !== '')
                                    ) ||
                                    (
                                        (
                                            ($value->checkbox2 === true &&
                                                ($value->checkbox2_1 === true || $value->checkbox2_2 === true || $value->checkbox2_3 === true || $value->checkbox2_4 === true || $value->checkbox2_5 === true || $value->checkbox2_6 === true)
                                            ) ||
                                            ($value->checkbox3 === true &&
                                                ($value->checkbox3_1 === true || $value->checkbox3_2 === true)
                                            ) ||
                                            ($value->checkbox4 === true &&
                                                (
                                                    (
                                                        ($value->checkbox4_1 === true || $value->checkbox4_2 === true || $value->checkbox4_3 === true || $value->checkbox4_4 === true || $value->checkbox4_5 === true || $value->checkbox4_6 === true || $value->checkbox4_7 === true) && ($value->checkbox4_8 === false)
                                                    ) ||
                                                    (
                                                        ($value->checkbox4_1 === false && $value->checkbox4_2 === false && $value->checkbox4_3 === false && $value->checkbox4_4 === false && $value->checkbox4_5 === false && $value->checkbox4_6 === false && $value->checkbox4_7 === false) && ($value->checkbox4_8 === true) && (sprawdzRegexTextInput($value->prawnieUzasadnionyInteres)) && (is_object($value->dokumentacjaOceny) || is_string($value->dokumentacjaOceny))
                                                    )
                                                )
                                            )
                                        ) &&
                                        (is_string($value->panstwo)) && ($value->panstwoZListy === 'none') && ($value->panstwo !== '') && (sprawdzRegexTextInput($value->panstwo)
                                        )
                                    ) &&
                                    ((is_int($value->panstwoid) && is_numeric($value->panstwoid)) || (is_bool($value->panstwoid) && $value->panstwoid === false)) &&
                                    (is_bool($value->zmiana))
                                ) {
                                    echo "ok";
                                    //nie robię nic
                                } else {
                                    $flagaPanstwa = false;
                                }
                            } else {
                                $flagaPanstwa = false;
                            }
                        }

I'm working in VSC and recently managed to not only write, but make readable very long conditions in nested if statements. Just use brackets and new lines like this. It should make automatic indentations:

foreach ($panstwa as $key => $value) {
                            if (is_object($value)) {

                                if (
                                    (
                                        ($value->checkbox1 === true) && (is_string($value->panstwoZListy)) && ($value->panstwoZListy !== 'none') && ($value->panstwo === '') && ($value->panstwoZListy !== '')
                                    ) ||
                                    (
                                        (
                                            ($value->checkbox2 === true &&
                                                ($value->checkbox2_1 === true || $value->checkbox2_2 === true || $value->checkbox2_3 === true || $value->checkbox2_4 === true || $value->checkbox2_5 === true || $value->checkbox2_6 === true)
                                            ) ||
                                            ($value->checkbox3 === true &&
                                                ($value->checkbox3_1 === true || $value->checkbox3_2 === true)
                                            ) ||
                                            ($value->checkbox4 === true &&
                                                (
                                                    (
                                                        ($value->checkbox4_1 === true || $value->checkbox4_2 === true || $value->checkbox4_3 === true || $value->checkbox4_4 === true || $value->checkbox4_5 === true || $value->checkbox4_6 === true || $value->checkbox4_7 === true) && ($value->checkbox4_8 === false)
                                                    ) ||
                                                    (
                                                        ($value->checkbox4_1 === false && $value->checkbox4_2 === false && $value->checkbox4_3 === false && $value->checkbox4_4 === false && $value->checkbox4_5 === false && $value->checkbox4_6 === false && $value->checkbox4_7 === false) && ($value->checkbox4_8 === true) && (sprawdzRegexTextInput($value->prawnieUzasadnionyInteres)) && (is_object($value->dokumentacjaOceny) || is_string($value->dokumentacjaOceny))
                                                    )
                                                )
                                            )
                                        ) &&
                                        (is_string($value->panstwo)) && ($value->panstwoZListy === 'none') && ($value->panstwo !== '') && (sprawdzRegexTextInput($value->panstwo)
                                        )
                                    ) &&
                                    ((is_int($value->panstwoid) && is_numeric($value->panstwoid)) || (is_bool($value->panstwoid) && $value->panstwoid === false)) &&
                                    (is_bool($value->zmiana))
                                ) {
                                    echo "ok";
                                    //nie robię nic
                                } else {
                                    $flagaPanstwa = false;
                                }
                            } else {
                                $flagaPanstwa = false;
                            }
                        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文