On Tuesday, 14 February 2012 at 22:45:28 UTC, H. S. Teoh wrote:
On Tue, Feb 14, 2012 at 11:00:43PM +0100, 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?
I do that all the time. I guess that makes me crazy. :)
[...]
What I want to know from you people... is that stupid? To even
think
about doing something like this? Basically mixing procedural
and OOP?
D allows you to program in functional style, OOP, procedural
style, or
any combination of them. There is a reason D was designed this
way. :)
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?
[...]
OOP has its own share of dangers and disadvantages. Which,
sadly, most
people don't talk about very much because they don't have the
guts to go
against the current cool trendy bandwagon that everyone's
jumping on.
Templates, for one thing, don't fit very well into the OO
paradigm (ever
tried a virtual template function?), even though you *can* use
it to
enhance an OO-based design. And D's templates are one of the
best things
about D, ever.
Of course, many aspects of OO does help with large projects, so
it's
usually a good idea to take advantage of that. But that doesn't
mean you
*have* to use OO, or that it's "bad" to mix in procedural stuff
when it
suits you.
I mean, if you take OO to the extreme, that would require
excluding all
those evil procedural constructs like if statements and for
loops, and
write everything in terms of invoking object methods... like
this
monstrosity:
class MyClass {
void myMethod() {
IntVariable i;
ForLoopFactory.create(
new IntSetter(i.address(), new Number(0)),
new BooleanCondition(
new LessThanComparator(i.address(),
100)),
new IntAdder(&i, 1),
new IfStatement(
new EqualComparator(i.address(),
new Number(42)),
new FunctionCaller(writeln.address(),
new String("Found it!")),
)
).execute();
}
}
which is, of course, completely ridiculous.
The bottom line is, use whatever tools work best for what you
need to
do. If OO works well, use it. If procedural code works well,
that use
it. If both works well in different cases, then use a mix of
both
depending on the circumstances.
Trying to shoehorn everything into an object is stupid.
T
I agree with the general sentiment to have a large toolbox and
using the right tool for the job be it functional, OOP, etc..
having said that, I have to strongly disagree with both claims
above.
1. D templates are an enhanced version of C++ templates which are
a poor design. The problem stems IMO not from issues with OOP but
rather with the horrible idea of C++-like templates. Other
languages have *much* better solutions which integrate better.
2. The above horrible example completely misrepresents OOP. The
"correct" way to truly do control flow in OOP is a-la smalltalk:
class My class {
void myMethod() {
...
100.times({ ... }); // for-loop
(myVar > 42).ifTrue({ ... }); // if statement
(myVar < 100).if({.. code if true ...}, {... code if false
...}); // if statement with else clause
...
}
}
etc.. incidentally, Smalltalk uses selectors (Objective-C is
basically fugly smalltalk..) : (expression) ifTrue: [ ^42 ]
ifFalse: [ ^24 ] the above returns 42 if true and 24 otherwise.