Skip to content
loading...

re: Are C macros awesome? VIEW POST

FULL DISCUSSION
 

C macros are kind of like dynamic typing, aren't they

Perhaps, rather it is about templates in C++

I starting wondering, are macros a really awesome

I won't be surprised if most people consider them the worst language feature. C was created in very ancient time when using preprocessor was justified. Since then languages have got more advanced mechanisms.

Main problem is they allow to make the total mess of code. I met constructions like this in production sources:

#ifdef SOMEBLA
while (some condition) {
    doSomethingA();
#else
if (other condition) {
    doSomethingB();
#endif
    doSomethingC();
}

this is quite unreadable and poorly comprehensible especially when fragments take dozens of lines. And we read code for more time than we spent on writing it.

Another problem is macros only work at pre-compile time. No way to affect anything at run-time, so while they are devastating weapon to entangle and obfuscate your code, on the other hand they still lack much of power...

 

I've definitely seen macros used to create unreadable code. But don't they provide a lot of power when used well? For example, I don't think there's any way to do what I did in Go. You can use reflection and interface{} maybe, but I'm pretty it would come out to much longer and less readable. Maybe it wouldn't be needed in a language with generics, but I wonder if there aren't some really useful things other static languages coul gain.

I can see how dynamic languages like Python and Javascript wouldn't gain anything from macros. I guess I don't know very many static languages, just Go and C.

Perhaps, rather it is about templates in C++

Man, I always miss so much in these discussions from not knowing C++ :P But everyone seems to think it's horrible, so I'm averse to learning it.

 

For example, I don't think there's any way to do what I did in Go.

Well, it is hard to judge without specific example :)

But both C and Go are intentionally limited in some ways. So of course for them macros feel useful. However Go designers preferred to go limited but not use macros (of course one can add some macros preprocessor before Go compiler anyway).

Languages which compile to bytecode like Java and C# take middle place between scripting and compiling to native code. So usually there is no sense in macros.

I remember Scala (Java-based language) was going to introduce macros. Many people were alarmed. Probably this idea have died in peace - I don't know.

Man, I always miss so much in these discussions from not knowing C++ :P But everyone seems to think it's horrible, so I'm averse to learning it.

I won't say it's horrible, but it become so overcomplicated over years, so many (like me) prefer Java, Go or something like this.

Nevertheless it makes sense to get acquainted with C++ at "non-professional" level to know better what magics are implemented in it nowadays :)

Concluding it's good to have macros in C. But it is also good we don't have them in many other languages :) just my humble opinion, of course.

code of conduct - report abuse