Calculator Program improvements

I'm pretty new to programming and made a few programs so far. I recently made a Calculator program and I hope you guys could tell me what I could do to improve.

int firstNumber;
string sign;
int secondNumber;

Console.WriteLine("Input Number:");
Console.Clear();

Console.WriteLine("Input Sign:");
Console.Clear();

Console.WriteLine("Input Second Number:");
Console.Clear();

switch (sign)
{
case "+":
answer = firstNumber + secondNumber;
break;
case "-":
answer = firstNumber - secondNumber;
break;
case "*":
answer = firstNumber * secondNumber;
break;
case "/":
answer = firstNumber / secondNumber;
break;
default:
Console.WriteLine("Invalid Input");
break;
}

• I would split this into 2 functions. One which does the IO, console reads/writes. And another which does the actual calculation; primarily the current switch statement. This would separate the concerns, enabling many advantages like testing & reuse. Jan 6, 2021 at 4:56

In general

Whenever you try to implement an application try to use sentences to describe the desired behavior. After that ask yourself: "Can it be further refined?" If so, then write new sentences to detail your plan in a more fine-grained level.

Let me show you what I mean:

The view from 10 000 feet

The application can perform basic operations between two operands.

What can be further refined?

• What does it mean basic operation?
• What did we mean by two operands?

High-level view

The application can perform basic operations between two operands.

It allows addition, subtraction, multiplication and division.

User can provide two numbers and the desired operation.
The application will perform it on the user's behalf.

What can be further refined?

• How can an user provide numbers?

Fine-grained problem decomposition

The application can perform basic operations between two operands.

It allows addition, subtraction, multiplication and division.

User can provide two numbers and the desired operation.

The application receives an input from the user then it perform a preliminary check.

If it is valid then it goes to the next statement.
If it is invalid then it ask the user again to provide a valid input.

The application will perform it on the user's behalf.

Let's put together the skeleton

Now, we can translate each sentence into instructions.
This problem decomposition also helps us to well-organize our code:

///<summary>
/// The application can perform basic operations between two operands.
///</summary>
public static void Main()
{

}

///<summary>
/// User can provide two numbers and the desired operation.
///</summary>
private static void AskForInputs()
{

}

///<summary>
/// The application receives an input from the user then it perform a preliminary check.
/// <list type="bullet">
/// <item>
/// <description>If it is valid then it goes to the next statement.</description>
/// </item>
/// <item>
/// <description>If it is invalid then it ask the user again to provide a valid input.</description>
/// </item>
/// </list>
///</summary>
///</summary>
private static int AskForNumber()
{

}

///<summary>
/// The application receives an input from the user then it perform a preliminary check. <para />
/// It allows addition, subtraction, multiplication and division.
///</summary>
private static char AskForOperation()
{

}

///<summary>
/// The application will perform it on the users behalf.
///</summary>
private static void PerformRequestedOperation()
{

}


The implementation is bit naive, because:

• It does not handle those cases when the user provides malformed input
• For example: instead of 3 (s)he provides three
• It does not handle under- or overflow cases
• For example: int.MaxValue + 100
• It does not handle zero division case
• For example: 100 / 0
• It does not handle properly a division where the result is a floating number
• For example: 10 / 3
• etc.

In your switch statement you repeat the following two statement in each case:

Console.WriteLine(answer);


An alternative implementation could be:

switch (operation)
{
case "+":
answer = firstNumber + secondNumber;
break;
case "-":
answer = firstNumber - secondNumber;
break;
case "*":
answer = firstNumber * secondNumber;
break;
case "/":
answer = firstNumber / secondNumber;
break;
}



This can be easily enhanced to include all the above validations as well:

switch (operation)
{
case "+":
break;
case "-":
answer = PerformSubtraction(lhs, rhs);
break;
case "*":
answer = PerformMultiplication(lhs, rhs);
break;
case "/":
answer = PerformDivision(lhs, rhs);
break;
}


One suggestion: anticipate invalid inputs from user. I would recommend using int.TryParse in place of Convert.ToInt32().