Which design is preferable?

Found a couple of solutions to this task:

1. To console the values of three integers
2. To choose the greatest of three numbers using if
3. Bring to the console
4. Compare parity with Switch
5. To check the size < 100 using quick test

I'm sorry that as many as 5 pieces of code, but I need to figure out how to arrange the code and to disassemble the code to understand everything written there, although the majority of I understand.

1)
Console.WriteLine("Enter the first number");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter third number");
int num3 = Convert.ToInt32(Console.ReadLine());
int result = 0;
if (num1 > num2 && num1 > num3)
{
 result = num1;
 Console.WriteLine($"the Largest number {result} ");
}
else if (num2 > num1 && num2 > num3)
{
 result = num2;
 Console.WriteLine($"the Largest number {num2} ");
}
else
{
 result = num3;
 Console.WriteLine($"the Largest number of {num3} ");
}
switch (result % 2)
{
 case 0:
 Console.WriteLine("Even number");
break;
default:
 Console.WriteLine("Not even number");
break;
}
string FinalResult = result < 100 ? $"The number {result} is less than 100" : $"the Number {result} greater than 100";
Console.WriteLine("Result:" + FinalResult);

2)
Console.WriteLine("Enter three numbers:");
int num1 =
Convert.ToInt32(Console.ReadLine());
int num2 =
Convert.ToInt32(Console.ReadLine());
int num3 =
Convert.ToInt32(Console.ReadLine());
int result = 0;
MuchSmaller string = " ";

if (!(num1 < num2) && (!(num1 < num3)))
{
 Console.WriteLine($"the Largest {num1}");
 result = num1;
}
else
{
 if (!(num2 < num1) && (!(num2 < num3)))
{
 Console.WriteLine($"the Largest {num2}");
 result = num2;
}
else
{
 if (!(num3 < num1) && (!(num3 < num2)))
{
 Console.WriteLine($"the Largest {num3}");
 result = num3;
}
}
}
switch (result % 2)
{
 case 0:
 Console.WriteLine($"Number is {result} is even");
break;
default:
 Console.WriteLine($"Number is {result} is not odd");
break;
}

MuchSmaller = result < 100 ? "less" : "more";
Console.WriteLine($"Number is {result} {MuchSmaller} than 100");


3)
int a, b, c, max;
Console.WriteLine("Enter the first number:");
while (!int.TryParse(Console.ReadLine(), out a)) // Check that it has introduced a number
{
 Console.WriteLine("incorrect input! Enter an integer");
}
Console.WriteLine("Enter the second number:");
while (!int.TryParse(Console.ReadLine(), out b))
{
 Console.WriteLine("incorrect input! Enter an integer");
}
Console.WriteLine("Enter the third number:");
while (!int.TryParse(Console.ReadLine(), out c))
{
 Console.WriteLine("incorrect input! Enter an integer");
}
Console.WriteLine("---------------------------");
Console.WriteLine("Here are your numbers: {0}, {1}, {2}", a, b, c);
Console.WriteLine("---------------------------");
Console.WriteLine("Now looking for the greatest of three numbers");
Console.WriteLine("---------------------------");

if (a > b && a > c)
{
 Console.WriteLine("The biggest number is {0}", a);
 max = a;
}
else if (b > a && a > c)
{
 Console.WriteLine("The biggest number is {0}", b);
 max = b;
}
else
{
 Console.WriteLine("The biggest number is {0}", c);
 max = c;
}

Console.WriteLine("---------------------------");
Console.WriteLine("Let's check on the parity of");
Console.WriteLine("---------------------------");

switch (max % 2)
{
 case 0:
 Console.WriteLine("Number is even");
break;
default:
 Console.WriteLine("Number is odd");
break;
}
string Result = max < 100 ? $"The number is {max} is less than 100" : $"Number is {max} is greater than 100";
Console.WriteLine("Result:" + Result);

4)
Console.WriteLine("Enter 3 numbers: ");
int q = Convert.ToInt32(Console.ReadLine());
int w = Convert.ToInt32(Console.ReadLine());
int e = Convert.ToInt32(Console.ReadLine());

if (q > e && q > w)
{
 Console.WriteLine(q + "biggest number");
 switch (q % 2 == 0)
{
 case (true):
 Console.WriteLine(q + "is even number");
break;

 case (false):
 Console.WriteLine(q + "is odd number");
break;

}
 Console.WriteLine(q < 100 ? "Less Than 100" : "Greater Than 100");
};
if (w > e && w > q)
{
 Console.WriteLine(w + "biggest number");
 switch (w % 2 == 0)
{
 case (true):
 Console.WriteLine(w + "is even number");
break;

 case (false):
 Console.WriteLine(w + "is odd number");
break;
}
 Console.WriteLine(w < 100 ? "Less Than 100" : "Greater Than 100");
};
if (e > q && e > w)
{
 Console.WriteLine(e + "biggest number");

 switch (e % 2 == 0)
{
 case (true):
 Console.WriteLine(e + "an even number");
break;

 case (false):
 Console.WriteLine(e + "is odd number");
break;

}
 Console.WriteLine(e < 100 ? "Less Than 100" : "Greater Than 100");
};

