Programming Style (C with Namespaces vs. ???)

Hi everyone,

I'm doing my first embedded project and I noticed the practice of programming mostly in C and scoping things out by using static declarations inside the cpp file. That was a bit unusual to my eyes.

I was thinking of still using namespaces but making the implementation with static classes declared and implemented within a .cpp, not in the .h which seems a bit nicer (to my eyes) instead of the C-style programming. Given that they would be within my namespace that should limit link collisions even if I do not use anonymous namespaces. The static methods could still be used as callbacks.

Something like this:

In foo.h (same as in "C with Namespaces")
Code:
namespace Foo {
  void foobar();
}

In foo.cpp
Code:
namespace Foo {

  class fooImp {
      static void foobar() { ... blah, blah... }
      <...rest of the implementation...>
  }

  void foobar() {
     fooImp::foobar();
   }
}

In main.cpp or main.ino (same as in "C with Namespaces")
Code:
Foo::foobar();

Being a noob to embedded systems, and only doing this as a hobby, I was wondering over the pros/cons of these and perhaps other approaches. The free-floating functions kind of induce me seizures LOL

I'm not writing libraries. I'm using Clion with PlatformIO instead of the Arduino UI... if it makes a difference.

What style do you use?
 
Last edited:
Simplicity. Embedded hobby projects usually don't have gazillions of files and name conflicts therefore namespaces are rarely used.

Historically embedded projects did not use C++ at all because modern C++ with templates, std lib might bring bloat that is not really welcome when you need to compile things that must fit into limited flash space (like old ATMega uC).

Therefore even though C++ is used now, an "old school" "C with classes" is frequently used instead of bleeding edge C++23 or higher.

Of course namespaces by themselves do not generate bloat, so you are free to use them if you wish.
 
Last edited:
Hmmm... in the project that I'm working on, a universal testing machine, the stuff is simple, move this, measure that, record stuff, show some graphs and calculate some properties. Though there is a lot of UI and a fair number of inputs as in buttons, a force meter, a jog encoder, a glass scale, limit switches and stuff. But the programing stuff should be straight forward (famous last words). I can stay clear from templates and do old school memory management :)

Thanks!
 
Back
Top