Good coding practice / refactoring to remove multiple inheritance
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
properties
end
methods
end
end
classdef C < B
properties
end
methods
end
end
I want an efficient way to refactor this into
classdef Cnewbase
properties
end
methods
end
end
- 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?
0 commentaires
Réponses (1)
Brendan Hamm
le 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.
2 commentaires
Brendan Hamm
le 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.
Voir également
Catégories
En savoir plus sur Software Development Tools dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!