0

I'm not asking if it's possible to #define a macro inside a function. I understand that it is possible and that macros are preprocessing mechanisms that are almost copy-paste (to an extent). What I'm asking is are there broad reasons to avoid making a macro for use in a single function or is it simply up to the codebase maintainers as to what they decide is good style?

Here is a contrived example similar to my usecase:

int main() {
   /* ... */
   
   #define POS(x, y) grid_array[width*(y) + (x)]
   
   /* Use POS(x, y) in this function */
   
   #undef POS
   
   /* ... */
}

Another way to put the question is: Would other C developers nod their head in understanding or shake their head in distain?

Edit:

This is not a duplicate of the question "Macro vs function." I understand (some of) the differences between the two, e.g. MIN(a, b) (a)>(b)?(b):(a) evaluates a and b twice. I'm asking if it is good practice to use a macro for a single function.

The answer (and comments) pointed out that my simple example doesn't merit using a macro. Though my actual use case is not that simple, I have to agree. The "saved typing" doesn't merit convoluted code.

Here's my actual use case if you were curious:

/* array represents a graph with max degree 2. These operations are domain specific. */
#define CONNECT(a, b, i) {array[8*(a) + i] = b; array[8*(b) + (i+4)%8] = a;}
Noah May
  • 1,049
  • 8
  • 18
  • 1
    My opinion is that for a simple example like this it makes the code less readable. – Retired Ninja Dec 24 '20 at 07:26
  • Somewhat related C++ thread (does not apply to C, because C does not support `constexpr`): [Constexpr vs macros](https://stackoverflow.com/questions/42388077/constexpr-vs-macros) – Andreas Wenzel Dec 24 '20 at 07:37
  • 3
    @AndreasWenzel but more modern and effective languages have much better constructs for this. C++ is a convoluted mess of half thought out concepts implemented half well. The only thing to learn from it is what not to do. imho. – mevets Dec 24 '20 at 08:00
  • Does this answer your question? [Macro vs Function in C](https://stackoverflow.com/questions/9104568/macro-vs-function-in-c) – Ulrich Eckhardt Dec 24 '20 at 12:30
  • Not quite @UlrichEckhardt. I understand (at least most of) the differences between macros and functions. What I'm asking is if it's reasonable to define a macro that is only used in one function. Thanks though :) – Noah May Dec 24 '20 at 14:13
  • 1
    Gcc supports nested functions in C. Those may be handy alternative for POS-like macros – tstanisl Dec 25 '20 at 21:01

1 Answers1

3

The primary purpose of any programming language, outside of just hex values, is to make the intent and implementation more obvious to the reader. Programs are meant to be read by people, and translated by machines.

To that extent, if POS(x,y) merely saves you typing, that might be of value to the reader, but you should consider the reader more than the writer. Any worthwhile program will have many more readers than writers, so your obligation as a writer is to lessen their load.

mevets
  • 10,070
  • 1
  • 21
  • 33
  • 2
    "that might be of value to the reader" -- I think you mean "writer", not "reader". – Andreas Wenzel Dec 24 '20 at 07:34
  • 1
    No, abbreviations aid the writer in reducing keystrokes, but may aid reader by reducing the cognitive load. That is why notations like ++ or += are important; they lessen the cognitive load reading the program, especially in the modern context of way over indicating the purpose of each variable. This is the tension between writing for the novice, who presumably doesn't understand the language, and the fluent who is annoyed by verbiage. – mevets Dec 24 '20 at 07:48
  • 2
    I can follow both reasonings, the one by Andreas and by mevets. But the answer would probably benefit if you (mevets) phrase to reduce the cognitive load for StackOverflow users to appreciate both interpretations. (Not making fun of you, I intentionally use your expression because I actually agree with the concept and the way you put it.) But you already have my upvote (for neither saying yes nor no, which I accept as an answer to what I first considered an opinion-based question), so I am afraid you won't get another one from me, even if you do as I propose. ;-) – Yunnosch Dec 24 '20 at 14:43