如何简化程序并简化所有if语句?

4

我正在创建一个十六进制、十进制和二进制转换器,目前进展顺利。这是我在iPhone上的第二个项目,我是一个初学者。然而,我想知道如何简化我已经有的代码(一堆if语句)。我的代码如下:

if (entered is hex)
     if (binary button clicked)
         convert to binary
     if (decimal button clicked)
         convert to decimal
     else (hex button clicked)
         keep in hex and inform
else if (entered is binary)
     if (hex button clicked)
         convert to hex
     if (decimal button clicked)
         convert to decimal
     else (binary button clicked)
         keep in binary and inform user
else if (entered is decimal)
     if (hex button clicked)
         convert to binary
     if (binary button clicked)
         convert to hex
     else (decimal button clicked)
         keep in decimal and inform user    
else   
    give error if something else entered in 

我觉得这看起来很重复。所有这些都在一个类中,所有这些if语句非常相似,所以我想知道是否有什么我可以做的?

感谢您的时间。


"hex button clicked" 是什么意思? - user523234
你是如何确定输入的类型的?这是基于按钮点击还是使用哪个文本字段进行输入?还是其他什么? - rdelmar
你的代码已经足够易读。进一步将代码分解为函数并不是必须的,但可以提高可读性。 - Pushpak Narasimhan
1
你可能缺少了一些else语句。 - Hot Licks
@HotLicks 表示你每组的第二个 if 应该改成 else if - lnafziger
7个回答

4

我会始终以相同的格式(让我们以十六进制为例)在内部存储输入的值。然后,您可以使用以下内容,这样更加简洁:

// Convert the user entered value to hex
if (enteredValue is hex)
    internalHexValue = enteredValue
else if (entered is binary)
    internalHexValue = convert enteredValue (binary) to hex
else if (entered is decimal)
    internalHexValue = convert enteredValue (decimal) to hex
else
    error and return

// Now, you have far less repetition because you only have to convert from hex:
if (binary button clicked)
    convertedValue = convert internalHexValue to binary
else if (decimal button clicked)
    convertedValue = convert internalHexValue to decimal
else (hex button clicked)
    convertedValue = internalHexValue

// Lastly, see if they selected the same format for input and output:
if (enteredValue == convertedValue)
    inform user

为了使上面的示例更易于阅读,您还可以将其分解为多个方法,像这样编写(为清晰起见已删除错误检查):

internalHexValue = [self convertEnteredValueToHex:enteredValue];
convertedValue   = [self convertHexValueToUserSelectedFormat:internalHexValue];
if (enteredValue == convertedValue)
    inform user

我也会将所有“将XXX转换为XXX”的行都作为您类中不同的方法来实现。

我会把 enteredType == targetType 的检查放在前面,但如果将使用共同的内部表单,则其他方面都可以这样做。将代码分解几乎总是朝着正确方向迈出的一步。 - Hot Licks
@HotLicks:我通常也会先检查这个,但在这种情况下,首先进行检查需要编写的代码几乎与计算值所需的代码一样多,因此这样做更快且更易于阅读... - lnafziger
检查指定的输入和输出类型是否相同应该几乎不需要成本。 - Hot Licks
可能吧。不过从伪代码中很难判断,而且他使用了“输入为十六进制”和“二进制按钮被点击”等术语,我认为他在使用不同的方法来确定它们,这将需要另一系列的if/else语句。不过我同意你的看法,在这种情况下,我认为这样做可能会简单一些。 - lnafziger
必须有一种方法可以单独指定输入格式 - 无法通过检查数字来获取它。 10101 是二进制,十进制还是十六进制? - Hot Licks
@HotLicks:虽然这是正确的,但很可能不像问“它们是否相同”那么简单,而可能需要多个if/else语句。就像我之前说的,我无法从伪代码中判断,所以我制定了一种通用的方法。希望如果其余的代码被正确设计,将会有可以以不同/更好的方式完成的事情。无论如何,这确实有效,并且类似于他最初的逻辑。感谢您的评论! - lnafziger

1
使用 switch,它非常顺畅,像下面这样做。
switch (entered )
{
case hex:
     if (binary button clicked)
         convert to binary
     else if (decimal button clicked)
         convert to decimal
     else (hex button clicked)
         keep in hex and inform
break;

case binary:

     if (hex button clicked)
         convert to hex
     else if (decimal button clicked)
         convert to decimal
     else (binary button clicked)
         keep in binary and inform user
break;

case  decimal:

     if (hex button clicked)
         convert to binary
     else if (binary button clicked)
         convert to hex
     else (decimal button clicked)
         keep in decimal and inform user  
break;  

default:

    give error if something else entered in 
}

1
那样并没有任何改进! - user149341
1
更不用说十进制/十六进制等可能无法在switch语句中使用了... - lnafziger
@lnafziger -- 十进制/十六进制等指示符号肯定可以转换为简单的整数(尽管转换可能很复杂)。而且,您可以将输入和输出类型都转换为整数,移位一个,然后添加,以将整个内容缩减为一个简单的开关,而无需内部if语句。 - Hot Licks
@HotLicks - 是的,你可以让几乎任何东西工作。但到了这个地步,它真的比他开始使用的更好吗? - lnafziger

1
将其分解为几个方法,以下内容相当概念化,不涉及不必要的重复或括号缺失:
    if (entered is hex)
        [self isHex];
    else if (entered is binary)
        [self isBinary];
    else if (entered is decimal)
        [self isDecimal];
        keep in decimal and inform user
    else
        give error if something else entered in
    return 0;
}

- (void)isHex {
    if (binary button clicked)
        convert to binary
    else if (decimal button clicked)
        convert to decimal
    else (hex button clicked)
        keep in hex and inform
}

- (void)isBinary {
    if (hex button clicked)
        convert to hex
    else if (decimal button clicked)
        convert to decimal
    else (binary button clicked)
        keep in binary and inform user
}

- (void)isDecimal {
    if (hex button clicked)
        convert to binary
    else if (binary button clicked)
        convert to hex
    else (decimal button clicked)
        keep in decimal and inform user
}

1

备选方案(不一定是我的首选):

int inputFmt = <input format reduced to integer 0..2>
int outputFmt = <output format reduced to integer 0..2>

int switchValue = (inputFmt * 4) + outputFmt;

switch (switchValue) {
    case BinaryFmtConst * 4 + BinaryFmtConst:
        <convert binary -> binary>
        break;
    case BinaryFmtConst * 4 + DecimalFmtConst:
        <convert binary -> decimal>
        break;
. . .
    case DecimalFmtConst * 4 + BinaryFmtConst:
        <convert decimal -> binary>
        break;
. . .
    case HexFmtConst * 4 + HexFmtConst:
        <convert hex -> hex>
        break;
    default:
        <error message>
}

1

你提到的if语句并不相同。

举个例子,当按下二进制按钮时,为了将数字转换为二进制,你需要两个不同的函数。

  1. 十六进制转二进制(输入为十六进制)
  2. 十进制转二进制(输入为十进制)

因此,实际上你正在调用不同的函数。


1
正如@apurv所暗示的那样,您的决策是独特的(输入+点击),您真的无法简化它或压缩它(就好像您有某种重复模式一样)。您所能做的最好的事情就是尽可能地使其可读,并且您现在的内容已经很好了。这很容易理解。这是那些任何试图“简化”或使其更优雅的情况之一,这可能会使其不必要地复杂且难以阅读。

0

为什么不使用switch语句以获得更好的可读性?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接