Resolved just need some assistance with class organisation

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
At the moment I have a lot of different functions in my main program. What I was thinking was to create another class called "ConsolePrompts" and transfer all the functions which require input from the user inside of there. Is this good or bad practice? each function does require prompt from the user and code wise they are slightly similar I.e. using do while loops/while loops
 
Solution
In general, you'll always want to keep the Single Responsibility Principle in mind. Every method should only have a single responsibility. Every class/struct should only have a single responsibility. After the SRP, then keep the other SOLID object oriented programming principles in mind.

Sometimes it pays off to move some methods into a utility or helper class which just holds a bunch of static methods. Sometimes, it doesn't really make sense because those methods are highly specialized and they actually should stay within a particular class but the class looks like it's getting to be pretty huge (more than 500 lines). When you are getting to that point, consider using the same class, but split up the class across multiple files...
In general, you'll always want to keep the Single Responsibility Principle in mind. Every method should only have a single responsibility. Every class/struct should only have a single responsibility. After the SRP, then keep the other SOLID object oriented programming principles in mind.

Sometimes it pays off to move some methods into a utility or helper class which just holds a bunch of static methods. Sometimes, it doesn't really make sense because those methods are highly specialized and they actually should stay within a particular class but the class looks like it's getting to be pretty huge (more than 500 lines). When you are getting to that point, consider using the same class, but split up the class across multiple files using partial classes.
 
Solution
Great answer. Haven fallen victim to this issue this week in one of my own apps. (We all do it - allow classes and methods to get to big.) Try to segregate all your logic into folder and files. Business logic, methods, functions, structures, static helpers etc... I would say for the sake of sanity, yes, create a Functions class and move all your functions to it, especially those that return a response. Try to keep your main program file free from clutter if possible.
 
When it's 3AM and I'm "in the zone" writing code, it's very easy to keep on writing code because it all makes sense to me at the moment. But after a few hours of sleep, and having the cold pizza for breakfast, I look at the code and go: "What was I thinking? I should have split those up when I was first writing it." This is why it's important to either get a code review, or better yet, if you can find a partner to code with, do some pair programming. From what I've seen in the past, having more than one perspective usually leads to better organized code.
 
I agree. I call it programming during the devils hours, because it always goes bad. lol

As you know, I don't sleep a lot, even after recently leaving my current job. But regardless, what I have learned from working late hours, is; It's very easy to get sucked into writing a method/class that breaks the SRP. Its even easier to fall into this trap, when C# is no longer your primary language, and you are writing in a variety of different languages all at the same time. :)
 
Back
Top Bottom