10 ms·
Breaking all the Eggs in C++
- kartikkumar 11y agoI completely agree. Not being an expert in C++ but someone that uses it almost daily, it's so incredibly frustrating to have to constantly battle the fact that for every choice, there are a million ways of doing it, and they're not all equal, but you only know that if you're an expert, or discover it when you fall flat on your face months later and have to refactor. Feels like I'm constantly shopping and left to figure out which deals suck and which are gems. Break all the eggs! C++ could be so much more effective if the backwards compatibility requirement could be overridden on the most basic of stuff that's still part of the language even though consensus, i.e., best practice, was to drop it out years ago.
- santaclaus 11y agoI'm surprised to see no mention of the Sutter and Stroustrup core guidelines [1]. Wouldn't a statically checkable implementation of these ameliorate most of Meyer's concerns? [1] https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC...
- something123 11y agoYou should watch the cppcon2015 keynotes https://channel9.msdn.com/Events/CPP/CppCon-2015/Writing-Good-C-14 https://channel9.msdn.com/Events/CPP/CppCon-2015/Writing-Goo... static checking of the core guidelines is the entire reason they are writing them. This isn't just a nice literary exercise The static checker is found here (obviously still under development) https://github.com/Microsoft/GSL https://github.com/Microsoft/GSL
- cjensen 11y agoThe problem is that this messes up version control history. For example, I work on a code base which is 20 years old. It's nice to use 'svn blame' and to get the log message associated with a given line of code and give me insight into what I was thinking. That said, it's not an insurmountable problem. Usually I'm looking at a particular line, and if history says it was touched in a compiler cleanup then I just have to click a few more times to get 'svn blame' of the version preceding the cleanup.
- makecheck 11y agoArguably this is a feature revision control systems should have; a kind of "trivial change" marker to filter certain annotation/log entries by default.
- humanrebar 11y agoYou should probably do this at inspection time, not commit time. Maybe a list of commits from the command line,or a commit ignore file.
- steven2012 11y agoThe thing I hate most about C/C++ is how they overload the same keywords to mean different things. So the idea of: int x = void; is inherently wrong to me. Why not just create a new keyword with a clear and explicit meaning? That said, breaking changes are simply wrong. C/C++ is what it is, stop trying to make it into Java, Go or whatever. Trying to change it into a language that it's not is simply going to cause confusion and chaos, and kill the language. The things Meyer points out are all correct, but just because it's right doesn't mean it's right for the C++ community. Every language has it's own pluses and minuses and thinking you can squash all problems in all languages and make it "perfect" is a losing battle.
- corysama 11y ago> Why not just create a new keyword with a clear and explicit meaning? That said, breaking changes are simply wrong. Ummm... What is your suggestion for a new keyword that would not be a breaking change?
- theseoafs 11y agoI think the point that they're trying to make is that it's best to use a non-overloaded word for this kind of thing, but that that ship has sailed since C++ has been around for 30 years or whatever. So making no change is preferable.
- mikeash 11y agoThe typical way to do this is to create a new keyword that starts with __, or _ and a capital letter. This is not a breaking change because this space is reserved for the language and implementations. You can then add a header that #defines something less ugly to these new keywords.
- marvy 11y agoLike what C did with bool? Ick. Gross. Ugly. Me no like. I understand why they did that in this case: by the time they got around to fixing the lack of bool, they had to work around everyone else's workarounds! But that was an extreme case. Let's not do that more often than we must.
- yoodenvranx 11y agoThis highlights a problem I see with stackoverflow (and also google in general) more and more often: In the beginning of stackoverflow all of its content was up-to-date but now that it is running for a few years a lot of stuff is out of date. If you google for "c++ pointer 0 or NULL" you get this page from 2008 as the top result: http://stackoverflow.com/questions/176989/do-you-use-null-or-0-zero-for-pointers-in-c http://stackoverflow.com/questions/176989/do-you-use-null-or... With C++11/17 these answers are now out of date but this pagr will continue to be at the top of google for the forseeable future. And exactly the same happens for all other technical stuff as well... I learn Javascript at the moment and at least 50% of all solutions I find on stackoverflow and other pages are heavily out of date. And this problem will get worse in the next 10 years. Perhaps it is time to go back to curated link directories? (I hope it is clear what I want to say, I am on mobile and busy with other stuff)
- chc 11y agoI think the initial idea was that if somebody later came along and found some stale information, they could go in and fix it, the same way you do with Wikipedia. Unfortunately, this vision of the site is at odds with the way Stack Overflow actually works, where each question has multiple answers that are upvoted, and one gets an "Accepted" checkmark from the asker. The solution that feels right is adding a new answer rather than stomping on somebody else's, but it seems unlikely that the new answer will get a lot of upvotes and even less likely that the asker will come back and reassign the checkmark. So a lot of more current answers are probably abandoned before they're even posted, and those that are posted end up buried (like the correct answer to that question, which is posted there but is currently languishing at 4 upvotes). Personally, I say that if you feel like an answer is outdated, you should either comment with a polite correction if you'd like the author to fix it or just edit in the updated information yourself. (This nets less karma than answering new questions, but I can say from personal experience that 150k karma and a dollar will buy you a pack of gum.)
- kzrdude 11y agoThere was a bitcoin faucet that gave out 0.2 btc for stack overflow rep over a threshold. I've got a lot of gum now!
- ufo 11y agoC++ is so full of dark corners due to its history and backwards compatibility requirements (both to itself and to C) that I'm not sure whether breaking backwards compatibility to solve just a handful of issues is worth the trouble.
- bkjelden 11y agoThe problem with a release that breaks backwards compatibility is that no one would use it. Very few new projects are written in C++. It's the old products with 1M+ LOC codebases that most C++ devs live in.
- vilya 11y agoYou're right with your first point, but wrong with the second: a lot of new projects are still written in c++. Pretty much every non-indie video game, for example.
- such_a_casual 11y ago>Pretty much every non-indie video game, for example. So code that relies on a lot of existing software then (ie the game engine and other in-house libraries that any company would have). You have proved his point instead of countering it.
- okatsu 11y agoAny large enough game will use C/C++ at its core because short of using assembly, it's the only real choice we have. C#, Lisp or whatever else are nice for gameplay scripting, but underneath there is a need for control that most languages will not give you. Same thing for anything with hard real-time requirements.
- such_a_casual 11y agoSo? That's completely irrelevant to the argument.
- ryandrake 11y agoI like the proposed ideas, but removing things and making breaking changes might be going too far. Compilers should be smart enough to help. I wish g++ and clang had a "-best-practices" compiler flag: -Wall is not enough. But since you will never get everyone to agree on what subset of C++ is best practice, and what dark corners should be avoided, maybe a set of -Wbest-practice-this and -Wbest-practice-that flags that allow you to pick and choose. Warn yourself when you do things that are not warning-worthy but you nonetheless want to avoid. EDIT: For examples, I'd love to have -Wold-style-cast on all of my compilers. The nullptr example in the article is good too, maybe call it -Wold-literal-null? It would be nice if my compilers could give me hints when std::move might be appropriate, if I might be missing a copy or move ctor, or when I could consider using initialization instead of assignment for members. Maybe this is more of a job for the static analyzer and not the compiler--dunno.
- ori_b 11y agoGCC has a '-Weffc++' option, which claims to do roughly what you want. Although, it's no doubt using obsolete guidelines these days, since it aims to match Scott Meyers' guidelines from his book. (Yep, same Scott Meyers that wrote this blog post.)
- pmelendez 11y agoIt would be nice if gcc would update the guidelines too, the new effective modern c++ book is very nice
- lfowles 11y agohttps://github.com/isocpp/CppCoreGuidelines https://github.com/isocpp/CppCoreGuidelines Like this? "The rules are designed to be supported by an analysis tool. Violations of rules will be flagged with references (or links) to the relevant rule. We do not expect you to memorize all the rules before trying to write code."
- something123 11y agohere is the tool https://github.com/Microsoft/GSL https://github.com/Microsoft/GSL Introduced in this year's cppcon https://channel9.msdn.com/Events/CPP/CppCon-2015/Writing-Good-C14-By-Default?ocid=SessionsInEvent https://channel9.msdn.com/Events/CPP/CppCon-2015/Writing-Goo...
- ArkyBeagle 11y agoIf NULL is bad, simply invest NULL with the same ... behavior? semantics? as nullptr. I cannot for the life of me help but think that this is simply a distinction without a difference. I work in the embedded space, and in each case when I've used C++ instead of 'C', me and at least one other senior ( and I mean age senior - +40 ) guy end up bemoaning that if we'd just had a 2-hour meeting on which subset of 'C' we'd have used, it would have taken half the time. Oh so obviously, everybody has a different experience, and celebrate the diversity and all that, but I am really happy that I can always fall back on good old 'C' and get something done. EDIT: s/invert/invest
- aidenn0 11y agoEmbedded is about 20 years behind the curve with regards to C++ best practices.
- magila 11y agoWorked for 6 years writing embedded C++, can confirm. The industry is dominated by stodgy old devs who still think C++ means C with classes.
- aidenn0 11y agoWell a lot of late '90s non-embedded C++ style was a very poor fit for embedded; everything dynamically allocated, lots of templates.
- ArkyBeagle 11y agoIt wasn't that bad.
- okatsu 11y agoC with classes is often frowned upon but I'm actually okay with it. It keeps things simple while still allowing for a few benefits. I think the Mars Rover project is a good example of that.
- 11y ago
- MaulingMonkey 11y agoPython 3 broke backwards compatibility and everyone stayed on 2 as a result. Why is breaking backwards compatibility "for the hell of it" in C++ not going to have the same problem? Codebases stay in stasis, stuck on older compilers, for far less drastic reasons. And I've found that modernizing a codebase is a lot easier if you can compile it to test your incremental changes. Talking about a magic wand that makes source-to-source changes on a 100MLOC codebase trivial is pure fantasy - requiring you to ignore that even in the best version control systems, the resulting ~10MLOC merge is going to cause a lot of merge conflicts (some of which will be incorrectly resolved) and history clutter. To say nothing of all the poor bastards who are satisfied with the occasional tarball per release. Who are still doing better than the people who can't be bothered to backup the code they wrote directly on the server it will run on. And nevermind the fact that at least some of your downstream customers are still using some old compiler that they're stuck with - the only option for some legacy embedded platform they can't eliminate yet, made by that company that went out of business. And maybe that kind of cost would be worth it - for the right reasons, given the right motivations. But I feel like I'm looking at a laundry list of mostly minor, tiny issues, that are a "solved problem" in the form of existing warnings. I can crank them up to errors as I modernize a codebase. You can enable them by default in your build systems and IDEs. You can mandate them across your organization, and in your contracts. You can incrementally apply them per library as you get around to doing more maintenance on them.
- makecheck 11y agoHad Python 3 stuck to relatively simple changes like converting to the print() function, making a few libraries more consistent, etc. then it would be closer to the C++ compatibility-breaking changes proposed here. Instead, Python 3 went far beyond things that were easily convertible and it isn't surprising that adoption was greatly slowed. C++ has atrocious defaults, where you have to add a bunch of keywords and do all these things just to make the simplest classes do the "right" thing. It's long overdue for some new behaviors. Adding things like "override" to existing code has been an absolute joy. It has already prevented errors during maintenance in projects at work. It is well worth a few days of ugly revision control logs. There are lots of ways to smooth a backward-incompatible transition for C++ on a larger scale. For instance, they could enable it on a file-by-file basis by having some new declaration like "using c++17;" (or better still, require only the old, out-of-date files to add this declaration in order to continue compiling).
- overgard 11y agoI don't get the urge for this -- its basically obsessing over other people's sloppiness. Make it a warning -- if you care about code quality you have warnings as errors anyway -- and you don't need to break everything in the process.
- cpeterso 11y agogcc and clang have optional warnings for some of these issues, without needing to use clang-tidy. For NULL, 0, and nullptr, gcc and clang already treat NULL as a magic __null value and -Wconversion-null (enabled by default) can warn about using NULL as a non-pointer value. gcc also can break 0-as-a-null-pointer with the -Wzero-as-null-pointer-constant flag. I had hoped to fix these warnings in Firefox (https://bugzilla.mozilla.org/show_bug.cgi?id=777515 https://bugzilla.mozilla.org/show_bug.cgi?id=777515) bug there were thousands of warnings because gcc warns about using NULL, not just 0. For override, gcc has -Wsuggest-override and -Woverloaded-virtual warnings that may help here. clang has -Winconsistent-missing-override. For uninitialized variables, gcc and clang have -Wuninitialized. However, gcc -Wuninitialized has historically reported a lot of false positives. Fortunately, recent gcc version moved the flakier uninitialized warnings to a new -Wmaybe-uninitialized flag. In my experience with Firefox, -Wmaybe-uninitialized warnings are false positives about 90% of the time. On the other hand, clang has a -Wsometimes-uninitialized which is reliable. For bool++, clang has -Wdeprecated-increment-bool. btw, clang does not document its warning flags, but some resources I've used are the http://fuckingclangwarnings.com/ http://fuckingclangwarnings.com/ reference, searching through clang's source code at https://github.com/llvm-mirror/clang/blob/master/include/clang/Basic/DiagnosticGroups.td https://github.com/llvm-mirror/clang/blob/master/include/cla..., and clang's -Weverything flag (which truly is everything, including contradictory warnings).
- akavel 11y agoAs much as I like the premise of the article, I see two serious problems, that would need to be resolved for the "magic wand" tool to be useful, and it's not very obvious for me how: 1. What about templates? If there's a "template<T> void foo(x &T)", and it does "x++" inside, and I sometimes use it on int, sometimes on bool, how would the magic wand upgrade only the bool cases to "x = true"? (And please don't tell me "then you have borked codebase, u go fix it, man!" In C++, you have codebase you have, sometimes decades of legacy code you hope to upgrade bit by bit "when time, boss and priorities permit".) Similarly, if the template does "x = 0;", how could the magic wand upgrade only the cases where T is a pointer, to "x = nullptr;"? 2. One more important, yet not mentioned feature of the magic wand tool, would be that it'd have to be flawless! If the tool has bugs, it could break my codebase without me knowing, till it's too late. Especially tricky here is how would the tool behave on code with undefined behavior; after years of working with C++, I have a nagging suspicion that virtually every sizable C++ codebase does actually have some undefined behavior lurking somewhere.
- eru 11y ago> [...] I have a nagging suspicion that virtually every sizable C++ codebase does actually have some undefined behavior lurking somewhere. You are being too optimistic. Any C++ code base over at most a couple hundred very carefully crafted lines will have undefined behaviour and implementation specified behaviour.
- fauigerzigerk 11y ago>1. What about templates? If there's a "template<T> void foo(x &T)", and it does "x++" inside, and I sometimes use it on int, sometimes on bool, how would the magic wand upgrade only the bool cases to "x = true"? Generate a new template specialization or overload? I realize this could be rather questionable if the offending operator use is somewhere in the middle of other code.
- eru 11y agoActually, all nullable variables should be explicitly tagged as Option values.