在使用 C++ 进行编程时,向您的合作程序员展示某些方法尚未在类中实现的方法;

发布于 2024-09-10 17:06:53 字数 301 浏览 6 评论 0原文

在以下情况下可以使用什么方法:

  • 您与其他几个(例如 1-3)个程序员一起处理一个小型 C++ 项目,您使用单个存储库
  • 你创建一个类,声明它的方法
  • 你还没有时间实现所有方法
  • 您还不希望其他程序员使用您的代码(因为它尚未实现);或者不想使用代码中尚未实现的部分
  • 你没有时间/机会向你的同事讲述所有这些尚未实施的事情
  • 当您的同事使用您尚未实现的代码时,您希望他们立即意识到他们不应该使用它 - 如果他们遇到错误,您不希望他们想知道出了什么问题,搜索潜在的错误等。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

幼儿园老大 2024-09-17 17:06:53

最简单的答案就是告诉他们。当你与一群人一起工作时,沟通是关键。

更强大(可能也是最好)的选择是创建自己的分支来开发新功能,并仅在完成后将其合并回来。

但是,如果您确实希望在主源代码树中实现您的方法,但不希望人们使用它们,请使用异常或断言将它们删除。

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.

皇甫轩 2024-09-17 17:06:53

我实际上很喜欢 .Net 中 NotImplementedException 的概念。您可以轻松定义自己的,派生自 std::exception,将 what 重写为“未实现”。

它的优点是:

  1. 易于搜索。
  2. 允许电流&要编译的依赖代码
  3. 可以执行到需要代码的位置,此时,您会失败(并且您立即有一个表明需要的执行路径)。
  4. 当它失败时,只要您没有完全接受异常,而不是依赖于不确定的状态,它就无法达到已知状态。

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.
油饼 2024-09-17 17:06:53

您应该要么不提交代码,要么更好的是,将其提交到开发分支,以便在您的机器发生灾难性故障时至少可以离开您的机器。

这就是我在工作中使用 git 存储库所做的事情。我在一天结束时将我的工作推送到远程存储库(而不是主分支)。我的同事知道这些树枝非常不稳定,不要用十英尺的杆子去碰,除非他真的喜欢折断的树枝。

我想,对于这种情况,Git 非常方便,就像其他具有廉价分支的 dvc 一样。在 SVN 或更糟糕的 CVS 中执行此操作将意味着痛苦和磨难。

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.

荒芜了季节 2024-09-17 17:06:53

我不会将其签入存储库。

I would not check it into the repository.

花伊自在美 2024-09-17 17:06:53

声明一下。不要实现它
当程序员使用调用代码的未实现部分时,链接器会抱怨,这对程序员来说是明显的打击。

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;
}

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;
}
ㄖ落Θ余辉 2024-09-17 17:06:53

断言是最好的方法。不终止程序的断言更好,这样同事就可以继续测试他的代码,而不会被您的函数存根阻止,并且他可以完全了解尚未实现的内容。

如果您的 IDE 不支持智能断言或持久断点,这里是简单的实现 (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.
    }

优点:

  1. 代码编译并运行,
  2. 当且仅当开发人员在测试期间遇到您的代码时,他才会收到有关未实现内容的消息,因此他不会被
  3. 消息指向相关代码(而不是异常捕获块或其他内容)的不必要信息所淹没。调用堆栈是可用的,因此可以追踪他调用未完成代码的位置。
  4. 开发人员收到消息后可以继续测试运行,而无需重新启动程序。

缺点:

  1. 要禁用消息,必须注释掉一行代码。这样的更改可能会潜入提交中。

PS 最初的 DEBUG_ASSERT 实现的积分归我的同事 EG 所有

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.

稀香 2024-09-17 17:06:53

您可以对继承的类使用纯虚函数 (= 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文