lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Robert wrote at 6/11/2009:
>> while a do
>>  while b do
>>    while c do
>>      while d do
>>        if abort() then
>>          goto exitloopc
>>        endif
>>      end
>>    end
>>    exitloopc:
>>  end
>> end

> Hmmm, I know this is probably generated code, but when I see stuff
> like that, I think some refactoring is in order.

The example there is perhaps deeper than neccessary. I know I can find
numerous algorithms that spell as perfectly readable code, like this:

for a in foo do
  for b in a.bar do
    if something then
      goto breakbothloops;
    end
  end
end
breakbothloops:;

If there is a forward goto (or named loop break) available in the
language, and look like this:

local exita = false;
for a in foo do
  for b in a.bar do
    if something then
      exita = true;
      break;
    end
  end
  if exita then
    break;
  end
end
breakbothloops:;


IME, this is a clear example why either a forward goto or a named
loop break is really needed for readable code. I don't think I can
think of a valid example for a backwards goto, though.

I believe that a goto that has to satisfy these conditions:
- only jumping forward
- only to a statement immediately after an "end" that closes a parent
scope would be useful, and would have no potential errors (cannot skip any
locals initializations).

Note that this is almost equivalent as named break (though with some
extra abilities), but is simpler to add to the grammar as it doesn't
require loop naming.

JM2C,
Alen