Good coding practice / refactoring to remove multiple inheritance

6 views (last 30 days)
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
classdef Cnewbase
  • 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?

Answers (1)

Brendan Hamm
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.
Brian on 19 Sep 2018
I initially used this practice to execute forward / backward compatibility.
Class A would work on one design intent. Class B would work on perhaps other file formats or would add / change methods, leaving A to still work on old methods / file formats.
Now I am ready to leave those behind and move forward with C as supporting all the desired features without legacy features.
Is there a better style practice to achieve forward / backward compatibility? I used inheritance.
Is there a good way out of this mess?
Brendan Hamm
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.

Sign in to comment.


Find more on Construct and Work with Object Arrays 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!