Good coding practice / refactoring to remove multiple inheritance
14 views (last 30 days)
Show older comments
Brian on 19 Sep 2018
Commented: Brendan Hamm on 20 Sep 2018
I have created some smelly code where classes are inherited and I wish to refactor the code to a new base class.
As an example:
classdef B < A
classdef C < B
I want an efficient way to refactor this into
- Is there a best practice for executing this action?
- Do I just re-write from scratch and grab the methods / properties from [A] and refactor into [C] piece by piece?
- Is there any feature in the Matlab editor that can make this operation easier?
- Is there a better way to architect code such that this is avoided?
Brendan Hamm on 19 Sep 2018
1. There is no "best" practice as this depends on why you made the initial decision to perform multiple inheritance and then later the decision which led you to change the design. Best practice would be having implemented this in the desired way the first time, but reality means design requirements often change.
2. Yes. Another alternative though is that you can still use inheritance, but overload the methods. Again, hard to know without understanding the full design (is A abstract? do all subclasses of A really require the method in question? is the implementation the same? similar? ...)
3. Other than Copy/Paste, Find & replace (ctrl-F), etc. not really. The Code Comparisson tool may come in handy here if they are "similar" enough.
4. Again, not sure what the source of the change is, so hard to generalize this.
Brendan Hamm on 20 Sep 2018
Again, it is difficult to know without the full problem at hand, but I have some thoughts that may help.
Firstly, spend a lot of time before coding in the architecture design. Layout design requirements, use cases and create UML diagrams.
It is possible that abstraction could have been useful here, possibly with more levels of inheritance. In this sense both A and B would have both inherited from some abstract class.
Another possibility would have been to further modularize the code through aggregation. If A and B both read different file types, you may have created another class (call them ARead and BRead) for each file type to read in. These classes could possibly inherit from an abstract superclass to avoid code duplication. In this manner A and B each have a similar property but the implementation differs. Again this can be implemented by having A and B derive from yet another abstract superclass.
I have found in general that the more you modularize your code, the easier it becomes to adjust to new design requirements. Consider researching some OOP design concepts and see which one best fits your project.
There is no easy way out of any coding mess, you just need to decide at this point whether it is worthwhile to completely refactor your code, or if writing a quicker hack fix will suffice. Bear in mind, the latter will likely lead to another similar challenge down the line. While it may seem daunting to undertake the task of re-writing your classes, considering a specific design upfront save lots of time later and I tend to favor spending my time now.
Find more on Scope Variables and Generate Names in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!