2

I have a large codebase filled with names such as MyCompanyMyDeptMyType. This is making the code rather hard to read. I would like to use a tool to move all these names to a nested namespace MyCompany::MyDept.

My header files would look like

namespace MyCompany {
namespace MyDept {   
  // here goes the code with shorter names
}
}

and my cpp files would end up with using namespace MyCompany::MyDept; at the top.

Does such a tool exist? (clang-rename?) If not, is my best shot to write my own clang AST visitor?

Touloudou
  • 2,079
  • 1
  • 17
  • 28
  • You could probably write a small C++ program with less than 10 lines to parse a header file and output the modified header file. – Peter Nov 10 '20 at 09:24
  • Unless your command has organised its departments in the same way one organises types in a namespace, the `MyDept` layer should not be necessary. – Mansoor Nov 10 '20 at 09:25
  • 1
    @Peter really? I would be impressed if you could write a parser that succinctly. Also, you are assuming no templates. – Mansoor Nov 10 '20 at 09:27
  • @M.A Not sure I understand your first comment. `MyDept` corresponds to my part of the organization. If I don't have it, some types might clash with types from other departments in my company. – Touloudou Nov 10 '20 at 09:40
  • @Touloudou why are types in your codebase bound to departments? Do your integer types vary between departments? – Mansoor Nov 10 '20 at 09:41
  • @M.A Sadly, there is rather little communication between departments. My `FooManager` could well clash with the `FooManager` from another part of the company. – Touloudou Nov 10 '20 at 09:43
  • @Touloudou :) surely you want to make one generic `FooManager` for all departments? and not have everyone write their own. But I feel your pain, seen it happen before. – Mansoor Nov 10 '20 at 09:44
  • @M.A If I could write everything from scratch, sure. :) Right now, given the current state of affairs, I am trying to at least get readable code. – Touloudou Nov 10 '20 at 09:46
  • Are the types you mention inside a superior namespace? – user1810087 Nov 10 '20 at 09:46
  • @user1810087 No they are not. However, these will not be used outside of my department if this is where you are going. – Touloudou Nov 10 '20 at 09:47
  • If you want to this on files basis and you have an editor that supports regex replacing (i know VS studio does - i suspect VS code does as well, as well as a host of other code editors) - it should be possible to build a regex that does this i would say - then you may or may not need to autoformat afterwards. – darune Nov 10 '20 at 09:47
  • @darune Doing this on a per-file basis is going to be tricky, because everything is so intertwined. If I change one base class for example, all derived classes must change too, etc. If I change a header file, many many files will break down the line. – Touloudou Nov 10 '20 at 09:49
  • @Touloudou I see, then you can use VS studio or resharper. If you can load your entire codebase that needs changing into VS studio then you should be able to change on a class basis. – darune Nov 10 '20 at 09:51

0 Answers0