A general question.
When designing a program and separating the different elements into separate classes; at what point should a task be relegated to another class as opposed to making it merely another method?
Any good info on program design?
A general question.
When designing a program and separating the different elements into separate classes; at what point should a task be relegated to another class as opposed to making it merely another method?
Any good info on program design?
Mmh, when the function seems not to fit with its class, then maybe you should create another class.
For exemple if you have a class which purpose is cookie business, and you need to implement a function that feed cats then maybe you should create another class dedicated to cat-business.
My exemple is kind of silly, but its the idea. Specialize your class. Each class should do one thing. Cookie-business (baking cookie, selling cookie, creating cookie-recipe) or cat businness (feed cat, play with cat, breed cat), not both.
On the contrary don't overdo it and create one class for each function you will make ^^'
Anyway, try to be as coherent as possible with your code. And trust your feelings :D
Yep, there's lots of useful information in the web. You shoud research on the Single Responsibility Principle (SPR).
The Single Responsibility Principle states that a class should have one and only one well defined responsibility. In resume, if the method you are about to add is needed for the class to perform its responsibility or purpose, it belongs to that class, otherwise, you should create a new class. The hard task here usually is to define that responsibility, but that comes with time and practice.
For instance, let's suppose I want to build a car, and I have a component Engine which is responsible for putting the vehicle in motion. My Engine class has a method accelerate(). After that, I discover that depending on the situation, my engine needs to perform in a different fashion, so I add a method changeGear(). Allright, now my Engine can put the vehicle in motion, and switch gears so it can perform better. Then I realise that, in some situations, perhaps I would like to be able to stop the vehicle, so I add a method decelerate() to my Engine class. Now, I have a class that is responsible for accelerating and decelerating the vehicle, which violates the SPR. What I should do is create a new class Brake with the method decelerate().
The example is very basic and has some flaws, we can have distinct views about what "putting the vehicle in motion" means, or what should be the Engine responsibility, but I think it ilustrates the concept.
Most of us gain experience by watching others or by doing it badly and getting burned.
For some reason I keep choosing option B -- getting burned by doing it badly.
There are mountains of information on the 'Net, but I've found great inspiration for class design in -- believe it or not -- programming resources for business applications. Their militant methods of DRY (Don't Repeat Yourself) and the hurtful and informative mantra "Keep It Simple, Stupid" (KISS) allowed me to focus on the thought process behind designing classes and interfaces without getting bogged down in game-related or engine focused information.
Indie games are what indie movies were in the early 90s -- half-baked, poorly executed wastes of time that will quickly fall out of fashion. Now go make Minecraft with wizards and watch the dozen or so remakes of Reservior Dogs.
"Keep It Simple, Stupid" (KISS)
Best mantra ever :D
Also, learning some design pattern could help you figure thing out and give you ideas. I recommend this very good book about design pattern : http://shop.oreilly.com/product/9780596007126.do
Even if it's explained with Java, the theory can be applied to any languages.
Single-Responsibility Principle.
Stated another way, functions, classes and modules should strive to this mantra: "Do very little, very well."
You get into a very philosophical question of "What is a single responsibility?" pretty quickly, and it depends on the level on which that element operates. For two numbers, adding and subtracting are each single responsibilities. For a string, a single responsibility might be concatenating two strings together, or finding the first occurrence of a character. For a database, a single responsibility might be inserting a row of data atomically.
A simple rule of thumb is this -- describe in some detail the essence of what the function, class, or module does in just one sentence. If you can do it without using a conjunction -- usually "and", but sometimes "or" -- you're probably golden. If you struggle to fit it into a sentence or can't do so without reaching for a conjunction, then it's a sign that it might do too much -- that it might have more than one responsibility. It doesn't *always* mean that, strictly speaking, and sometimes you just have to accept it for practical reasons (preferably late in the game, you should be less willing to compromise early on), but it should always make you cast a suspicious eye towards it.
throw table_exception("(? ???)? ? ???");