• SoftwareC/C++
  • C , C++, reference or example code, and cognitive load

Hi Belans.

I'm gluing TouchOSC to the transmogriFX and it may pass that some code becomes a reference or example. Or simply might have to be read by someone.

C++-isms sometimes creep into the code but I am not going whole-hog C++ since C can get the job done and might be easier to work with in conjunction with hardware, since hardware is the ultimate non-abstraction. ;D. Also more gentle for ppl just getting their C chops stood up.

But. What do ppl generally prefer, C or C++? I saw @giuliomoro 's posting about the C++ course so there must be demand for it.

I'm down for either, and in C++'s defense code might be a little more out-of-box reusable.

Keep On Compilin', and sorry if this starts a flame war (don't do that!),
- pos

C++ is a huge, ever-evolving beast, which I am not planning on mastering in its entirety any time soon (if ever). However, even a basic usage of C++ gives access to simple features that significantly improve coding over C, namely:

  • constructor and destructors help to initialise and automatically manage resources
  • std::vector is so much better than having raw arrays or malloc'ed pointers, and it comes with no overhead,
  • I like that pass-by-reference allows to use . instead of ->, that to me improves readability
  • smart pointers are also great when you do have to use pointers
  • object-oriented programming helps preserve state and avoid globals, favours separation of concerns, encapsulation, modularisation and code reuse, as you point out. When I teach, I find it an interesting exercise to move from a proof of concept with global and static variables where one instance of something works to turning that into a self-contained class so that you can easily have many of the same thing.

So, these are the C++ features we try to roll into our core and example code, but we tend to steer clear from the more advanced and obscure features.
(as a side note, I avoid std::cout << anytime I can, because printf() is much easier to use for properly formatting things. I understand real formatted strings should be coming to C++20 ... let's see).

Thanks for the detailed, qualified viewpoint. My question was one of those oversimplified ones; not a binary one but a question of degree. You hit a main point -- to avoid being too obscurant.

So then I'll use this as guidance. The transmogriFX OSC code would benefit from a class-based treatment, eg. Knobs and Faders and collections like FacePlate and ControlSurface. If Bela-side constructs for, say, a Fader or a Knob wanted to be available in a library, it would be good to instantiate any number of instances of it, mapping 1-1 to a widget in TOuchOSC, with behaviours included. Class instantiation makes it easier for a code generator to create template code from a layout file while still allowing in-code extensions.