Best reason to replace structures with classes
이전 댓글 표시
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.
채택된 답변
추가 답변 (3개)
Sean de Wolski
2012년 1월 23일
2 개 추천
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
2016년 1월 20일
2 개 추천
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
2012년 1월 23일
1 개 추천
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
K E
2012년 1월 23일
Andrew Newell
2012년 1월 23일
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.
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!