Re:The Evolution of C Programming Practices: A Study of Unix...
by simias on Friday May 11, 2018 @09:31AM
(#17047165)
That adds a lot of boilerplate though. IMO the best solution is destructors and RAII so that you can return early in case of error and not leave your resource half-initialized. And this way you don't have to repeat your cleanup code in the "deinit" method. Of course if you start adding destructors soon you'll want generics and before you know it you end up with "C with classes" and we all know that's the path towards insanity.
[ Parent
| on HN ]
Re:The Evolution of C Programming Practices: A Study of Unix...
by WalterBright on Friday May 11, 2018 @10:15AM
(#17047421)
> That adds a lot of boilerplate though.
In practice, it doesn't. (The compiler inlines the code.) I know because I'm pretty picky about this sort of thing - it was why I was using goto in the first place.
> RAII
RAII can work, but it's a bit clunky compared to a nested function call. Additionally, if the code being factored is not necessarily the same on all paths, that doesn't fit well with RAII.
Re:The Evolution of C Programming Practices: A Study of Unix...
by simias on Friday May 11, 2018 @10:45AM
(#17047634)
Boilerplate in term of characters typed, not resulting code size. Adding a bunch of function declarations adds a significant amount of noise IMO.
>RAII can work, but it's a bit clunky compared to a nested function call. Additionally, if the code being factored is not necessarily the same on all paths, that doesn't fit well with RAII.
I think I'd need to see an example of what you're talking about then because I don't quite understand how your method works exactly.
Re:The Evolution of C Programming Practices: A Study of Unix...
by vardump on Friday May 11, 2018 @01:47PM
(#17049367)
Boilerplate doesn't mean (binary) bloat.
Re:The Evolution of C Programming Practices: A Study of Unix...
by WalterBright on Friday May 11, 2018 @02:11PM
(#17049570)
Rather than debate, here's an example of replacing goto's with a nested function:
Re:The Evolution of C Programming Practices: A Study of Unix...
by vardump on Friday May 11, 2018 @02:49PM
(#17049877)
I guess it's a matter of taste, but I often (not always) prefer less return statements in a function with gotos to error handling/cleanup code. Especially in kernel mode drivers.
Re:The Evolution of C Programming Practices: A Study of Unix...
by WalterBright on Friday May 11, 2018 @02:56PM
(#17049944)
Check the asm code generated by your compiler - it may optimize multiple returns into one.