Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How flexible should you make your classes?

I have always wondered if I am thinking ahead too much or too little before I code something. This is especially true for me if I am not sure what possible future requirement changes I will be required to account for are. I don't know how flexible or abstracted I should make my classes. I'll give a quick example.

You want to write a program that plays blackjack against a computer and you're the type of person that likes to experiment. You begin to write the code for the deck, but then you realize blackjack could have 1, 2, 4, or any number of decks. You account for that, but then you realize that maybe the deck will be altered and not have any cards of value ten. You then decide that the deck should be completely versatile to allow any number of suits or ranks. You then decide that the rules for the deck should be able to be altered from the standard number of suits multiplied by the unique ranks to equal the total amount of cards in the deck... You can see where I am going here.

My question is this, are there any guidelines for how flexible a class should be?

like image 862
TheKobra Avatar asked Sep 26 '13 20:09

TheKobra


2 Answers

Favor minimalism and encapsulation, avoiding functionalities you don't need.

It's of course good to design based on needs, but cluttering designs with things you do not use -- or could possibly use in the future -- should be minimized. It's fine to consider and implement what you are sure you will need.

When you understand and specify a 'future problem' (specifically, at that point in the future), you will often solve it different from today's solution.

like image 79
justin Avatar answered Oct 01 '22 03:10

justin


Check out the great paper "On the Criteria to be Used in Decomposing Systems into Modules" by David Parnas, from back in 1972.

Generally speaking, you should try to identify areas of responsibility that can be pushed behind a very simple interface that hides useful functionality and complexity. You should strive to separate the what from the how in areas you feel are most likely to change (i.e. predicting variation).

like image 24
Matt Self Avatar answered Oct 01 '22 01:10

Matt Self