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

As a long-time C++ developer I wish people don't use any commenting tricks such as this, and I wish people don't use /* * / to comment their code, even if you develop in C. Instead, use // in modern C/C++.

The reason is that we often need to comment OUT code during development. In C++, the 4 choices are: (1) #ifdef, (2) bool dothis = false; if (dothis) { ... }, (3) /* * /, (4) // ...

Among them, I would say the best and least-confusing way to comment out a block of code is /* * /. #ifdef's are very confusing especially if you have multiple of them; // needs to be applied to every line so only works in small scale; if (dothis) doesn't work across multiple functions, and at a glance it is harder to discern whether this is a "comment-out", or a legit condition.

However, by using /* * / in your comments, you eliminate the possibility of commenting out blocks of code that contains /* * / comments. The problem, of course, is due to how the opening and closing symbols are matched.



"I'd appreciate if you'd have this discussion on Coderwall. That way I could 'defend' the post where it's due. However though, please read https://news.ycombinator.com/item?id=5626456 and summary is: It is a trick, not a silver bullet. It prevents code from being compiled to the binary, in a portable - yet hacky - way."


I've adopted:

#if 0 /* reason /

lines of messy stuff ...

#endif / reason */

That short comment makes it easy for me to find the ends.


Ha, I thought I put in the asterix-esses. You know what I mean, but I bet cpp actually ignores e everything after #if 0 and #endif, don't do that please ;)


Some C projects still use -ansi and thus C++ comments (//) will result in a compiler error.




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

Search: