167 points · by deepakkarki · 10y ago · 65 comments
As someone who doesn't work in C++ or have the benefit of 10 years experience to see the flaws, trying to write modern, safe C++ is essentially impossible. I spent a solid week trying to write something safe and only use C++14/17 features when they were available, and encountered a mountain of outdated- and mis-information about what one can and can't do, what is and isn't considered safe and why one certain feature is better than another.
It's nobody's fault that this happens, but with C++14 apparently fast becoming a language that is addressing it's #1 pitfall (safety) in an apparently very adequate way, it's frustrating that there's no "safe code" linter to stop rookies stepping on landmines.
Thanks for the list, it's a solid start, and the sibling comment about C++ Core Guidelines is another solid resource.
http://clang.llvm.org/extra/clang-tidy/ is much more approachable, and gives a solid overview.
The introductory sentence gave me project-naming cancer:
> clang-tidy is a clang-based C++ “linter” tool.
Then why is it called tidy ??? It appears first on page three of google's search results for "C++ linter" in spite of the fact that anything from LLVM is probably a) current and b) of exceptional quality.
A little marketing-mindset would go a long, long way for these kinds of things.
Whilst the Ruby community lead by the example of DHH often takes this concept too far, it is inarguably effective.
Besides the clang-tidy, there is the ongoing efforts to improve VS analyzers, but I guess they will only be fully productized on VS "15", the upcoming release.
I also imagine other C++ linters are improving their C++ Core Guidelines support.
Under C++ Core Guidelines you are supposed to use [[unsafe]] annotation to mark such features.
Am I the only one who uses C++ as C with classes? Sometimes I use vectors or strings and I like default values in functions and other minor improvements to C. I don't want to reimplement the n'th version of string concatenation when I can just use the "+" operator, sure... and there starts the rabbit hole. Before you know it you have a "protected abstract virtual base pure virtual private destructor" and have to explain the new hired mathematician who only has experience in R what the fuck you are doing.
I'd say unfortunately not. Focussing on the classes has the disadvantge it can quickly lead to 'everything is an object' or derived strict OOP mindsets thereby not only ignoring tons of other, more functional, goodies modern c++ has to offer but also leading to what you call 'protected abstract virtual base pure virtual private destructor'.
For that matter, I'd argue that this is a reasonable approach for every language.
"I remember from a symposium on higher level programming language a lecture given in defense of PL/1 by a man who described himself as one of its devoted users. But within a one-hour lecture in praise of PL/1. he managed to ask for the addition of about fifty new “features”, little supposing that the main source of his problems could very well be that it contained already far too many “features”. The speaker displayed all the depressing symptoms of addiction, reduced as he was to the state of mental stagnation in which he could only ask for more, more, more..."
https://www.cs.utexas.edu/~EWD/transcriptions/EWD03xx/EWD340...
Yes C++ is complex, but so are Python, Ruby, Ada, Fortran, C#, F#, Haskell, OCaml, Java...
Even Go will get caught in the complexity game if it gets enterprise adoption at scale.
If not the language, the libraries surely will.
Its not a "complexity game" in so much as poor design being over-compensated for. C++ is like cancer infecting the minds of young programmers with buggy habits -- and no amount of features and static analysis will change what lies beneath... :p
The only thing Ada has thrown away was GC support that no compiler vendor has ever bothered to implement.
What other features were thrown away across Ada83, Ada95, Ada2005 and Ada2012?
If I present you Ada random code snippet X, are you sure you will be able to say which language version it requires?
Also Python is the poster child of what happens when you throw compatibility away.
[1] https://github.com/isocpp/CppCoreGuidelines
[2] https://reviews.llvm.org/diffusion/L/browse/clang-tools-extr...
Nim's syntax is mostly pythonic, and it's metaproramming language is Nim (unlike C++'s abominable template metaprogramming language).
If you want it to be safe, you will basically be reinventing Rust.
Rust would be an obvious choice for what I described. It's been on my personal list for a while to learn Rust and make use of it. I think I will pretty soon.
I've been demotivated (to start) by reading over here on HN or elsewhere about how much of a learning curve it is to learn Rust. Perhaps, it was the bias that I already know what C++ was like in terms of syntax and idioms that made me ask for a choice with least friction.
That learning curve is necessary for safety. If you want a safe C++-like language without Rust's learning curve, you either (1) have a brilliant solution nobody has come up with yet or (2) don't actually want a safe language after all.
I'm mostly a front-end software engineer and I got along just fine with Rust.
> Make it's syntax as simple and easy to learn as Python
I've been in the situation many times where the compiler knows about a feature but the auto complete doesn't and the static analyzer will totally fail early and not report anything else except syntax error.
The things I dislike most are pairs and tuples. They make the code enormously harder to read, understand and debug, compared to non-standard classes or structures with meaningful member names. Even this guide promotes them in “using Coordinate = std::pair<int, int>;” Please, never do that, create your own class with x/y or latitude/longitude instead.
If you use the members a lot, a struct with named members really is much easier to read, though.
auto [a, b] = function_returning_pair();
Of course, tuple unpacking was already possible before if the variables were already declared: int a, b;
std::tie(a, b) = function_returning_pair();The author named their pair "Coordinate". In math and in computer graphics there’s widely known convention the fields of Cartesian coordinates are named x/y and not a/b.
When you’ll start writing code that use and/or modify those Coordinate values, meaningful names in C++ source and in the debugger help a lot.