3 ms·
> Refactoring code in C++ is challenging because there are roughly ten different ways to do one thing. When learning C++ I was not yet proficient in English, s
by jwrallie 2y ago
> Refactoring code in C++ is challenging because there are roughly ten different ways to do one thing.
When learning C++ I was not yet proficient in English, so I learned the version that was taught in the books translated to my language. I only later realized how much the language evolved over time, so while I can do C++, I feel I am writing in old style.
I wonder what is a good way to refactor code for keeping it conforming to updated best practices.
- mfdoom 2y ago[dead]
- kimixa 2y agoYou can check out the clang-tidy "modernize-" set of checks [0]. I think some IDEs like clion or visual studio (both the "full fat" and VS code with a plugin) have options to integrate this. Or even just as a build pass - if you use cmake you can just set the CMAKE_CXX_CLANG_TIDY variable [1] to the path of the clang-tidy executable and it'll run every build. They will likely pick out the "low hanging fruit", the easy like-for-like transforms like for loops or std::array, but more "structural" differences that are considered "bad" in modern C (like raw pointers or non-trivial users of those some of those same "replaced" features) will likely require much more significant manual input. In many ways the changing environment of more modern c++ changes how you holistically should design code and data structures, and no tools will "fix" that. But it's a start, and you need to start somewhere. I mean it's not like there's an "end target" for the "perfect modern c++" anyway. I also wouldn't treat the output of clang-tidy as gospel 100% truth either, look at the changes it suggests and decide if they are actually making clearer code before blindly applying them, it might even help with understanding the new features if you can understand why they were changed and what problems they are trying to solve too. [0] https://clang.llvm.org/extra/clang-tidy/checks/list.html https://clang.llvm.org/extra/clang-tidy/checks/list.html [1] https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLANG_TIDY.html https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLAN...