4 ms·
May I ask why? It doesn't look quite terrible to me (as much as I can judge out of context).
by simias 11y ago
May I ask why? It doesn't look quite terrible to me (as much as I can judge out of context).
- pjmlp 11y agoIt doesn't express the real intention of the code. Uses C implicit conversions between pointers, integral and boolean operators for something that would be clearly better expressed with an if (). This type of code wouldn't survive the type of code reviews I used to do.
- andrewchambers 11y agoexcept it is actually fairly common and once you have seen it once it is really clear.
- pjmlp 11y agoFairly common by developers that don't have to maintain code from others. Clarity trumps clever tricks. EDIT: To make it clear, if I am reviewing someone's C code, I'll be applying MISRA and Code Complete style rules.
- andrewchambers 11y agoI first saw this trick in the qemu source code.
- pjmlp 11y agoSo what?! How is qemu an example of C code quality?
- andrewchambers 11y agoI didn't say it was quality, but it is someone else's.
- DanWaterworth 11y agoI agree, is: servo = dlsym(NULL, "servo_test") != NULL; ... really too onerous?
- Dylan16807 11y agoDo you use ++? I don't see !! as any weirder.
- pjmlp 11y ago++ is an operator, !! isn't one. Also not all uses of ++ are accepted, specially the typical memcpy() interview question, which requires to know operator precedence rules between * and ++.
- Dylan16807 11y ago!! gets weirdness points for not being a builtin operator, and ++ gets weirdness points for the subtleties of how it acts on certain things, and for being different in prefix and postfix. I assume that's a yes to allowing ++ at all. In my opinion ++ generally does a good job of conveying intent, and !! does a very good job of conveying intent.
- pjmlp 11y ago++ and -- are allowed as: - single line statements always in postfix form - for loops - exception for the prefix form in for loops with C++ iterators due to performance reasons Clever C code has its place in hackatons, not in code that needs to survive a few project generations.
- Dylan16807 11y ago> - exception for the prefix form in for loops with C++ iterators due to performance reasons That is a reasonable thing to allow, but I personally find that particular use much more confusing and finicky than using !! to coerce to boolean.
- Manishearth 11y agoBut "expressing the real intention" isn't something you can objectively quantify. It all depends on what people read stuff like `!!` as. In a project where that's used often (I assume in CEF there are a whole lot more lines like that one near it), the intention is clear, because people will be reading `!!` as "Coerce to bool". Some won't, agreed. But in the context of a project where it's used often, this isn't as much of a problem. For example, `fork()` when used is not clear at all about what's going on to someone not familiar with it, but people still use it for stuff, and people reading it know what the intention was if they've had some experience with the language/library. After lots of use things like `!!` get a concrete intention ascribed to them. But yes, it can be more explicit so it perhaps should have been.