11

Why the actual fuck would you #define function calls into value-like things?!

Found when exploring the example code for some Chinese company's display module.

Comments
  • 1
    Example from their own code
  • 0
    Oh God why
  • 0
    #define eyes burning
  • 2
    @electrineer Sorry, Arduino IDE has this by default and I'd installed it only to view this code so didn't bother customising anything.
  • 1
    I see what they are doing... I get it... but I don’t understand why, other than abstraction for readability... doesn’t affect performance at all due to it being pre processor... but if you were going to abstract it for readability i would think you’d make it more verbose
  • 1
    Why not just macro everything to use one letter?
  • 1
    In this specific case: No idea.

    Otherwise: Look at OpenSSL. Many of the backward compatible functionally is just implemented as a Macro over the newer functions.
  • 1
    @irene because contrast hurts
    I don't subscribe to the whole dark theme is l33t thing but a lot of people run devRant in dark mode and therefore my screenshot would hurt.
  • 4
    Actually is not a very bad thing. Makes the code portable for various microcontrollers/boards/paltaforms.
    For example, in Arduino you can turn on a pin with digitalWrite(n, 1) but if you are using lower level code you can do it with something like PORTN&=1<<n.
    But if you are using other MCU, for example an STM32 with HAL library you would use HAL_GPIO_Write(PORTN, n, 1).
    Makes sense to write a library where the header has the defines to the low level functions of the target MCU/board/plataform.
  • 0
    @LuxARTS I don't mind that. I actually regularly transfer code from.Arduino to STM32 (I was actually doing just that here), I use #define and other abstractions all the time. What I do mind is function calls being hidden in things that look like values.

    I mean, I would expect LCD_DC_0 to be some sort of value that I can throw into a function somewhere or do something else with. Instead it's actually a function call with side effects. If they'd instead abstracted it into a function-like thing, say, SET_LCD_DC_LOW(); now that makes sense.

    Compare:
    LCD_DC_0;
    SET_LCD_DC_LOW();

    The first one looks similar to this:
    int x;
    x; //means nothing

    The second looks like a function call that does something, no confusion here.
  • 1
    It’s not uncommon to find these exploring C code. It’s even better when something is defined and it looks like a built in 🤦🏻‍♂️
Add Comment