Switching to C# and curious if there's anything I could improve on in this very basic math script.

c#
class Program
{
static void Main()
{
MathClass.Print(2,5,"subtract");
}
}
class MathClass
{
public static void Print(int x, int y, string type)
{
switch (type.ToLower())
{
case "add":
Console.WriteLine($"Result of adding {x} and {y} is {Add(x,y)}");
break;
case "subtract":
Console.WriteLine($"Result of subtracting {x} from {y} is {Subtract(x,y)}");
break;
case "multiply":
Console.WriteLine($"Result of multiplying {x} and {y} is {Multiply(x,y)}");
break;
case "divide":
Console.WriteLine($"Result of dividing {x} and {y} is {Divide(x,y)}");
break;
default:
Console.WriteLine("No matching operation found.");
break;
}
}
private static int Add(int x, int y)
{
return x + y;
}

private static int Subtract(int x, int y)
{
return x - y;
}
private static int Multiply(int x, int y)
{
return x * y;
}
private static int Divide(int x, int y)
{
return x / y;
}

}
c#
class Program
{
static void Main()
{
MathClass.Print(2,5,"subtract");
}
}
class MathClass
{
public static void Print(int x, int y, string type)
{
switch (type.ToLower())
{
case "add":
Console.WriteLine($"Result of adding {x} and {y} is {Add(x,y)}");
break;
case "subtract":
Console.WriteLine($"Result of subtracting {x} from {y} is {Subtract(x,y)}");
break;
case "multiply":
Console.WriteLine($"Result of multiplying {x} and {y} is {Multiply(x,y)}");
break;
case "divide":
Console.WriteLine($"Result of dividing {x} and {y} is {Divide(x,y)}");
break;
default:
Console.WriteLine("No matching operation found.");
break;
}
}
private static int Add(int x, int y)
{
return x + y;
}

private static int Subtract(int x, int y)
{
return x - y;
}
private static int Multiply(int x, int y)
{
return x * y;
}
private static int Divide(int x, int y)
{
return x / y;
}

}
10 Replies
Davaaron
Davaaron2mo ago
Depends what you want to do... However, I would assign the results of the methods to variables. You could also create an Enum for the "CalculationMethods". What's your level of programming? You could take this to the next level ... I would consider the SOLID principles as well as other principles. That means I would try separate the responsibilities the best way I could.. try to put together what's common and only specify what's different, finding a good "mechanism" to be opened for alterations ... for example
using System;

class Program
{
static void Main()
{
MathOperation.Execute(new Addition(2, 5));
MathOperation.Execute(new Subtraction(2, 5));
MathOperation.Execute(new Multiplication(2, 5));
MathOperation.Execute(new Division(2, 5));
}
}

abstract class Operation
{
public abstract string OperatorSymbol { get; }
public abstract int Perform(int x, int y);
}

class Addition : Operation
{
public override string OperatorSymbol => "+";

public override int Perform(int x, int y)
{
return x + y;
}
}

class Subtraction : Operation
{
public override string OperatorSymbol => "-";

public override int Perform(int x, int y)
{
return x - y;
}
}

class Multiplication : Operation
{
public override string OperatorSymbol => "*";

public override int Perform(int x, int y)
{
return x * y;
}
}

class Division : Operation
{
public override string OperatorSymbol => "/";

public override int Perform(int x, int y)
{
if (y == 0)
{
throw new ArgumentException("Division by zero is not allowed.");
}

return x / y;
}
}

class MathOperation
{
public static void Execute(Operation operation)
{
int result = operation.Perform(operation.X, operation.Y);
Console.WriteLine($"Result of {operation.X} {operation.OperatorSymbol} {operation.Y} is {result}");
}
}
using System;

class Program
{
static void Main()
{
MathOperation.Execute(new Addition(2, 5));
MathOperation.Execute(new Subtraction(2, 5));
MathOperation.Execute(new Multiplication(2, 5));
MathOperation.Execute(new Division(2, 5));
}
}

