How do you flag code so that you can come back later and work on it?

前端 未结 23 2272
后悔当初
后悔当初 2020-12-24 11:46

In C# I use the #warning and #error directives,

#warning This is dirty code...
#error Fix this before everything explodes!
<         


        
23条回答
  •  感情败类
    2020-12-24 12:17

    I'm a C++ programmer, but I imagine my technique could be easily implemented in C# or any other language for that matter:

    I have a ToDo(msg) macro that expands into constructing a static object at local scope whose constructor outputs a log message. That way, the first time I execute unfinished code, I get a reminder in my log output that tells me that I can defer the task no longer.

    It looks like this:

    class ToDo_helper
    {
      public:
         ToDo_helper(const std::string& msg, const char* file, int line)
         {
           std::string header(79, '*');
           Log(LOG_WARNING) << header << '\n'
                            << "  TO DO:\n"
                            << "    Task:  " << msg << '\n'
                            << "    File:  " << file << '\n'
                            << "    Line:  " << line << '\n'
                            << header;
         }
    };
    
    #define TODO_HELPER_2(X, file, line) \
      static Error::ToDo_helper tdh##line(X, file, line)
    
    #define TODO_HELPER_1(X, file, line) TODO_HELPER_2(X, file, line)
    #define ToDo(X) TODO_HELPER_1(X, __FILE__, __LINE__)
    

    ... and you use it like this:

     void some_unfinished_business() {
       ToDo("Take care of unfinished business");
     }
    

提交回复
热议问题