简体   繁体   English

有没有更好的方法来重构此方法?

[英]Is there a better way to refactor this method?

The method should return false if the bool is true and the length of array is not equal to 27 or if the array value 25 is empty. 如果布尔值为true,并且数组的长度不等于27,或者数组值25为空,则该方法应返回false。

The method should also return false if the bool is false and the length of array is not equal to 28 or if the array value 26 is empty 如果布尔值为false且数组长度不等于28或数组值为26为空,则该方法还应返回false

  private static bool IsValid(string[] values, bool isFullFile)
        {
            if (isFullFile && (values.Length != 27 || values[24] == string.Empty))
            {
                return false;
            }

            if (!isFullFile && (values.Length != 28 || values[25] == string.Empty))
            {
                return false;
            }

            return true;
        }

This can be simplified enough to use an Expression body definition 可以足够简化以使用表达式主体定义

private static bool IsValid(string[] values, bool isFullFile)
    => isFullFile 
        ? (values.Length == 27 && values[24] != string.Empty) 
        : (values.Length == 28 && values[25] != string.Empty);

(Forgive me for answering this, because this really belongs in code review.) (请原谅我回答这个问题,因为这确实属于代码审查。)

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

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