abstract class Operation
{
public abstract string OperatorSymbol { get; }
public abstract int Perform(int x, int y);
}

class Addition : Operation
{
public override string OperatorSymbol => "+";

public override int Perform(int x, int y)
{
return x + y;
}
}

class Subtraction : Operation
{
public override string OperatorSymbol => "-";

public override int Perform(int x, int y)
{
return x - y;
}
}

class Multiplication : Operation
{
public override string OperatorSymbol => "*";

public override int Perform(int x, int y)
{
return x * y;
}
}

class Division : Operation
{
public override string OperatorSymbol => "/";

public override int Perform(int x, int y)
{
if (y == 0)
{
throw new ArgumentException("Division by zero is not allowed.");
}

return x / y;
}
}

class MathOperation
{
public static void Execute(Operation operation)
{
int result = operation.Perform(operation.X, operation.Y);
Console.WriteLine($"Result of {operation.X} {operation.OperatorSymbol} {operation.Y} is {result}");
}
}
boiled goose
boiled goose2mo ago
yeah you could improve this essentially ad infinitum
Angius
Angius2mo ago
Depends what you mean. Can more functionality be added? Sure. Can the code be shortened? Also yes. Could use a switch expression for example Expression-bodied methods Top-level statements And so on, and so forth
AlgorithMage Ω(n) Θ(n×p)
Thanks for your reply, I think what you've provided is what I was looking for, a direction to start with to improve my usage of C# and the various paradigms/design patterns. I have 3 years of experience writing in Python and have an undergrad in CS. In general I'd say I'm a novice/beginner nearing intermediate programmer. For the specific script in question I was hoping for general critique which you've provided nicely. Examples are more helpful than vague and absolute statements like this FWIW. I've provided additional context if that helps.
boiled goose
boiled goose2mo ago
"could this be improved?" "yes" that's kinda what i could tell you with the informations i had; as Z said it depends, do you want to keep the code simple? do you have any other constraint? do you want more features or more abstractions? you could add ui (whether textual or graphical), you could have history of the operations and undo/redo, you could have api, you could have more operation types, you can have translation, you could have expression parsing, and so on for example for sure 1st thing i would do is - removing static - using sealed - adding parameters checks
AlgorithMage Ω(n) Θ(n×p)
Thanks for your time I appreciate it. It will take a bit for me to consider everything everybody has said, it's difficult to chat between work and home life.
do you want to keep the code simple? do you have any other constaint?...
My apologies, I really did provide horrific context didn't I. The only goal of this exact "project" is to explore the different areas of C# that Python doesn't offer and felt that some more experienced programmers than myself would shed some light into what parts of the language they'd recommend. I don't intend to make use of anything I'm doing beyond pure enjoyment for what it's worth. My only prior programming experience is Python and some very basic C in university, but I am more interested in learning as a personal interest but will be working with lots of math and problems involving things like Bayes algorithm and the explore/exploit dilemma and other similar problems. To be specific on 3 points I'd choose: 1) What ways could the way I am writing script right now come to be problematic in the future? 2) How would someone in a more experienced situation approach the methods and classes and why would they do it that way? 3) What resources would be better than the Microsoft C# docs for reference if any? I'm off for the night, but I will read and reply as soon as I can.
boiled goose
boiled goose2mo ago
if you want to work with math and numbers then learning about the various interfaces implemented by int and other primitive types could be useful
3) What resources would be better than the Microsoft C# docs for reference if any?
for math? probably tanner in the allow-unsafe-blocks channel 😆 1) What ways could the way I am writing script right now come to be problematic in the future? it's a bit early for this, depends what the project should become/do
AlgorithMage Ω(n) Θ(n×p)
I'll look into all of your suggestions, and fair point for #1. Is there anything else you would add before I move on from this thread?
boiled goose
boiled goose2mo ago
i feel like stuff i would say at this point would be akin to premature optimization
AlgorithMage Ω(n) Θ(n×p)
I can appreciate that. Take care thanks again for your time.