Help with Object Oriented Design

Discussion in 'Programmer's Corner' started by Willyd57, Dec 9, 2016.

  1. Willyd57

    Thread Starter New Member

    Nov 21, 2016
    3
    0
    I only recently began learning about electronics in general which quickly led me to the Arduino stuff. I have done some C programming earlier in my life and I took C programming in college, but I'm 59 years old and all my programming education was a very long time ago. Now I am working on a robotics project that amounts to getting my feet wet so to speak with programming for hardware. The ultimate goal is to build a quadcopter. I am thinking that the program design might benefit from being object oriented like C+ +. Trouble is, I know very little about OOP so there would be a learning curve for me to tackle.

    The first question is do you guys that do OOP think it's a better choice than designing and writing the code in C. I am trying to get my head around the possible benefits of creating objects that know how to do something like fly, which would have methods for operating motors for instance.

    In addition, does anyone know of some really good reference material for OOP design & development especially with Arduino? I have an Arduino programming book but it is fairly basic and while it was useful as a review for me, I was already familiar with about 90% of what the book had in it.

    Thanks in advance
    Bill
     
  2. WBahn

    Moderator

    Mar 31, 2012
    22,984
    6,881
    OOP is not the end-all-be-all that many people try to make it out to be. In general, you pay for all of the nice things it offers with significant impairments in performance and things like code size suffer, as well. This means that you often need significantly more capable hardware to perform the same real-time tasks. If the hardware on that quadcopter aren't up to it, then you will just flounder. As a general note, keep in mind that non-OOP code can be written in ways that retain a lot of OOP-like features but also retain non-OOP-like performance. But it requires a lot more discipline on the part of the code developer.
     
    Willyd57 likes this.
  3. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    4,718
    5,047
    OOP shines with large memory and fast processes that hide the run-time abstractions well. I really can't think of a good reason to use C++ in a small embedded project unless you're a masochist but using OO with C using pointers(to both data and code) and structures with both data and code pointers is a typical method of writing complex applications. In systems and embedded programming, you're simply better off with C because it forces you to write code that closer to the hardware. C++ tries to solve problems that don't normally exist at the micro-controller domain. A less system-oriented context-dependent (a simple local code view context doesn't tell you full information) language using objects, templates and function overloading are neat things for an Android app that runs on top of a real OS doing the bit twisting. I would say the key to a good embedded programming language is not syntactic extensions of language, it's I/O, data, structures and operations closely matching the hardware in one precise programming view. For this well written C (or a restricted C++ subset) is perfectly functional. C is "as simple as possible, but no simpler".
     
    Willyd57 likes this.
  4. Willyd57

    Thread Starter New Member

    Nov 21, 2016
    3
    0
    If I had the option to love both responses to my post rather than just like them I would have. Good stuff from both of you.

    My mind is now at ease. I almost feared that the answer would have been yes, go for the C++ it's so much better! Now I can proceed using C and know that it is the correct way to go.

    The projects in front of me are challenging, to say the least. But I have a great reference book for C programming and now I know this forum has my back. That means I can relax and enjoy the journey.

    Thanks
    Bill
     
  5. WBahn

    Moderator

    Mar 31, 2012
    22,984
    6,881
    But never forget that C is more than willing to give you plenty of rope with which to hang yourself!
     
    Willyd57 likes this.
  6. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    4,718
    5,047
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg"
    Bjarne Stroustrup

    Every simple bug that's eliminated by a language constraint is an opportunity for a much more complex bug later.
     
    Robert Murphy and Willyd57 like this.
  7. MrSoftware

    Well-Known Member

    Oct 29, 2013
    1,051
    292
    Anything that can be done with OOP, can be done with strait C. But, generally speaking, the more complicated your task the more OOP can help. If you make use of big or sloppy libraries, or if your code is written in an inefficient way, then yes, using C++ can result in a performance and size penalties vs. strait C, BUT if you avoid inefficient libraries, write sensible code and use a good compiler then the performance differences can be minimal or insignificant. Plus using good practices can reduce common errors and make exception handling easier. Good C++ can easily be faster and smaller than sloppy C. In the end, the thought that you put into your code will affect your result more than the choice of using C or C++.
     
    Willyd57 likes this.
Loading...