lua-users home
lua-l archive

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


On Fri, Sep 23, 2005 at 11:17:30PM -0500, Rici Lake wrote:
> I've actually never really noticed the lack of continue in Lua, since 
> you can almost always achieve the effect simply by inverting the if 
> statement:
> 
> for x in iterator() do
>   if condition(x) then continue end
>   ...
> end
> 
> -->
> 
> for x in iterator() do if not condition(x) then
>   ...
> end end

In C, I consider this very poor style, forcing more and more code into
deeper levels (and hence indentation), and I make a strong habit of
fixing this by un-inverting it whenever possible.  I havn't noticed
the problem in Lua, but that's probably because I mostly use it for
small mini-script code snippets, not involved code, so it doesn't
become an issue for me very often.

It quickly becomes awkward when you have a lot of exit cases.  I've
seen code that looks like this:

if( a )
{
	if( b )
	{
		if( c )
		{
			if( d )
			{
			...
			}
		}
		else
			set_error;

	}
	else
		set_error;
}

In some old OpenAL code, I found constructs like this nested fifteen (15)
levels deep--apparently whoever wrote that code had never heard of break,
continue or return.  :)  (My guess, in that case, is that it was someone
who had some very old-school "only one exit path" notion drilled into his
head, and hadn't realized that compilers have advanced in the last decade
or two and it's a useless guideline today.  The case can apply to loops,
too.)

One reason I don't like all this indentation (style atrocities aside) is
that for each nesting level a code block is in, I need to go and see why;
a block of code nested 5 level deep takes more work to understand than one
nested 2 deep.

Another simple but annoying reason: if you add a new check, you have to
change the indentation level of everything.  That wreaks havoc with CVS
diffs, turning a 4-line diff into 40 lines (forcing me, checking the
patch, to check to make sure it's really just indentation changes).
It also encourages merge conflicts.  (Unless you start cheating by
leaving out indentation levels, but the confusion that can cause is
probably obvious ...)

-- 
Glenn Maynard