0

I'm writing some console application and for example I have different classes which I want to be able to call in my application for printing output.

// For example I have Vehicle class and method Park in it
public void Park()
{
        if (Parking.ParkedVehicles.Count < ParkingSettings.ParkingCapacity)
        {
            Parking.ParkedVehicles.Add(this);
            _isParked = true;
            Console.WriteLine("Your vehicle(" + Color + " " + Make + "," +
                              LicenseNumber + ") was parked successfully. Go be pedestrian somewhere else");
        }
        else
        {
            Console.WriteLine("Sorry. Unfortunately all parking slots are occupied, try again next time");
        }
}

And Parking class with PrintOccupanctStatus() method in it

    public static void PrintOccupancyStatus()
    {
        if (ParkedVehicles.Count < ParkingSettings.ParkingCapacity)
        {
            Console.WriteLine($"Currently there are {ParkingSettings.ParkingCapacity - ParkedVehicles.Count} spots left");
        }
        else
        {
            Console.WriteLine("Sorry, parking has no free spots left");
        }
    }

So these are two different independent classes and I'm calling them in my console app. Is it considered an "OKAY" approach or should I do something like ConsoleHelper to print results and make above methods return string instead?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ismv
  • 47
  • 1
  • 5
  • 1
    It's ok.... But looking at that function, it hurts me that it is returning void and operating on an internal member and writing to console to return data through side channels. If you instead structured it to return a Tuple then it would be far easier to unit test in isolation. If you also passed in ParkedVehicles.Count and ParkingSettings.ParkingCapacity, then it would be perfectly easy to unit test, and you could make it static – Brian White May 19 '19 at 15:52
  • Thanks, Brian. I will look into your advice, appreciate it – ismv May 19 '19 at 17:18

3 Answers3

0

Yes it is OK to invoke this method from different classes. Console is basically the ConsoleHelper class that you refer to. It is even thread safe, so you can call it from multiple threads.

mnistic
  • 10,866
  • 2
  • 19
  • 33
0

I see no problem in this implementation. Because the Console.WriteLine is a static method and if you want to create another method in another class, calling that method has no difference with this one

Ehsan Kiani
  • 3,050
  • 1
  • 17
  • 19
0

It is OK however I personally recommend to use a logging library like https://github.com/nlog/NLog/. You could for example write to the console and additionally into a log file and review the whole output in a text editor. You can make this library write which class and method is giving you the output, which can help you debug your application. You can also configure different levels like info, warning, error or trace.

Tony Stark
  • 2,318
  • 1
  • 22
  • 41
  • Thank you, Tony. However, I have some conditions to my application, that requires not using external libraries (it's some sort of assignment) – ismv May 19 '19 at 17:22