It seems like the majority of Arduino applications are written without using classes / objects. Is there a reason for this? Does is slow down the Arduino or somehow present undesirable behavior?

  • 1
    Please note that "functional programming" is not what you think it is, I guess you wanted to say "procedural programming".
    – jfpoilpret
    Aug 21 '16 at 16:13
  • Functional programming usually involves the use of closures, in languages that support them. C++ doesn't, and you would probably use functors instead. Aug 21 '16 at 16:59
  • 1
    Check out this article. It explains the neglegable performance effect of using classes.
    – Gerben
    Aug 21 '16 at 17:16

Your assumption is quite wrong:

  • The majority of libraries use OOP.
  • The majority of in-built device drivers use OOP.
  • The core is filled with many OOP helper objects and classes.
  • 99.99% of all sketches use those OOP objects.

The sketches themselves may not be written as a class or set of classes for a number of reasons:

  1. There is no point
  2. The programmer doesn't know how

When a sketch is just stitching together calls to objects defined through libraries and making a few decisions there is little point in making the sketch itself an object. It's just a waste of time. In general if the sketch gets too complex to manage it gets broken down into smaller units - i.e., libraries - which are very often written as classes.

The moment you do Serial.begin(9600); you are using OOP.

  • 1
    Is there more than one Serial object? If not, what's the difference between Serial.begin and (hypothetically) Serial_begin?
    – user253751
    Aug 22 '16 at 0:14
  • 1
    On some boards yes there are multiple Serial objects.
    – Majenko
    Aug 22 '16 at 0:15
  • 1
    If OOP is using C++ classes for implementation then this answer is fine. A more detailed answer may be found by first reading Peter Wegner's paper; cse.msu.edu/~stire/cse891/wegner.pdf. Arduino would be OOP if interfaces/classes would be more encapsulated, reused and extended. Aug 23 '16 at 10:57
  • You mean like Cosa? ;)
    – Majenko
    Aug 23 '16 at 10:57
  • Hard to miss :) Aug 23 '16 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.