lua-users home
lua-l archive

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


On 29/07/2022 04:13, Sean Conner wrote:
It was thus said that the Great Sean Conner once stated:

  If I'm doing this, then I would restructure the code to be:

	-- existing code

	local function foo()
	  for ...
	    for ...
	      for ...
	        if somecondition then
                  return
		end
	      end
            end
	  end
	end
	
	foo()

  And for those that know that naming things is one of the harder problems
in Computer Science:

	(function()
	  for ...
	    for ...
	      for ...
	        if somecondition then
	          return
	        end
	      end
	    end
	  end
	end)()

  -spc

Well, this latter is really cute, and I may end-up using it! :-)

It's an idiom I already use when I need statements to return a value for an expression, like:

local x = (function()...end)() -- Initialize x with complex algo.


but I've never used specifically to bail out from nested loops.


The main readability problem I see is when you don't notice where the call site is because that (function() ... end)() combination is split by, say, 50+ lines. Again, people reading it may thing they are returning from a "regular function", and not from a local nameless function used to replace a goto to bail out from nesting.