1

Below is the code for addition of two numbers. I ask for user input and validate the same in the function - GetValidateInput(). Code is working fine, but is it correct approach to ask for user input from user-defined function or we should do this in Main() and only validate the data in user-defined function (so that this function can be reused for other validation in some other places). What is the right approach?

class Solution
{
    static void Main(string[] args)
    {

        Console.WriteLine("Enter the First Number");
        int firstNumber = GetValidateInput();

        Console.WriteLine("Enter the Second Number");
        int secondNumber = GetValidateInput();

        int sum = SolveMeFirst(firstNumber, secondNumber);
        Console.WriteLine("The Sum is {0}",sum);

        Console.ReadLine();
    }

    static int SolveMeFirst(int firstNumber, int secondNumber)
    {
        return firstNumber + secondNumber;
    }


    static int GetValidateInput()
    {
        bool isValid = false;
        string number = null;
        int result;
        do
        {
            number = Console.ReadLine();
            if (int.TryParse(number, out result))
                isValid = true;
            else
                Console.WriteLine("Invalid Number, please re-enter");

        } while (!isValid);
        return result;
    }
}
John Papa
  • 7
  • 3
  • I'd separate the two. At some point you might want to add unit tests for your validation methods. If they're separate, like `ValidateInput(string input)` then you can test them and it doesn't matter where the input comes from. – Scott Hannen Dec 31 '18 at 20:48
  • I've typically seen two lines of thought here. First, simply separate functionality. They do different things, therefore should be different methods. This follows the textbook definition of what a method is. Second, the method is pretty small already and *sorta* does the same thing, so just leave it. This is what I'm going to dub the "in practice" approach, as I rarely, if ever see methods broken down to the "textbook" level, and TBH, I'm lucky if they're broken down this far in the first place. Just don't do this in main. – Broots Waymb Dec 31 '18 at 20:59
  • One other thing you might consider. Pass the prompt in as a string parameter to your GetInput method. You might also want to allow users to quit, or get help or other things from within that method. – Flydog57 Dec 31 '18 at 21:03
  • If you want to get _really_ fancy, take a look at my answer to: https://stackoverflow.com/questions/52431607/exit-console-app-at-any-time-during-any-input-c/52462939#52462939 – Flydog57 Dec 31 '18 at 21:10

1 Answers1

1

I typically try to keep the UI code separate from the business logic code, which means that, for the most part, I don't have calls to Console.ReadLine or Console.WriteLine in most methods.

Having a helper library that does input validation, however, is essential if you're doing a lot of user interaction through a Console. I have a library with methods similar to yours, only which take in the prompt to display to the user as an argument, so the method can be reused. The simplest version might look something like this:

public static int GetIntFromUser(string prompt)
{
    int input;

    do
    {
        Console.Write(prompt);
    } while (!int.TryParse(Console.ReadLine(), out input));

    return input;
}

Then you can call it like this:

int firstNumber = GetIntFromUser("Enter the First Number");
int secondNumber = GetIntFromUser("Enter the Second Number");

I have similar methods for different types, so I don't have to worry about data validation in my main code.

Rufus L
  • 36,127
  • 5
  • 30
  • 43