Monday, 30 December 2019

Actions as procedures

There is one more technical thing I've tried and it actually works. The background for this "issue" is the way OOP can fragment the code if you need more than one class where any particular class doesn't have a "major" role. It can also be fragmented by inheritance.

If there is a complex action it can become quite annoying to implement in OOP fashion, but in C++ you don't have to, since it's a multi-paradigm language. A real life example is digging with pickaxe. It's happening both in Level class and Creature/Player class, but when you create a procedure (I'm trying to avoid the word 'function' here, because there are functional programmers stalking everywhere) and in this case only one procedure which takes Level, Creature, digging item and direction as parameters, it removes the confusion by unification of the code.

A procedure solves more than one problem. First it's making the code less fragmented and in this case the procedure becomes a linear collection of actions. It's also removing code from Level and Creature classes which often become large and difficult to maintain. Not only that, with C++ you can take care of a procedure not breaking OOP, because the procedure can use class code without any side effects of regular, non-functional style.

Obviously you could do this with OOP as well, using a special class to handle this the same way, but it's the exact same thing. Where this works really well are actions such as digging, but then again it does solve only a particular problem of complex structure. OOP in general is better for low level code, handling data etc.

No comments:

Post a comment