Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One thing is wrong with it: a large part of C developers either doesn't know it exists, or doesn't know what it does.

… which I'd fix by using it more :D



It doesn't exist in C. It's solely a compiler extension. Include guards get the same performance benefit as #pragma once on compilers that support it. There's no point in embracing laziness with no upside.


There is an upside: complex header files often contain a bunch of #ifdef … #endif blocks, especially when dealing with cross-platform support. Since indentation is generally not used there, they can be rather hard to read, especially when nested.

Not having the include guard use the same mechanism makes these a bit easier to read and removes a little bit of cognitive load. (And every bit of cognitive load counts since that's a very hard limit of people's brains.)

Personally (and for the projects I work on), I couldn't care less whether it exists in ISO C or not. I have a list of compilers and platforms I want/need to support. But of course your project may be different.


> Since indentation is generally not used there,

Then indent your macros for readability. clang-format can do it for you if you don't want to maintain them.


Thanks but I'm not asking for a better way to handle #ifdef's, indenting them is extra work¹ and there's still an unnecessary outermost block for the include guard. You've also not made any further argument against the use of "#pragma once".

¹ especially across an open source project with more than a hundred contributors from a multitude of companies.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: