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

7

9

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, declare its methods
  • you don't have a time do implement all methods yet
  • you don't want other programmers to use your code yet (because it's not implemented yet); or don't want to use not-yet-implemented parts of the code
  • you don't have a time/possibility to tell about all such not-yet-implemented stuff to you co-workers
  • when your co-workers use your not-yet-implemented code you want them to immediately realize that they shouldn't use it yet - if they get an error you don't want them to wonder what's wrong, search for potential bugs etc.
Inshore answered 22/7, 2010 at 23:25 Comment(3)
You could use preprocessor warnings inside method stubs.Prana
You should create a branch for your temporary code and merge it when you are done implementing it.Development
@Markos: it's not suitable for top-down developmentMichelinamicheline
E
14

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.

Endive answered 22/7, 2010 at 23:29 Comment(4)
+1. for assert. Its the most direct answer for doing exactly what the question was asking.Calix
assert/exception way is the said communication but with means of the code instead of verbal/chat communication. And in this case it should be preferred over verbal. Or what else? Every time one made a function stub, one have to bother his colleagues with meetings or phone calls, when most of them wouldn't ever call the stub?Michelinamicheline
something like this should work real well: assert(false && "Not Implemented Yet, do not go hunting for bugs!")Quarta
@Alsk, indeed I was intending "tell them" to be broadly applicable to many forms of communication (e-mail, comments, etc.). I personally would rather know not to use a function because that fact was communicated with me than to try and use it only to find out at compile/runtime that I shouldn't have been in the first place.Endive
P
10

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.
Parton answered 23/7, 2010 at 0:9 Comment(0)
S
8

You should either, just not commit the code, or better yet, commit it to a development branch so that it is at least off your machine in case of catastrophic failure of your box.

This is what I do at work with my git repo. I push my work at the end of the day to a remote repo (not the master branch). My coworker is aware that these branches are super duper unstable and not to be touched with a ten foot pole unless he really likes to have broken branches.

Git is super handy for this situation as is, I imagine, other dvcs with cheap branching. Doing this in SVN or worse yet CVS would mean pain and suffering.

Serle answered 22/7, 2010 at 23:34 Comment(1)
Trust me, not nearly as much suffering as you get if you don't have version control. Even RCS on NFS is better than that. (Just. You might as well use VSS in that case for all the usability of that.)Supernumerary
M
7

I would not check it into the repository.

Macready answered 22/7, 2010 at 23:28 Comment(1)
Best option, methinks. If the code isn't usable code, just don't check it in until it's in a usable state. No harm if they can't use it anyway. Or branch-then-merge.Jeanicejeanie
H
5

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;
}
Hemimorphic answered 21/11, 2011 at 12:44 Comment(0)
M
4

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.

Michelinamicheline answered 23/7, 2010 at 9:32 Comment(3)
Wouldn't it be better to replace __emit__(0x90CC) with __asm__("int $0x3;")?Archaism
@tauran: asm was not allowed in class templates (at least in my IDE), so it wouldn't be possible to use it everywhere.Michelinamicheline
How cross-compiler is this ?Glutton
F
0

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.

Fighterbomber answered 22/7, 2010 at 23:27 Comment(4)
Pure virtual functions will need an implementation in one of the derived classes.Struck
The point is not to do that. That way if they're tried to call, it'll fail.Fighterbomber
the code won't compile, making the rest of the (already implemented) class useless.Platoon
The goal is to make sure that the specific methods can't be invoked, not to completely break existing derived classes (or consumers of the base class itself, which would suddenly become non-instantiable). And if a derived class then were to provide a stub implementation just to unbreak the build, then when the base class does provides the actual method implementation, the derived class would still happily use the wrong version. Great. Using pure virtual methods is pointless. (Your second recommendation about simply not implementing the methods is fine, though.)Struck

© 2022 - 2024 — McMap. All rights reserved.