Best Practices: Class properties

I have a quick C++ best practices question about class properties:

Normally, I would probably make most class properties private, and make getter/setter functions for access. But since memory is more constrained in an embedded system, should I just make most properties public for direct access, and only write getters/setters when I need some other code to run when a value is changed?

Is it worth worrying about? Or will C++ programmer bullies make fun of me if I do it wrong? :stuck_out_tongue_closed_eyes:

@dougal, I think you are spending way to many brain cycles here!!! Using private properties allows you to control HOW these are changed and fetched since the class defines the getter/setter functions. Making them public removes that control entirely. Debugging also becomes more complicated IMO. :smile:

Just use good software development practices and let the compiler worry about optimizations.

2 Likes
2 Likes

Okay, thanks @peekay123, @jvanier, @AndyW. :slight_smile:

If you’re worried about performance and or unnecessary flash size overhead, be sure to declare your accessors and mutators as inline - the compiler will then inline the function body when possible, so you can achieve public property performance yet still have private property encapsulation.

As said already, I shouldn’t worry about premature optimization. My code is not very complex or time-critical.