简体   繁体   English

什么是用于格式化该语句的惯用C方式

[英]What would be the idiomatic C way to format this statement

I've come up with this hard to read if statement, and I can't figure out the best way I should format it's layout to make it more readable and do so in a C way. 我想出了这个难以理解的if语句,我无法弄清楚我应该如何格式化它的布局以使其更具可读性并以C方式执行。

if (((value == intMax) && (intMax != 0)) || // Deals with upper bound
    (value > (intMax/10)) ||
    ((value == (intMax/10)) && (digitAdjusted > digitLastIntMax)) ||
    ((value == intMin) && (intMin != 0)) || // Deals with lower bound
    (value < (intMin/10)) ||
    ((value == (intMin/10)) && (digitAdjusted < digitLastIntMin))) {

    // Some code
}

If this is an inappropriate use of SO, please let me know - I'll remove this question. 如果这是对SO的不当使用,请告诉我 - 我将删除此问题。

Actually, since && precedes || 实际上,因为&&先于|| , the inner parenthesis aren't necessary - you can remove them. ,内括号不是必需的 - 你可以删除它们。

Also: (intMin != 0) is the same as just intMax . 另外: (intMin != 0)intMax相同。

if ((value == intMax) && intMax || // Deals with upper bound
    (value > intMax/10) ||
    (value == intMax/10) && (digitAdjusted > digitLastIntMax) ||
    (value == intMin) && intMin || // Deals with lower bound
    (value < intMin/10) ||
    (value == intMin/10) && (digitAdjusted < digitLastIntMin)) {

    // Some code
}

So this is what I've come up with following all the advice that I've received. 所以这就是我按照我收到的所有建议提出的。

// Upper bound
maxReached              = (value == intMax) && (intMax != 0);
maxDivTenExceeded       = value > (intMax/10);
maxLastDigitExceeded    = (value == (intMax/10)) && (digitAdjusted > digitLastIntMax);
// Lower bound
minReached              = (value == intMin) && (intMin != 0);
minDivTenExceeded       = value < (intMin/10);
minLastDigitExceeded    = (value == (intMin/10)) && (digitAdjusted < digitLastIntMin);
// Stop conditions
stopMax                 = maxReached || maxDivTenExceeded || maxLastDigitExceeded;
stopMin                 = minReached || minDivTenExceeded || minLastDigitExceeded;


// Prevent integer overflow
if (stopMax || stopMin) {
    // Code
}

I would put each of the clauses separated by || 我会把每个子句用||分开 into functions that return a boolean value. 到返回布尔值的函数。

if ( test1(value, intMax) ||  // ((value == intMax) && (intMax != 0)) || // Deals with upper bound
     test2(value, intMax) ||  // (value > (intMax/10)) ||
     test3(value, intmax, digitAdjusted, digitLastIntMax) || // ((value == (intMax/10)) && (digitAdjusted > digitLastIntMax)) |
     test4(value, intMin) //     ((value == intMin) && (intMin != 0)) || // Deals with lower bound
     test5(value, intMin) || // (value < (intMin/10)) ||
     test6(value, intMin, digitAdjusted, digitLastIntMin) //((value == (intMin/10)) && (digitAdjusted < digitLastIntMin))
)
{
// Some code
}

If you want to make it more readable, use nested ifs and a good explanation in comments as to what each stage is doing. 如果你想让它更具可读性,可以在评论中使用嵌套的ifs和一个很好的解释,说明每个阶段在做什么。 There's not much practical benefit to doing it all on one line and it certainly does seem unreadable. 在一条线上完成这一切并没有太大的实际好处,而且它确实看起来似乎不可读。

Lining things up vertically helps a little bit, but the real question is, "What are the requirements that you're trying to implement?". 垂直排列有助于一点点,但真正的问题是,“你正在尝试实施哪些要求?”。 It sort of looks like your converting an input string to an integer while trying to avoid overflow, but the intMin checks make no sense in that case. 它看起来像是在尝试避免溢出时将输入字符串转换为整数,但在这种情况下intMin检查没有任何意义。 A little more context might help to sort this out. 多一点上下文可能有助于解决这个问题。

if (((value == intMax) && (intMax != 0)) || (value > (intMax/10)) || ((value == (intMax/10)) && (digitAdjusted > digitLastIntMax)) ||
    ((value == intMin) && (intMin != 0)) || (value < (intMin/10)) || ((value == (intMin/10)) && (digitAdjusted < digitLastIntMin)) )

Note that vertical alignment would have made the bitwise OR vs. logical OR errors immediately obvious. 请注意,垂直对齐会使得按位OR与逻辑OR错误立即显而易见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM