Anti-OOP... stupid?

Timon Gehr timon.gehr at gmx.ch
Tue Feb 14 14:47:52 PST 2012


On 02/14/2012 11:00 PM, Zero wrote:
> Hello!
>
> I've recently started to work with D, and I'll start a "bigger" project
> soon, using it. For a few days I've been thinking about the approach
> I'll take here, and since I don't /have/ to use full OOP in D, I was
> wondering... how crazy is it to not use full OP nowadays?
>
> Naturally one would use objects, but since this isn't C#, or Java, not
> freaking everything has to be an object. The C style kinda feels more
> natural to me. (I know, I know, C naturally can do some OOP like stuff,
> etc... but you get the idea, mainly normal functions, some globals,
> yada, yada.)
>
> What I want to know from you people... is that stupid? To even think
> about doing something like this? Basically mixing procedural and OOP? I
> know about the dangers, and disadvantages, but if those don't scare one
> away, would you consider it bad, if someone did this, on a larger project?
>
> I'm looking forward to your answers.
>
> Zero

I think that is not stupid at all.

Approaching the problem the way that feels most natural is likely a good 
idea. As long as you actively keep in mind code quality measures such as 
extensibility and maintainability, everything should work out nicely. I 
encourage you to use the right abstractions. If something should be a 
free-standing function, use a free-standing function. If it is hard to 
force the involved objects into a hierarchy, use templates instead of/in 
combination with sub-typing and inheritance. If you want to parameterize 
a function on a few actions, consider using closures. If it is really 
clear how a well-designed class hierarchy would look like, use OOP. etc.

The basic achievement of OOP is that it can be used to replace 
procedural code of this general form:

void foo(ref S x){
     switch(x.kind){
         case KIND1:
             ...
             break;
         case KIND2:
             ...
             break;
         ...
     }
}

void bar(ref S x){
     switch(x.kind){
         case KIND1:
             ...
             break;
         ...
     }
}

This pattern emerges very often in practise, that is why OOP is useful. 
If you have anything that should work like that, using classes will do 
the job. Whether or not you apply OOP, make sure to keep stuff 
extensible. It does not hurt at all if your code base is more flexible 
than necessary.




More information about the Digitalmars-d-learn mailing list