Samstag, 4. Juni 2011

Code use and re-use

One of the things that may seem trivial is how in software systems reuse is achieved. I'm going to write about some thoughts of mine in this regard.


OOP
I think that most people would agree with the OO(Object Oriented) Paradigm, it is common to use inheritance (implementation inheritance).  So you define code in a class, now if you extend that class from another one you can use the code from the parent class just fine.

Did you reuse the code from the parent class or are you now just using it?


It is quite important to pay attention to this. You can now use the code from the parent class but just in the child classes. So there is a dependency from child to parent class. I would argue that this is code use.

FP
In functional programming reuse is achieved through composition. Which means if you have a function which takes an Int and results in an array of chars and another one which takes an array of chars and results in a String, I can create yet another function which takes an Int and results in a String. Again this seems trivial and one might ask why same can't be done with OOP?

RT
The answer to this is (I guess) referential transparency. The fundamental principal of  FP is that if I  invoke a function with the same arguments I will always get the same result (or type therefore). This guarantees me re-usability.

Code use and re-use is about granularity and scope.  To identify clearly what you need in a specific situation is important.

So these are quite different approaches and there are many other aspects involved (besides re-usability) in software development. But for now I like the FP approach better. Because when composing functions I can rely on referential transparency while with OOP state and functions are usually mixed up.