简体   繁体   English

C-Eclipse CDT-有效的调试+什么是更好的代码(指向函数的指针)?

[英]C - Eclipse CDT -Efficient debugging + What is better code (pointers to functions)?

I'm a new C programmer and I'm writing some data structures for homework. 我是一名新的C程序员,正在为家庭作业编写一些数据结构。 I have two questions here. 我在这里有两个问题。

  1. We see a lot of examples of C's function-pointers, usually used to save code duplication. 我们看到了很多C函数指针的示例,通常用于保存代码重复。 I messed around with this function, which I initially wrote: 我弄乱了这个函数,最初是我写的:

(The constants we're pre #defined. Indentation is off, too). (我们预先定义了常量。缩进也已关闭)。

    static PlayerResult playerCheckArguments(const char* name, int age,
    int attack, int defense) {
    PlayerResult result = PLAYER_SUCCESS;
    if (!name) {
    result =  PLAYER_NULL_ARGUMENT;
    } else if (strlen(name) > PLAYER_MAX_NAME_LENGTH) {
    result =  PLAYER_NAME_TOO_LONG;
    } else if (invalidAge(age)) {
    result = PLAYER_INVALID_AGE;
    } else if (invalidAttack(attack)) {
    result = PLAYER_INVALID_ATTACK;
    } else if (invalidDefense(defense)) {
    result =  PLAYER_INVALID_DEFENSE;
    }
    return result;
}

until I got this ghoul: 直到我遇到这个食尸鬼:

    static PlayerResult playerCheckArguments(const char* name, int age, int attack,
    int defense) {
void* arguments[PLAYER_NUM_OF_PAREMETERS] = { name, &age, &attack, &defense };
PlayerResult (*funcArray[PLAYER_NUM_OF_PAREMETERS])(
        int) = {&invalidName, &invalidAge, &invalidAttack, &invalidDefense };
PlayerResult result = PLAYER_SUCCESS;
for (int i = 0;
        i < PLAYER_NUM_OF_PAREMETERS && result == PLAYER_SUCCESS; i++) {
    PlayerResult (*func)(int) = funcArray[i];
    void* key = arguments[i];
    result = func(key);
}
return result;

My first question being - is there any reason why I should use/write the second function over the other, and generally try to use such "sophistications" which obviously lessen the code's clarity and/or simplicity? 我的第一个问题是-我为什么应该使用/编写第二个函数而不是另一个函数,并且通常尝试使用这种“复杂性”,这显然会降低代码的清晰度和/或简单性?

now, for my second question: As you may have noticed, I am using a lot of local variables for the purpose of easier debugging. 现在,关于第二个问题:您可能已经注意到,为了方便调试,我使用了许多局部变量。 this way, I can see all relevant evaluations and efficiently monitor the program as it runs. 这样,我可以查看所有相关评估并在程序运行时对其进行有效监视。 Is there any other way to display expressions made in a function other than using local variables? 除了使用局部变量,还有其他方法可以显示函数中的表达式吗?

Thanks very much! 非常感谢!

return 0 ;-) 返回0 ;-)

Since your question is tagged coding style, I'll just say, the first is definitely preferred. 因为您的问题是标记的编码样式,所以我只能说,第一个绝对是首选。 The reason is simple. 原因很简单。 Show the two functions to 200 programmers, 100 see the first, 100 see the second, and then record the average time it takes for the programmers to be able to describe what the function does. 向200个程序员显示这两个功能,100个显示第一个,100个显示第二个,然后记录程序员能够描述该功能执行的平均时间。 you'll absolutely, averaged over hundreds of programmers, find that the first wins every time. 您绝对会平均超过数百名程序员,每次都会发现第一个胜利。

So you would only do the second if perhaps you had 20+ different parameters to check, and even then there are cleaner ways to do it. 因此,只有在可能要检查20多个不同参数的情况下,才执行第二个操作,即使那样,也存在更干净的方法来执行此操作。 I don't believe you'd see any speed increase for the second one either. 我认为第二个也不会提高速度。

Clarity is far more important than cleverness. 清晰远比聪明重要。 The harder it is to figure out the harder it is to get right, and to debug when you don't. 越难弄清楚正确和在不正确时进行调试的难度就越大。

There is nothing wrong with using local variables for clarity or debugging. 使用局部变量进行澄清或调试没有错。 There is an ole saw that goes "Avoid the sin of premature optimization". 有一张油锯看到“避免过早优化的罪过”。 Make your code as simple and as clear as you can. 使您的代码尽可能简单明了。 If you then find that isn't enough work to add as little complexity as needed to get the job done. 如果您发现这还不够,那么就可以增加完成工作所需的复杂度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM