简体   繁体   English

在函数中定义/使用宏 - 好的风格/做法?

[英]define / use a Macro inside a fcuntion - good style/practice?

I folks, I have a question for the C/C++ Syntax / Coding-Style Gurus out there:伙计们,我有一个关于 C/C++ 语法/编码风格大师的问题:

The situation: I am working on an STM32 Microcontroller and in some functions I have to do a lot of bit shifting, logical operations (and, or, xor) on bits, set/clear bits and writing / reading this code is really painfull.情况:我正在研究 STM32 微控制器,在某些功能中,我必须对位进行大量位移、逻辑运算(和、或、异或)、设置/清除位以及编写/读取此代码真的很痛苦。

An example: lets asume I had to turn an device on with a digital Output, and I have 5 sensors to check, where each of the sensors has its own dedicated digital Input.一个例子:假设我必须用数字输出打开设备,我有 5 个传感器要检查,每个传感器都有自己的专用数字输入。 Some of them need to be TRUE, others need to be FALSE其中一些需要为 TRUE,另一些需要为 FALSE

A (Pseudo-)Code would eventually look like this: Note: this code is obviously nonsense/nothing really functional, it's just for explaining the principle. (伪)代码最终看起来像这样: 注意:这段代码显然是胡说八道/没有真正的功能,它只是为了解释原理。

#define SENSOR1 0
#define SENSOR2 1
#define SENSOR3 2
#define SENSOR4 3
#define SENSOR5 4

void DeviceOn(void) {
   uint8_t DIOPort = ReadDIOPort();
   if((DIOPort & (1 << SENSOR1)) && (!(DIOPort & (1 << SENSOR2))) && (DIOPort & (1 << SENSOR3))) {
      turnOnDevice();
   } else {
      turnOffDevice();
   }
}

As you see, the if-condition becomes pretty ugly and if there are more signals involved it becomes more and more unreadable.如您所见,if 条件变得非常难看,如果涉及更多信号,它变得越来越难以阅读。

My idea was to define macros inside the function, only used by / within that function, to make the code more readable.我的想法是在函数内部定义宏,仅由 / 在该函数中使用,以使代码更具可读性。 This would look like this.这看起来像这样。

void DeviceOn(void) {
       #define __POWER_ISOK (DIOPort & (1 << SENSOR1))
       #define __SAFETY_ISOK (!(DIOPort & (1 << SENSOR2)))
       #define __LIGHT_ISON (DIOPort & (1 << SENSOR3))
       #define __COFFEEMUG_ISFULL (DIOPort & (1 << SENSOR4))
       uint8_t DIOPort = ReadDIOPort();
       if(__POWER_ISOK && __SAFETY_ISOK && __LIGHT_ISON && __COFFEEMUG_ISFULL) {
         turnOnDevice();
       } else {
         turnOffDevice();
       }
    }

IMHO the if-condition is much more readable as the first example.恕我直言,if 条件作为第一个示例更具可读性。

The question is: is this considered to be "good coding style" or is that an absoulte no-go and other developers will start to avoid me and consider me to be the worst coder in the whole universe?问题是:这是否被认为是“良好的编码风格”,还是绝对不行,其他开发人员会开始避开我并认为我是整个宇宙中最糟糕的编码人员?

I want the macros to be defined within the function, because they are ONLY used in this one function, and it makes documentation easier: the one who has to read this code just has to scroll up to the start of the function and does not have to search in the C-File or even the Header-File for the definition.我希望在函数中定义宏,因为它们仅在这一个函数中使用,并且它使文档更容易:必须阅读此代码的人只需向上滚动到函数的开头并且没有在 C 文件甚至头文件中搜索定义。 The Definitions of the bits in the Digital Input Status Variable are defined gobally (at the beginning of the C-File) as they are used in other functions, too.数字输入状态变量中的位定义是全局定义的(在 C 文件的开头),因为它们也用于其他功能。

Of course I'd define any macros gobally, that are used in multiple functions.当然,我会定义任何用于多个函数的宏。 Of course I could also define all macros gobally, but I think that it makes the code more readable, if those who are used only in one function, are defined in that function.当然,我也可以全局定义所有宏,但我认为如果只在一个函数中使用的宏在该函数中定义,它会使代码更具可读性。

Technically I don't see any issues, as the preprocessor should replace the macros in the build process, or am I wrong?从技术上讲,我没有看到任何问题,因为预处理器应该在构建过程中替换宏,还是我错了?

Marcos don't have scopes, and their definition won't end at the end of the function. Marcos 没有作用域,它们的定义不会在函数的末尾结束。

Defining macros inside of functions such that they remain defined after the function is a bad practice because programmers expect definitions to end when the scope ends.在函数内部定义宏以使其在函数之后保持定义是一种不好的做法,因为程序员希望定义在作用域结束时结束。 You could undefine the macros at the end of the function.您可以在函数末尾取消定义宏。 That is a good practice when the macros is needed in the first place which isn't often.当首先需要宏但并不经常使用时,这是一个很好的做法。

Unnecessary use of macros is a bad practice.不必要地使用宏是一种不好的做法。 In this case, it is a better practice to use constant variables instead.在这种情况下,最好改用常量变量。 Variables have a type system, and scope, both of which make it easier to write a correct program.变量具有类型系统和范围,这两者都使编写正确的程序变得更加容易。

PS Names that contain two consecutive underscores are reserved to the language implementation. PS 包含两个连续下划线的名称保留给语言实现。 Don't define them yourself.不要自己定义它们。

Alternate solution:替代解决方案:

#include <stdbool.h>

void DeviceOn(void)
{
    uint8_t DIOPort = ReadDIOPort();

    bool PowerIsOK       =    DIOPort & 1 << SENSOR1;
    bool SafetyIsOK      = ! (DIOPort & 1 << SENSOR2);
    bool LightIsOn       =    DIOPort & 1 << SENSOR3;
    bool CoffeeMugIsFull =    DIOPort & 1 << SENSOR4;

    if (PowerISOK && SafetyIsOK && LightIsOn && CoffeeMugIsFull)
        turnOnDevice();
    else
        turnOffDevice();
}

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

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