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?
-
1Please note that "functional programming" is not what you think it is, I guess you wanted to say "procedural programming".jfpoilpret– jfpoilpret2016-08-21 16:13:49 +00:00Commented Aug 21, 2016 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.Edgar Bonet– Edgar Bonet2016-08-21 16:59:04 +00:00Commented Aug 21, 2016 at 16:59
-
1Check out this article. It explains the neglegable performance effect of using classes.Gerben– Gerben2016-08-21 17:16:32 +00:00Commented Aug 21, 2016 at 17:16
1 Answer
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:
- There is no point
- 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.
-
1Is there more than one
Serialobject? If not, what's the difference betweenSerial.beginand (hypothetically)Serial_begin?Stack Exchange Broke The Law– Stack Exchange Broke The Law2016-08-22 00:14:52 +00:00Commented Aug 22, 2016 at 0:14 -
1On some boards yes there are multiple Serial objects.Majenko– Majenko2016-08-22 00:15:45 +00:00Commented Aug 22, 2016 at 0:15
-
1If 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.Mikael Patel– Mikael Patel2016-08-23 10:57:17 +00:00Commented Aug 23, 2016 at 10:57
-
-
Hard to miss :)Mikael Patel– Mikael Patel2016-08-23 10:59:04 +00:00Commented Aug 23, 2016 at 10:59