Tagged good practices
Properties in the D programming language
Posted on by Idorobots
Just to evangelize D a little and increase my code/crap ratio, let's pretend we develop a library in C++ that contains this class:
class SomeMetaVariables {
public:
std::string foo;
bool bar;
};
// ... Somewhere in the client code:
SomeMetaVariables baz;
baz.foo = "foo";
baz.bar = true;
Our library is quite successful and many people are using SomeMetaVariables
despite its obvious flaws. Now, say we get many requests for additional functionality, for example: "Make bar
true
only when foo
is set to "foo" and other way arround." "Well, ok." - we say and commit this new version of SomeMetaVariables
:
class SomeMetaVariables {
std::string foo;
bool bar;
public:
std::string getFoo() {
return foo;
}
std::string setFoo(std::string newFoo) {
foo = newFoo;
bar = (foo == "foo");
return foo;
}
bool getBar() {
return bar;
}
bool setBar(bool newBar) {
bar = newBar;
foo = bar ? "foo" : "";
return bar;
}
};
We implemented the requested feature, but SomeMetaVariables
' interface has changed... "But why are you mad clients? You asked for it!" - cries the C++ developer.
Mixins make sense
Posted on by Idorobots
Let's suppose we have this class hierarchy:
It's a fairly specialized class hierarchy providing some functionality, let's say for the sake of this example that SomeClass
provides a kind of player-game interaction interface and HierarchyWith
defines it as a trigger based interaction (as in "a player does something - it triggers a response"). Our ExampleClass
represents an entity that can be triggered by the player and that generates some response and in addition to that is further specialised to have some concrete functionality - let's say it's a lever that a player determined enough can pull to flood something with delicious magma. As we clearly can see ExampleClass
is well-defined in terms of OOP, it's highly specialised and modular, but as an in-game entity it lacks a rather crucial functionality - it can't even be displayed on the screen. What do we do now?