Jump to content
  • Advertisement
Sign in to follow this  
Kirlim

C++ [Code Organization] What should I do with private static methods that does not access instances variables?

This topic is 388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've recently started reading Clean Code: A handbook of Agile Software Craftsmanship. The concepts in there threw me in some occasional code refactoring. While making changes, I end up with methods that are entirely helper methods and does not make any sense in being exposed as public. I make them private. Some of these methods also does not need to access instance or class variables or methods even while still being tightly coupled to the class itself (i.e a helper method to ensure consistent text format related to the instance, for debug logging). I make them static.

On the Header file, I declare those private static methods in the class declaration. They feel like useless noise, since they are so "private" and "coupled". In this situation, is there something that is believed to be a good way of action? I thought of moving them to anonymous namespaces into the source file as functions, but then it feels like I'm hiding functionalities.

Share this post


Link to post
Share on other sites
Advertisement

I generally try to avoid advertising private implementation details in headers as much as possible. If it's functionality that can be resused elsewhere, it goes into a shared utilities library. Otherwise, if it can be easily refactored to not depend on private data, it lives in an anonymous namespace in the type's source file. This has the added benefit of allowing you to change the functionality without modifying a header and forcing a lot of other code to recompile. Look into pImpl for more flavors and examples of the pattern :)

Edited by Zipster

Share this post


Link to post
Share on other sites

Be aware of the reasons you're doing it, so you aren't just changing code for the sake of changing code.

 

If you're doing it because the code has usefulness on its own, or can be reused, then it should go somewhere publicly accessible. They can go out in the namespace as a freestanding function rather than static. 

For some things I like the anonymous namespace inside a C++ file.  When they aren't reusable and aren't part of the class, they can quietly live there without any risk of causing issues, such as rare but still occasional issues of code getting pulled in from another library through aggressive link-time optimizations.

 But in both cases, unless I have a reason to make the change I don't change it.  

Share this post


Link to post
Share on other sites

Thank you for both responses! They helped me to decide more easily where to put those methods without going by "feelings" alone.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!