2013-05-10 60 views
2

我正在尝试创建一个函数getInput(prompt, number),其中prompt是将在用户输入前面打印的文本(例如>Choose a password:),而number是一个布尔值,用于指示输入是数字还是任何类型。避免编写代码两次,同时保持正确的代码方式?

这是我写的函数:

function getInput(prompt, number) 
    if number then 
     while not input do 
      io.write(prompt) 
      input = tonumber(io.read()) 
     end 
    else 
     io.write(prompt) 
     input = io.read() 
    end 
    return input 
end 

不过,我重复的代码相当很多。 我有io.write(prompt)写了两次,我也有input = io.read()两次与tonumber()围绕其中一个电话。 我基本上只是重复相同的事情两次,一次在while循环,一次不在。

这里有一个小的解决方法我做:

function getInput(prompt, number) 
    while not input do 
     io.write(prompt) 
     input = io.read() 
     if number then 
      input = tonumber(input) 
     end 
    end 
    return input 
end 

这个版本只有io.write()io.read()写一次,但它不是“正确”的代码。 即使不需要(当number为false时),我仍在使用while循环。 我也在做if number检查while循环的每一轮(当number为真)。

我应该去第一个代码,还是有一种方法来改善第二个代码,使其更“适当”?

+0

第二个版本在到达EOF时会永远循环。 – lhf 2013-05-10 16:16:01

+1

'input'是全球性的,它的值保留了以前的呼叫,所以这两个函数都不会要求用户在第二次呼叫时输入数据。 – 2013-05-10 16:22:15

+0

还要考虑'read(“* n”)'来读取一个数字。那么你的代码路径就不一样了。 – daurnimator 2013-05-10 17:00:09

回答

3

一般情况下,重复一个非常简单的单行代码类似io.write(prompt)的代码不会被视为“代码重复”。此外,重复检查相同的简单条件通常不会被认为是性能危险。两种方式都具有同等的可读性,所以任何一种都可以,这取决于您的偏好。

的一种可能的改进将被分裂功能在两个,并且丢弃number标志,这样的:

function getInput(prompt) 
    io.write(prompt) 
    return io.read() 
end 
function getNumericInput(prompt) 
    while not input do 
     io.write(prompt) 
     input = tonumber(io.read()) 
    end 
    return input 
end 

然而,这可能不是当输入的类型是在运行时决定在以下情况下接受的,并且必须通过变量来控制。

+0

那么我不会有这个问题,除了你只能写文本和/或数字,所以这将是足够的! :) – 2013-05-10 16:20:23

1
local function getInput(prompt, number) 
    io.write(prompt) 
    local input = (number and tonumber or assert)((assert(io.read(), 'EOF'))) 
    return (input and function() return input end or getInput)(prompt, number) 
end 
+0

这不是一个尾部调用,可能会溢出调用堆栈。 – lhf 2013-05-10 16:48:16

+0

@lhf - 修正后打个电话:-) – 2013-05-10 18:06:59

0

那么,我会说,第一种形式是非常清晰,易于阅读。即使你写了两次非常相似的语句,它也没什么问题。

我唯一的建议是将它分成没有布尔标志(即,getInput(提示)和getNumericInput(提示))的两个函数,或者将布尔值更改为类型并将逻辑捕捉合适的类型,以一个单独的方法:

function getInput(prompt, type) 
    io.write(prompt) 
     input = getTypedInput(type) 
    return input 
end 

function getTypedInput(type) 
    input(io.read()) 
    ...change to type here <I don't know lua syntax) 
    return input 
end 

后者可能是你有问题矫枉过正,除非你认为有你会比数字或非数字使用更多类型的机会。