goto

Robert Fraser fraserofthenight at gmail.com
Thu Feb 5 20:06:46 PST 2009


BCS wrote:
> Hello Andrei,
> 
>> I was coding a simple Euclidean distance function with limit:
>>
>> double euclideanDistance(Range)(Range a, Range b, double limit)
>> {
>> limit *= limit;
>> double result = 0;
>> for (; !a.empty; a.next, b.next)
>> {
>> enforce(!b.empty);
>> auto t = a.head - b.head;
>> result += t * t;
>> if (result >= limit) goto thatsit;
>> }
>> enforce(b.empty);
>> thatsit:
>> return sqrt(result);
>> }
>> How would an elegant goto-less approach look like? It should not
>> duplicate code, e.g. the call to sqrt.
>>
>> Andrei
>>
> 
> I think this is a good reason for more scope(*) versions
> 
> you could fake it with
> 
>> for (; !a.empty; a.next || (enforce(b.empty), false), b.next)

Which looks even worse. IMO, this is the perfect place for goto -- small 
function, used only for an exit condition. There's no "spaghetti code" 
here at all. I know goto used to be abused back in the day, but it has 
its place.



More information about the Digitalmars-d mailing list