Reputation: 5
I'm making mini calculator.
My code:
public class Calc
{
public double a = 1;
public double b = 1;
public double plus()
{
return a+b;
}
public double minus()
{
return a-b;
}
}
public void Main()
{
Calc sq = new Calc();
Console.WriteLine("\na+b:");
Console.WriteLine(sq.plus());
Console.WriteLine("\na-b:");
Console.WriteLine(sq.minus());
}
My problems:
I want to show result from function without Console.WriteLine, what I should change in my code?
Is this code OOP? I want to improve my code, but I don't know how to do. Should I use variables like this?
Console.WriteLine(sq.plus(a, b));
Upvotes: 0
Views: 68
Reputation: 43264
You are writing functional code, so OOP is not a good fit here. Your plus
and minus
methods are deterministic, so turn them into static, thread safe functions.
You should not put the WriteLine
calls into the Calc
class as you are then mixing concerns, rather than separating them, as the class then becomes responsible for both calculating and displaying the results of the calculation. This makes testing harder and thus your code becomes less maintainable. So I'd rewrite it as:
public static class Calc
{
public static double Plus(double a, double b)
{
return a+b;
}
public static double Minus(double a, double b)
{
return a-b;
}
}
public void Main()
{
Console.WriteLine("\na+b:");
Console.WriteLine(Calc.Plus(1,1));
Console.WriteLine("\na-b:");
Console.WriteLine(Calc.Minus(1,1));
}
Upvotes: 1