2 ms·
> This could be considered wasteful. It's true that there is an extra compare. I think it's a small cost for maintainable code. > Then they need to read what
by asveikau 13y ago
> This could be considered wasteful.
It's true that there is an extra compare. I think it's a small cost for maintainable code.
> Then they need to read what the function is doing and understand it before they mess with the code, just like in any other situation.
Sounds great, however, the time they spent figuring out your haphazard, repetitive and confusing free() statements could be better spent somewhere else. When I said that my way is more "composeable" I meant that adding, removing, or re-ordering operations is a cheaper operation for the programmer. Follow this style and you'll spend less time trying to read and figure out code because it will fit the existing convention and will be bleedingly obvious where the buffers are released.
I may have been a bit hyperbolic with calling this "correct" or "supposed" however I didn't make up these conventions, I advocate them because I have seen them work really well and I have seen yours create mounds of inflexible spaghetti.
- Nursie 13y ago>> It's true that there is an extra compare. I think it's a small cost for maintainable code. And I don't think it's the only way to achieve maintainable code. That's all I'm saying. >> Sounds great, however, the time they spent figuring out your haphazard, repetitive and confusing free() statements If it's the coding standard of the product that you have a small block of this at the top before you start actually writing the function then it doesn't take any more time for a coder to understand than any other way around, the important part is consistency. >> I advocate them because I have seen them work really well and I have seen yours create mounds of inflexible spaghetti. What's mine? I'm not advocating any of them, just sticking to one. I still don't think yours is any better than (for instance) - type function() { type2 *thing = (cast) malloc (size); type2 *thing2; if (thing) { thing2 = (cast2) malloc (size2); if(thing2) { // do some stuff here // and some more stuff ... free(thing2); } free(thing); } return code; } A pattern which auto-unrolls as it exits without the need for more tests, and to me is every bit as maintainable as the goto cleanup; pattern. Spaghetti code (to me) is more about encapsulation and modularisation failures than it is about the content of any individual function.
- asveikau 13y agoSo you avoid a few null checks but you introduce the indentation problem you mentioned a few comments ago. I think we can agree this is better than the alexkus example but there are tradeoffs involved. (PS: I am bothered at how you cast the return value of malloc, this is C we're talking about right, not some other language with more plus signs? :-))