Ways to show your co-programmers that some methods are not yet implemented in a class when programming in C++

后端 未结 7 1605
生来不讨喜
生来不讨喜 2021-02-15 04:02

What approaches can you use when:

  • you work with several (e.g. 1-3) other programmers over a small C++ project, you use a single repository
  • you create a class,
相关标签:
7条回答
  • 2021-02-15 04:24

    Assert is the best way. Assert that doesn't terminate the program is even better, so that a coworker can continue to test his code without being blocked by your function stubs, and he stays perfectly informed about what's not implemented yet.

    In case that your IDE doesn't support smart asserts or persistent breakpoints here is simple implementation (c++):

    #ifdef _DEBUG
        // 0xCC - int 3 - breakpoint
        // 0x90 - nop? 
        #define DebugInt3 __emit__(0x90CC)
        #define DEBUG_ASSERT(expr) ((expr)? ((void)0): (DebugInt3) )
    #else
        #define DebugInt3
        #define DEBUG_ASSERT(expr) assert(expr)
    #endif
    
        //usage
        void doStuff()
        {
            //here the debugger will stop if the function is called 
            //and your coworker will read your message
            DEBUG_ASSERT(0); //TODO: will be implemented on the next week; 
                             //postcondition number 2 of the doStuff is not satisfied;
                             //proceed with care /Johny J.
        }
    

    Advantages:

    1. code compiles and runs
    2. a developer get a message about what's not implemented if and only if he runs into your code during his testing, so he'll not get overwhelmed with unnecessary information
    3. the message points to the related code (not to exception catch block or whatever). Call stack is available, so one can trace down the place where he invokes unfinished piece of code.
    4. a developer after receiving the message can continue his test run without restarting the program

    Disadvantages:

    1. To disable a message one have to comment out a line of code. Such change can possibly sneak in the commit.

    P.S. Credits for initial DEBUG_ASSERT implementation go to my co-worker E. G.

    0 讨论(0)
  • 2021-02-15 04:25

    Declare it. Dont implemented it. When the programmer use to call the unimplemented part of code linker complains, which is the clear hit to the programmer.

    class myClass
    {
        int i;
    public:
        void print(); //NOt yet implemented
        void display()
        {
            cout<<"I am implemented"<<endl;
        }
    };
    
    int main()
    {
        myClass var;
        var.display();
        var.print(); // **This line gives the linking error and hints user at early stage.**
        return 0;
    }
    
    0 讨论(0)
  • 2021-02-15 04:38

    I would not check it into the repository.

    0 讨论(0)
  • 2021-02-15 04:41

    You can use pure virtual functions (= 0;) for inherited classes, or more commonly, declare them but not define them. You can't call a function with no definition.

    0 讨论(0)
  • 2021-02-15 04:42

    I actually like the concept from .Net of a NotImplementedException. You can easily define your own, deriving from std::exception, overriding what as "not implemented".

    It has the advantages of:

    1. easily searchable.
    2. allows current & dependent code to compile
    3. can execute up to the point the code is needed, at which point, you fail (and you immediately have an execution path that demonstrates the need).
    4. when it fails, it fails to a know state, so long as you're not blanketly swallowing exceptions, rather than relying upon indeterminable state.
    0 讨论(0)
  • 2021-02-15 04:43

    The simplest answer is to tell them. Communication is key whenever you're working with a group of people.

    A more robust (and probably the best) option is to create your own branch to develop the new feature and only merge it back in when it's complete.

    However, if you really want your methods implemented in the main source tree but don't want people using them, stub them out with an exception or assertion.

    0 讨论(0)
提交回复
热议问题