Best reason to replace structures with classes
Afficher commentaires plus anciens
I use structures a lot. For example I might find the statistics of data contained in a structure containing measurements of ocean waves,
ndbc.timeGmt
ndbc.waveHeight
ndbc.wavePeriod
What would improve if I used classes instead of structures? For example would it make my code more transparent to another programmer? Help me decide if it's worth my time to (a) learn how to use classes myself, and (b) explain them to others using my code who are unfamiliar with classes as well.
Réponse acceptée
Plus de réponses (3)
Sean de Wolski
le 23 Jan 2012
2 votes
Using OOP can really help "idiot-proof" your code if well written. That is, you can make sure conditions are met before setting properties, you can conceal things you need (such as flags or handles). If you're going to have others using the code, I think this is a good reason to lean toward OOP. It just makes preventative programming easier.
As far as others being able to understand the classes: If you document them well and organize them well, it should be just as easy to follow as standard code (after a syntax learning curve).
My $0.02. (Ps. I just started using classes about a month ago, so I still have a lot of mountain to climb)
Jose Marcelo Duarte
le 20 Jan 2016
2 votes
You can use OOP paradigm with structures too. I have recently converted some of my code from object to struct in order to compile individual methods into MEX functions (MatLab Coder v2015a do not accept class files as entry point). In my case, I basically only have to convert all methods, including the constructor, in functions and put them in separated files.
The main advantages of using classes for OOP instead of structures are code organization and protection against coding errors. This come with a cost in performance.
A brief explanation about class and object for people familiar to structure is "An object of a certain class is a structure that has a set of predefined functions to operate with it. The definition for both, the structure type and the set of function, is in its class file".
Andrew Newell
le 23 Jan 2012
1 vote
There are a lot of potential advantages to object-oriented programming. I can hardly do them justice in a short answer. Try reading Why use object-oriented design.
2 commentaires
K E
le 23 Jan 2012
Andrew Newell
le 23 Jan 2012
I use them a lot. There is definitely a learning curve, but I think it is worth the effort if you are creating a large program.
Catégories
En savoir plus sur Image Arithmetic dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!