request consistent for style it is

“blue is the sky” … “the sky is blue” …

this is a very minor thing (and just when you thought there was nothing left worth nitpicking!!) but…

i’d like to suggest that Corona adopt a consistent style for writing conditional statements.

why?  i think it would benefit the community, particularly beginners, if a standard were adopted.

I can offer up my own preference, but I don’t really care - either way, just pick one, and stick to it.

as a simple example, take the included “Composer” sample code:

in the scenetemplate.lua file (which matches docs) we find human style, fe:

if phase == "did" then

but in all of the actual scene files (scene1.lua et al) we find yoda style, fe:

if "did" == phase then

the open-source libraries are a hodge-podge, fe:

-- timer.lua -- yoda in performWithDelay(): "function" == type( listener.timer ) -- human in cancel() a few lines later: if type(entry) ~= "table" then

$0.02 section.  My personal preference?  Human-style always, yoda-style never.  The only “real” argument in favor of yoda is in languages like C that support assignments within conditional expressions (and even there it’s of debatable value).  Lua doesn’t even have that problem, so there’s really no argument in favor of yoda within this context.

fwiw

even if the force is with yoda, he should retire =P

+1

I agree, and would vote against yoda-style too.  

If one makes this mistake,

if bad = "yoda" then

The interpreter will complain, so we don’t need this to protect us:

if "yoda" = bad then

Actually, Walter explained this to me once. I personally don’t like yoda case. It’s not how I learned to program and it doesn’t read correctly. 

Yoda case protects against programming mistakes where you do:

if bad = “yoda” then

would return true because bad isn’t nil. It, however, does not test if bad is equal to “yoda”. By using yoda case, you would get a runtime error because you can’t assign the value of bad to an umutable string. By adopting this, you, in theory, have less buggy code.

But 30 years of writing in natural language is hard to overcome. Our code style is to use yoda case and we are very bad at being consistent about it.

Rob

that’s true for languages like C, but not true in Lua – Lua won’t even compile that.

In Lua the if statement requires an expression as its conditional, and assignment is not an expression in Lua (it’s a statement).

So that example will generate a syntax error before you ever get a chance to run it.  (so it definitely does not return true)

FYI, Lua is smarter than that.  It detects this as a syntax error:

local yoda = "alive" if( yoda = "dead" ) then -- syntax error on this line print("dead" ) else print("alive") end

Update - Oh dang! I’m late to the part on the last response.

even if the force is with yoda, he should retire =P

+1

I agree, and would vote against yoda-style too.  

If one makes this mistake,

if bad = "yoda" then

The interpreter will complain, so we don’t need this to protect us:

if "yoda" = bad then

Actually, Walter explained this to me once. I personally don’t like yoda case. It’s not how I learned to program and it doesn’t read correctly. 

Yoda case protects against programming mistakes where you do:

if bad = “yoda” then

would return true because bad isn’t nil. It, however, does not test if bad is equal to “yoda”. By using yoda case, you would get a runtime error because you can’t assign the value of bad to an umutable string. By adopting this, you, in theory, have less buggy code.

But 30 years of writing in natural language is hard to overcome. Our code style is to use yoda case and we are very bad at being consistent about it.

Rob

that’s true for languages like C, but not true in Lua – Lua won’t even compile that.

In Lua the if statement requires an expression as its conditional, and assignment is not an expression in Lua (it’s a statement).

So that example will generate a syntax error before you ever get a chance to run it.  (so it definitely does not return true)

FYI, Lua is smarter than that.  It detects this as a syntax error:

local yoda = "alive" if( yoda = "dead" ) then -- syntax error on this line print("dead" ) else print("alive") end

Update - Oh dang! I’m late to the part on the last response.