Sunday, November 22, 2015

Generated code

When two months ago I created generator for RTTI code I didn't realize how this one script will change so much in my programming style.

Right now I have already four of generators in my code base:
  1. RTTI - Generate part of my RTTI code.
  2. SID - Generate CStringID with already calculated hash
  3. Pimpl - Generate interface class.
  4. Enum - Generate toStringID, fromStringID functions.
So what changed for me after writing this one generator that make difference ? I try not to do monkey job. To show you how this improve my workflow let's look on generator I wrote today: Generator of Enumerator functions: toStringID, fromStringID.

This is simple problem where for some enumerators we want to have conversion from value to some verbose type. Some people may claim that this is not issue. You write functions once and they work. Later you just need to modify them when you do some changes in enum. This take like about half minute work 

... if we don't do mistake there. Then this is one extra compilation time. In some code base this can be counted in minutes. I would really love to see how much time of our programmers life we spending of stuff like that :) 

In my case I just have one macro: WR_ENUMGEN(EnumType, InvalidValue). Which look like this:
#   define WR_ENUMGEN(E, D)    \
        const CStringID&        toStringID(E a_value);                   \
        E                       fromStringID(const CStringIDa_value);
And is used like this:
    namespace EConstantsBuffers
    {
        enum Enum
        {
            CBPerFrame,
            CBPerView,
            Count,
        };
 
        typedef Enum Type;
 
        WR_ENUMGEN(TypeCount);
    };
The rest is generated :] So on top of this one line I just need to generate projects and that's all. No extra programmer steps to do. Thanks to that:

  • Adding/modifying of enumerators is simple
  • Generate code is easy to debug (what may be tricky with some macros).
  • Not slowing down my compilation. 
  • There is almost no place for any mistake  (always in sync with enum) .
  • There is unified code convention between enums.
  • Expanding of functionality need only modification of macro and/or generator.
So I don't know about you but personally with time I will probably use more and more generated code. I don't have a lot of spare time for monkey job I prefers to leave it for computers.

Greg

No comments:

Post a Comment