Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP: Where to stop Abstracting

Where do you draw the line to stop making abstractions and to start writing sane code? There are tons of examples of 'enterprise code' such as the dozen-file "FizzBuzz" program... even something simple such as an RTS game can have something like:

class Player {} ;/// contains Weapons
class Weapons{} ;/// contains BulletTypes
class BulletType{} ;///contains descriptions of Bullets 
class Bullet{} ;///extends PlaceableObject and RenderableObject which can be placed/drawn respectively
class PlaceableObject{} ;///has x,y,z, coords
class RenderableObject{} ;///an object with a draw() command
class MovingObject{}; ///an object with a move() function

etc... and it can turn into a nightmare. This can be drawn to its logical extreme, much like functional programming can be drawn to the extreme where you can create a language with only variables, function application, and anonymous function definitions (although I must admit that is slightly more elegant)...

Any sane advice on this topic?

like image 888
Claudiu Avatar asked Oct 19 '08 07:10

Claudiu


People also ask

How much abstraction is too much programming?

Simply put, there is too much abstraction if the code is difficult to understand. Now this isn't to say that you should hard code everything, because that's the easiest code to write and read. The easiest test is to either put it down for a few days, pick it back up and ask yourself, does this make any sense.

Is too much abstraction good?

Over abstraction can create unmaintainable, untestable monstrosities. Under abstraction may mean duplicate code, but duplicate code is almost always better than unmaintainable code. That is until it becomes unmaintainable itself. Duplicate code can be fixed via factoring; the over-abstracted code may not be fixable.

How is abstraction used in everyday life?

Abstraction in the real world Sounds familiar? Making coffee with a coffee machine is a good example of abstraction. You need to know how to use your coffee machine to make coffee. You need to provide water and coffee beans, switch it on and select the kind of coffee you want to get.

Why do we need abstraction?

The main purpose of abstraction is hiding the unnecessary details from the users. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user. It helps in reducing programming complexity and efforts.


1 Answers

  1. YAGNI (You Ain't Gotta Need It). Don't create abstractions you don't see immediate use for or a sensible reason. This way you have a simple thing that may become more complex, instead of a complicated things that you would strive to make simpler, but lose.
  2. Make sure the abstractions make sense. If they're too far from reality, too hard to justify... forget it.
  3. Let the solution feel natural. Work on it until it does. Then for an unfamiliar person the solution should seem so obvious, that he screams "how could you have done it differently?".
  4. Don't try to predict the future. You can't. If you try to cover all 10 possible cases, you will soon discover 11th and more, and it will be more difficult to implement it because of previous 10, not encountered in practice. Make it simple and easy to adapt. Software needs to be changed, but ease of adaptation (agility) is often much better strategy than trying to cover all maybe-possible cases up-front.
like image 200
Paweł Hajdan Avatar answered Sep 22 '22 20:09

Paweł Hajdan