3 ms·
> 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`, `Eq
by catnaroek 10y ago
> 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.
- tempodox 10y agoI 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.
- AndreyKarpov 10y agoI 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.
- catnaroek 10y ago> 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.
- catnaroek 10y agoTechnical facts are much more offensive than you might think.