3 ms·
>> 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.
by 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? :-))