News for hackers. Stuff that matters.
Re:Recommendations about coding in C++ by tempodox on Sunday February 19, 2017 @02:42PM (#13681528)
Seeing that list of error patterns made me realize that letting mediocre programmers use something like C / C++ really boils down to Luck-Driven Programming. I hope that in the not too distant future something along the lines of Rust or Swift can help avoid at least some of those traps.
Re:Recommendations about coding in C++ by chris_wot on Sunday February 19, 2017 @02:48PM (#13681570)
There are quite a few errors in this article that could occur in Rust and Swift as well. Did you actually read this article?
Re:Recommendations about coding in C++ by tempodox on Monday February 20, 2017 @01:14AM (#13684770)
There is no silver bullet. And no language can stop you from making mistakes (assuming “at least some” means the same as “all” is one example). But some languages help you avoid certain classes of mistakes with automated checks. Like Rust.

And even if you added those features to C++, you couldn't guarantee a codebase that only uses the secure subset. Nobody has the time to rewrite old code. And making sure that current code uses only the secure subset would require code reviews. Not everyone does that.

Re:Recommendations about coding in C++ by chris_wot on Monday February 20, 2017 @02:30AM (#13685198)
If nobody has time to rewrite old C++ code then it's not going to be rewritten in rust
Re:Recommendations about coding in C++ by tempodox on Monday February 20, 2017 @02:48AM (#13685288)
That would seem to be the case, naturally. Why do you mention it?
Re:Recommendations about coding in C++ by catnaroek on Sunday February 19, 2017 @04:04PM (#13681975)
> 2. Larger than 0 does not mean 1

Rust and Swift have sum types, so their equivalent of `memcmp` can have a return type with exactly three values (`Less`, `Equal`, `Greater`).

> 3. Copy once, check twice

I'd like to see the context of this code. Perhaps this could have been written more cleanly using iterators.

> 4. Beware of the ?: operator and enclose it in parentheses

Not a problem in Rust. `if cond { true_case } else { false_case }` is an expression in the obvious way.

> 5. Use available tools to analyze your code

The right tool is called “type checker”, but of course, the language has to be intelligently designed, rather than just evolved, for the type system to be useful.

> 6. Check all the fragments where a pointer is explicitly cast to integer types

Or, even better, don't do it at all.

> 7. Do not call the alloca() function inside loops

Or even better, don't do it at all.

> 8. Remember that an exception in the destructor is dangerous

Rust solves this neatly: use different tools for recoverable and unrecoverable errors (the `Result` sum type and panics, respectively). Destructors always return `()`, so they can't fail in a recoverable way.

> 9. Use the '\0' literal for the terminal null character

Or even better, use Rust-style iterators and forget this null-termination nonsense.

---

I could go on, but this is getting tiresome.

Re:Recommendations about coding in C++ by tempodox on Monday February 20, 2017 @02:57AM (#13685334)
I don't understand why you got downvoted for citing facts. It seems, some readers in this thread do everything they can to not see the elephant in the room.
Re:Recommendations about coding in C++ by AndreyKarpov on Monday February 20, 2017 @03:38AM (#13685524)
I think they but downvote for this comment because this approach is not very sensible. There is no point to say "you should write well and without mistakes". Moreover there is no point to recommend rewriting everything in a different language - it is impossible for large projects.
Re:Recommendations about coding in C++ by catnaroek on Monday February 20, 2017 @03:43AM (#13685554)
> There is no point to say "you should write well and without mistakes".

That's not what I'm saying. I'm showing how better-designed languages (which, to be fair to C++, have the benefit of over 20 years of hindsight) avoid some of the pitfalls you list.

Re:Recommendations about coding in C++ by catnaroek on Monday February 20, 2017 @03:42AM (#13685549)
Technical facts are much more offensive than you might think.