5)
Console.WriteLine("Enter the first number: ");
int inputOne = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the second number: ");
int inputTwo = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the third number: ");
inputThree int = int.Parse(Console.ReadLine());

//Looking for maximum value using if statement
if (inputOne > inputTwo && inputOne > inputThree)
{
 Console.WriteLine("the First number was greater, and its value =" + inputOne);
}
else if (inputTwo > inputOne && inputTwo > inputThree)
{
 Console.WriteLine("Second number was larger and its value =" + inputTwo);
}
else if (inputThree > inputOne && inputThree > inputTwo)
{
 Console.WriteLine("Third number was larger and its value =" + inputThree);
}
else
{
 Console.WriteLine("cannot find maximum value");
}

Console.WriteLine("\pLog the numbers: ");
//Check greater than or less than the number
if (inputOne > 100)
{
 Console.WriteLine("First number is greater than one hundred");
}
else
{
 Console.WriteLine("First number is less than one hundred");
}

if (inputTwo > 100)
{
 Console.WriteLine("Second number is greater than one hundred");
}
else
{
 Console.WriteLine("Second number is less than one hundred");
}

if (inputThree > 100)
{
 Console.WriteLine("Third number is greater than one hundred");
}
else
{
 Console.WriteLine("Third number is less than one hundred");
}

//Check parity
Console.WriteLine("\pLog the parity of numbers: ");
switch (inputOne % 2)
{
 case 0:
 Console.WriteLine("First number is divisible by 2");
break;

default:
 Console.WriteLine("First number is not divisible by two");
break;
}
switch (inputTwo % 2)
{
 case 0:
 Console.WriteLine("Second number is divisible by 2");
break;

default:
 Console.WriteLine("Second number is not divisible by two");
break;
}
switch (inputThree % 2)
{
 case 0:
 Console.WriteLine("Third number is divisible by 2");
break;

default:
 Console.WriteLine("Third number is not divisible by two");
break;
}
April 3rd 20 at 17:33
2 answers
April 3rd 20 at 17:35
Solution
General comments (for all pieces):

There is no separation of functions (problem: code duplication, complicating perception)

Take out each piece into a convenient method, and call. For example:
* Getting a number from the console
* Search for the minimum of three numbers
* Check for evenness

You will be much clearer what executes the code - your opinion will cover several challenges and do not be distracted by the specific details of implementation.

Common problems are now over and from the top down:

1 piece:

1. [ Critical ] No check for correctness of input. The user can accidentally enter a letter and the method Convert.ToInt32 will throw an exception. It is quite unpleasant.

2. Violation of naming conventions for variable FinalResult

3. Optional initialization variable FinalResult for the announcement.

4. [ Subjective ] Problems with formatting: the ternary operator takes up too much space for one row

5. [ Can be seen as quibbling ] copy-paste approach. The message we can draw from if-s - it is the same for all cases.

2 piece:

1. [ Critical ] Similar to the previous

2. Violation of the agreement about naming for the variable MuchSmaller

3. Ad and superfluous variable initialization MuchSmaller much ahead of actual use.

4. Too complicated conditional expression.

5. Similarly, section 5 of the previous example.

3 piece:

Here, hurray!, there is an underlying problem which is found in all the other examples.

1. Violation of naming conventions for the variable Result

2. A strange mix of English and Russian language in news reports

3. A strange mix of use for the internment of lines ($) and the old format.

4 piece:

1. [ Critical ] No check for correctness of input. The user can accidentally enter a letter and the method Convert.ToInt32 will throw an exception. It is quite unpleasant.

2. [ Significantly ] copy-paste approach to even parity.

3. Redundant braces in switch case

5 piece:

1. [ Critical ] No check for correctness of input. The user can accidentally enter a letter and the Parse method will throw an exception.

2. Problems with the name of the variables. inputOne

Total:

The 3rd version of the conditionally normal. Others worse.

Exercise:

Try to write your own version taking into account the comments listed above =)
April 3rd 20 at 17:37
Yes, all the rules except the second.
And this piece of strange:
Console.WriteLine("Enter 3 numbers: ");
 int q = Convert.ToInt32(Console.ReadLine());
 int w = Convert.ToInt32(Console.ReadLine());
 int e = Convert.ToInt32(Console.ReadLine());
@Florida_Nolan, second, or 2.1 ? - Kiara.Harber commented on April 3rd 20 at 17:40
@Kiara.Harber, just a second. Too indusky.
IMHO, of course.
And so all of the code is quite pilitan, methods of design - mass and then everyone wets as he wants. - Florida_Nolan commented on April 3rd 20 at 17:43
@Florida_Nolan, Thank You !) - Kiara.Harber commented on April 3rd 20 at 17:46
@Florida_Nolan, What better use for carrying out ? Parse(), TryParse() or Convert class. - Kiara.Harber commented on April 3rd 20 at 17:49
@Kiara.Harber, I usually TryParse() use, but it is quite a spike. It is necessary to write stable code. - Florida_Nolan commented on April 3rd 20 at 17:52

Find more questions by tags Code reviewC#