19
votes
Accepted
Is using C/C++ macros as a shortcut for conditional compilation a good practice?
Sure, if you're OK with using macros in the first place, then defining a parametrized one rather than keep repeating the same conditional code is certainly preferable by any measure of good coding.
...
12
votes
Is updating a macro value in the Xcode preprocessor's macros violating the open–closed principle?
If your requirements have permanently changed, just change the code already. Anything else - including slavish devotion to the open-closed principle - is abstraction for abstraction's sake and does ...
11
votes
Is this using macros to define classes that fit a pattern in C++ a sound idea?
I currently work on a code base that has classes created with these types of macros. I would strongly discourage doing things this way because if anything goes wrong in the future with any of the ...
8
votes
Accepted
Is this using macros to define classes that fit a pattern in C++ a sound idea?
I oppose to use macros instead of normal class here because:
It is harder to debug, especially when it has compile error
If the cost of creating a new class easily is harder to maintain later, I ...
7
votes
Is updating a macro value in the Xcode preprocessor's macros violating the open–closed principle?
First, make sure you have understood that the OCP is not violated at the time when you change some source code, see my answer here for a detailed explanation. The OCP is followed or violated at the ...
7
votes
Is this using macros to define classes that fit a pattern in C++ a sound idea?
Using macros for this will become a maintenance horror, as others have already pointed out. As an alternative approach, you could implement a small code generator for creating the repeated boilerplate ...
5
votes
Accepted
Using a macro for a libraries namespace?
This technique is sometimes used to handle transitive dependencies on header only libraries. It is not generally a best practice.
The problem: I am writing a C++ library. I put all of my declarations ...
4
votes
Accepted
Declaring functions using macros?
The way macros are used makes it very difficult for a programmer to reason about them, and guess what would be the result of a macro. For anything but the most basic examples, macros would usually ...
4
votes
Accepted
Using a preprocessor macro to wrap functions?
Does this work? Yes, probably. By using variadic macros, the usual errors about commas in the arguments will not occur. However, this approach might not be very good for debuggability – this will ...
4
votes
Current industry standard with regards to C macros
You see an absolute rule, and things don't work that way. You also seem to be focussed on macros vs. inline functions which is very rarely the question.
Macros can be extremely useful if you know how ...
3
votes
Detecting keyboard "callbacks" directly from a keyboard not from a operating system
In general, if you're not programming on the bare hardware, your application is talking to the operating system and has to believe what it is told, even if the OS claims "this is actually what the ...
3
votes
Python decorators and Lisp macros
A decorator (be it in Python or in any other - functional programming - language) is just a function which accepts an original (to-be-decorated) function (and sometimes more additional arguments), and ...
2
votes
Is using C/C++ macros as a shortcut for conditional compilation a good practice?
Definitely, just make sure you're not stepping on the code guidelines given by your team. Make sure no other code in the system tries to reach the same functionality via a general if condition.
1
vote
Accepted
Detecting keyboard "callbacks" directly from a keyboard not from a operating system
Generally, no.
Even if you're going to write a USB HID mouse/keyboard driver and your program somehow have the privilege to replace the default mouse/keyboard driver in the kernel, or even if your ...
1
vote
Is this using macros to define classes that fit a pattern in C++ a sound idea?
class MyClass :
public AbstractBase {
Q_OBJECT
You're not writing C++ code here, you're writing Qt code. Critically, this is code that is first processed by the Qt moc compiler. It expects to ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
macros × 45c × 17
lisp × 11
c++ × 10
clojure × 5
design × 2
programming-languages × 2
compiler × 2
language-design × 2
objective-c × 2
templates × 2
scheme × 2
meta-programming × 2
python × 1
programming-practices × 1
api-design × 1
version-control × 1
data-structures × 1
refactoring × 1
functional-programming × 1
naming × 1
code-quality × 1
coding-standards × 1
libraries × 1
history × 1