Re: Please, no knee-jerk "no goto ever" reactions
Re: Please, no knee-jerk "no goto ever" reactions
Posted Dec 30, 2014 2:10 UTC (Tue) by filipjoelsson (guest, #2622)In reply to: Re: Please, no knee-jerk "no goto ever" reactions by ldo
Parent article: The "too small to fail" memory-allocation rule
I thought it would be a fun exercise to rewrite your code with the exact same structure that you used. But instead of the do { /* code */ } while (false);
construct - I'd use goto cleanup
. The point of this would have been to prove to you that what you are doing is in fact to use goto, but in a circumspect manner.
You do realize that your construct will optimize to exactly the same code, when compiled, right? So, that all you do is add empty words and pointless levels of indent, that may or may not help compilers and other programmers understand what you mean?
So I took a dive into your code, stripped out all the comments and newlines, ran it through indent, and tried again.
And I must say that I believe that do { /* code */ } while (false);
is one of your smaller sins!
Why the f*ck do you for instance do things like this?
for (i=0;;) {
if (i == nrcolors) break;
/* code */
++i;
}
Breaking expectations like that do not anything of value, and makes sure nobody else will want to bother helping out on your project.
You also have nuggets like:
if (nrhistentries <= 4 || /* other condition */)
{
if (nrhistentries > 0) {
histogram[0].index = 0;
if (nrhistentries > 1) {
histogram[1].index = 1;
if (nrhistentries > 2) {
histogram[2].index = 2;
if (nrhistentries > 3) {
histogram[3].index = 3;
}
}
}
}
Which I would like to replace with:
int max_histentries = (nrhistentries>4)?4:nrhistentries;
for (i=0; i<max_histentries; ++i) {
histogram[i].index = i;
}
I also wonder what the point is of putting semicolons after comments.
In conclusion: I was in your camp until this discussion started - I really never ever use goto. You, however, have convinced me that there are reasonable exceptions to that rule.
Posted Dec 30, 2014 20:42 UTC (Tue)
by ldo (guest, #40946)
[Link] (1 responses)
I have this convention: if one of the exits from a loop is via an explicit break, then all exits from the loop shall be via explicit breaks. That way you don’t have to look for more than one kind of termination condition, just look for the breaks. Only if there are none will there be something like a for-loop termination condition. Never mix the two.
Posted Dec 30, 2014 21:12 UTC (Tue)
by filipjoelsson (guest, #2622)
[Link]
Is there any construct that you use as intended?
Re: Why the f*ck do you for instance do things like this?
Re: Why the f*ck do you for instance do things like this?