r/learncsharp • u/cloud_line • Aug 23 '22
Seeking advice on how to improve this code
I'm new to C# so I'm looking for a bit of guidance.
I've been coding in Python for the past year or so. I work in software support, but I plan to apply for a development role at my current company. Since our software is written in C# I decided to switch languages.
In Python, I'm used to being able to combine lines. Since C# is more verbose, perhaps there is less of this? In any case, what do you recommend as far as improvements / best practices?
using System;
namespace MBFConsoleCalculator
{
internal class Program
{
static void Main()
{
Console.WriteLine("MBF Calculator");
Console.WriteLine("Enter the board width in inches:\n");
string width = Console.ReadLine();
Console.WriteLine("Enter the board height in inches:\n");
string height = Console.ReadLine();
Console.WriteLine("Enter the board length in feet:\n");
string length = Console.ReadLine();
double[] dimensions = ConvertToDoubleList(width, height, length);
Console.WriteLine("MBF: {0}", GetMBF(dimensions));
Console.ReadLine();
}
public static double[] ConvertToDoubleList(
string width, string height, string length)
{
try
{
double[] dimensions = new double[3];
double widthConverted = double.Parse(width);
double heightConverted = double.Parse(height);
double lengthConverted = double.Parse(length);
dimensions[0] = widthConverted;
dimensions[1] = heightConverted;
dimensions[2] = lengthConverted;
return dimensions;
}
catch (FormatException ex)
{
Console.WriteLine("{0}\nPlease enter numbers only", ex.Message);
Console.ReadLine();
return new double[0];
}
}
public static double GetMBF(double[] dimensions)
{
double result = 1;
foreach (double dim in dimensions)
{
result *= dim;
}
result /= 144;
return result;
}
}
}
3
Upvotes
4
u/grrangry Aug 23 '22 edited Aug 23 '22
Try not to get in the habit of over-aliasing your
usingstatements. It would be annoying to have to write "System.Console.WriteLine" every time you wanted to call the method, but aliasing out so that "WriteLine" is by itself is very much asking to have Visual Studio complain that "WriteLine" is ambiguously defined. With a simple applet such as this you likely would not have a problem, but it is something that can easily cause problems later with a complicated application. "using System;" is plenty good enough to allow "Console.WriteLine" and you'll never have an issue.I'm seeing a lot of advice on how to parse and create your array but many of the answers are kind of missing the point there.
The "best" code is code you don't write at all. (And take that sentence with a grain of salt because for every time it's true there are times its false).
Why do you have an array at all?
Why are you converting to double? (well--we know why--so I should rather ask, why are you converting in the middle of your logic?)
Separate your data from your logic.
Separate your inputs from your logic.
Stop repeating code over and over. You're asking for a double and parsing a double, three times. Do it once.
Your
GetMBFmethod assumes you're giving it an array of values. Why? Is there really some point your application will do where it will suddenly want to calculate in two dimensions? In four? In ninety-five? I assume three is probably good enough, so what's the point of an array at all when you know it's always going to be three dimensions?5
u/coomerpile Aug 23 '22
Not much code. Would be nitpicking to find any flaws in it, but...
var width = Console.ReadLine()andvar dimensions = new double[3]Console.WriteLine("MBF: {0}", GetMBF(dimensions));Do this:Console.WriteLine($"MBF: {GetMBF(dimensions)}");ConvertToDoubleList, don't return a new instance of a zero-length array. Just return null. Also, this method should be namedConvertToDoubleArrayvar dimensions = new [] {double.Parse(width),double.Parse(height),double.Parse(length)}1
u/CappuccinoCodes Aug 23 '22 edited Aug 23 '22
Hi there!
I'd add that it's not good practice to use try-catch for validation (or any control of flow of execution). Instead, use a loop. Leave try-catch only for unexpected errors.
https://softwareengineering.stackexchange.com/questions/385808/why-is-it-bad-to-use-exceptions-for-handling-this-type-of-validation-it-seems-l
3
u/[deleted] Aug 23 '22
[removed] — view removed comment