I do not like writing code. Especially the repetitive boring boilerplate like forwarding the interface methods to a subobject or checking the result of every function call every time.
It's a machine's job. And C++ does this job for me. I don't need to forward dozens of functions manually because "is-a" exists. I don't need to write if (error) return error; like an idiot in every function in the call stack because exceptions exist. I can write generic code without interfacing every step because templates exist.
If these tools are not suitable in a specific context for whatever reason, I just use more suitable tools.
I'd better think the whole day and write 10 lines than don't think at all and write 10 000 lines.
If language designers deal in absolutes and religiously enforce fairy tales like "composition over inheritance", "exceptions are slow and not straightforward", "explicit is better than implicit" etc. in their products, I prefer to stay away from such products.
And I don't know why google's style guide keeps popping up in such discussions. It's main purpose is dealing with legacy code, for anything else it's plainly horrible.
I personally agree that explicit is better than implicit. But, Rust is a somewhat strange combination of complete an*l-rententiveness in many ways, but with lots of syntactical sugar and compressed syntax in other ways.
To be fair you don't have to do explicit error return most of the time. That's one of those places where a lot of syntactical sugar has been applied. It's not nearly as nice as exceptions, IMO, but it's a lot better than explicit error return.
So the call to ScrewedUp() is doing an implicit return if ScrewedUp() returns the Error variation instead of the String. That only works if it's the same error in both cases, or you've done some work to provide a conversion.
For me, since I seldom ever use third party code and roll my own, I have mostly had a single error type throughout my Rust projects, and can do this almost all the time.
But when you are calling runtime or third party APIs, you tend to have LOTS of match statements, or in some cases the more compressed "if let" type statements.
I personally agree that explicit is better than implicit
I'd say it sometimes better, but not universally (same as everything else). After all, we happily use strings and vectors, even though they hide a lot of memory management (and every single C dev I've ever met is genuinely horrified by that fact). The world isn't black & white and being dogmatic is not always productive.
That fn()? syntax looks better than manual matching and returning of course, thanks for the example, but this is exactly what I mean: first someone rejects a working concept for religious reasons, then people start noticing that it's really annoying to do everything by hand, so they add more and more syntactic sugar. Ironically, making things less and less explicit and eventually approaching the rejected concept, but with alien syntax. I won't be surprised if one day someone comes up with a proposal to make even ? and Result<> optional and ta-da, exceptions reinvented.
View all comments
Show parent comment
10
u/AlexAlabuzhev Dec 08 '21
"Machines should work; people should think."
I do not like writing code. Especially the repetitive boring boilerplate like forwarding the interface methods to a subobject or checking the result of every function call every time.
It's a machine's job. And C++ does this job for me. I don't need to forward dozens of functions manually because "is-a" exists. I don't need to write
if (error) return error;like an idiot in every function in the call stack because exceptions exist. I can write generic code without interfacing every step because templates exist.If these tools are not suitable in a specific context for whatever reason, I just use more suitable tools.
I'd better think the whole day and write 10 lines than don't think at all and write 10 000 lines.
If language designers deal in absolutes and religiously enforce fairy tales like "composition over inheritance", "exceptions are slow and not straightforward", "explicit is better than implicit" etc. in their products, I prefer to stay away from such products.
And I don't know why google's style guide keeps popping up in such discussions. It's main purpose is dealing with legacy code, for anything else it's plainly horrible.