简体   繁体   English

一些编码惯例/标准实践问题

[英]A few coding convention/standard practice questions

I have this function that takes a string from main. 我有这个功能,从主要。 The string contains all of the valid characters that a user can input from some menu options. 该字符串包含用户可以从某些菜单选项输入的所有有效字符。 Function will put character input into a variable and be compared to each character of the string. 函数会将字符输入放入变量中,并与字符串的每个字符进行比较。 Compare input variable to the string characters until valid input is entered. 将输入变量与字符串字符进行比较,直到输入有效的输入为止。

My question is, what is the best way to implement this loop? 我的问题是,实现此循环的最佳方法是什么? I don't like using while (true) with a return in the middle because it looks like an infinite loop with an exception in the middle, which makes it slightly harder to read, but I'm not sure how else I can do what I want it to do. 我不喜欢在中间使用return while (true) ,因为它看起来像是一个无限循环,中间有一个异常,这使得读取起来有点困难,但是我不确定我还能怎么做我要它做。 What's the best practice for achieving my goal? 实现我的目标的最佳实践是什么? Thanks. 谢谢。

char getValidKey(string validKeys)
{
    char letter;

    while (true) {
        cout << "Operation ? ";
        cin >> letter;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        for (int i = 0; i < validKeys.length(); i++) {

            if (letter == validKeys[i])
                return letter;
        }

        cout << "Error. Invalid input.\n";
    }
}

Also, I have a switch statement with multiple returns. 另外,我有一个带有多个返回值的switch语句。 Is it more common/preferred to assign calculations to a variable and have one return at the end or is this way generally okay? 将计算分配给变量并在末尾有一个返回是更常见/更可取的吗?还是这样通常可以吗?

string opStr;

switch (myOperation) {
    case 1:
        opStr = "RIGHT";
        break;
    case 2:
        opStr = "LEFT";
        break;
    case 3:
        opStr = "CENTER_ONLY";
        break;
    case 4:
        opStr = "CENTER_MISSING";
        break;
    default:
        opStr = "Error. Invalid input.";
        break;
}

return opStr;

OR 要么

switch (myOperation) {
    case 1:
        return "RIGHT";
        break;
    case 2:
        return "LEFT";
        break;
    case 3:
        return "CENTER_ONLY";
        break;
    case 4:
        return "CENTER_MISSING";
        break;
    default:
        return "Error. Invalid input.";
        break;
}

For the first case, refactor your code in smaller self-contained functions, and it becomes clear to understand the logic of getValidKey even from a while(true) : 对于第一种情况,使用较小的自包含函数重构代码,即使从while(true) ,也很清楚了解getValidKey的逻辑:

char isKeyValid(char x, const string& validKeys)
{
    return validKeys.find(x) != string::npos;
}

char readCharFromCin()
{
    char letter;

    cout << "Operation ? ";
    cin >> letter;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return letter;
}

char getValidKey(const string& validKeys)
{
    while (true) 
    {
        const char key = readCharFromCin();
        if(isKeyValid(key, validKeys)) return letter;

        cout << "Error. Invalid input.\n";
    }
}

For the second case, avoid break and simply return from your switch . 对于第二种情况,请避免break ,只需从switch返回即可。 Make the function containing the switch only do one thing. 使包含switch的功能仅做一件事。

string switchOperation(int myOperation)
{
    switch (myOperation) 
    {
        case 1:  return "RIGHT";
        case 2:  return "LEFT";
        case 3:  return "CENTER_ONLY";
        case 4:  return "CENTER_MISSING";
    }

    return "Error. Invalid input.";
}

Also, try to maximize usage of const and pass string instances you're only reading by const& to avoid unnecessary copies. 另外,请尝试最大程度地使用const并传递仅由const&读取的string实例const&以避免不必要的复制。

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

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