I'm a C++ lover who has had to program in ASP and Vbscript lately. (Ugh!) I've been using parentheses liberally even in places where they aren't completely necessary. They don't affect the execution of the program and make it look better and clearer to me. I think the situation is probably similar with LUA (which I haven't yet tried).
But I really do think curly braces beat keywords. Those braces are decipherable at a very brief glance and I don't know why people would forget them any more than the equivalent keywords. I myself forget curly braces very rarely because I always insert them in pairs. Those pesky thens on the other hand...
Channel Elements -- too many attacks?
Moderator: Moderator
-
- Uruivellas
- Posts: 656
- Joined: Thu Jul 25, 2002 8:07 am
- Location: Leafy East Surrey, UK
- Contact:
Trinary operator is sometimes usefull but really quite ugly IMO:
res = (condition) ? true_value : false_value;
Whcih will select the true_valu or false_value based on the condition
res = (condition) ? true_value : false_value;
Whcih will select the true_valu or false_value based on the condition
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning

a ? b : c;
a is the condition
b is done if a is true
c is done if a is false
Basically the same thing as writing
if (a)
{
b;
}
else
{
c;
}
VBScript has it
if a then
b
else
c
end if
This should be close to how lua does it. (I know it lacks the final if.) I'm not putting up a lua example as I have never more than glanced at it.
I tend to use the long way myself even in C. It takes more lines but is more readable. I was taught in a place in which a lot of weight was put on good coding style.
a is the condition
b is done if a is true
c is done if a is false
Basically the same thing as writing
if (a)
{
b;
}
else
{
c;
}
VBScript has it
if a then
b
else
c
end if
This should be close to how lua does it. (I know it lacks the final if.) I'm not putting up a lua example as I have never more than glanced at it.
I tend to use the long way myself even in C. It takes more lines but is more readable. I was taught in a place in which a lot of weight was put on good coding style.
Zothiqband -- still an Angband variant.
Fo4s:
The trinary operator is (cond ? thenpart : elsepart). It is an expression, not a statement, and it evaluates to whatever the thenpart evaluates to if cond evaluates to true, or whatever the elsepart evaluates to, otherwise.
As for implementing it in lua, try something like
function ifte(cond,t,e) if cond then return t else return e end end
Use it as follows:
ifte(dam-10 < 1,1,dam-10)
add in some more brackets, BenH style, if you prefer.
Of course, ifte is 4 characters long, which is a bit of a pain, but 'if' is a reserved word and 'i' is probably namespace pollution. But you might think of a better name.
You can't actually implement new operators in lua (although you could by modifying the source of lua itself, of course), but you can alter the existing ones. So one might be able to do a tremendous hack with them, but I don't recommend it.
The trinary operator is (cond ? thenpart : elsepart). It is an expression, not a statement, and it evaluates to whatever the thenpart evaluates to if cond evaluates to true, or whatever the elsepart evaluates to, otherwise.
As for implementing it in lua, try something like
function ifte(cond,t,e) if cond then return t else return e end end
Use it as follows:
ifte(dam-10 < 1,1,dam-10)
add in some more brackets, BenH style, if you prefer.
Of course, ifte is 4 characters long, which is a bit of a pain, but 'if' is a reserved word and 'i' is probably namespace pollution. But you might think of a better name.
You can't actually implement new operators in lua (although you could by modifying the source of lua itself, of course), but you can alter the existing ones. So one might be able to do a tremendous hack with them, but I don't recommend it.
...which goes back to my original point.Nerdanel wrote:It takes more lines but is more readable. I was taught in a place in which a lot of weight was put on good coding style.
This is rubbish, of course. The '?' version is no 'less readable' if you are used to the '?' operator. Indeed, it is *more* readable and *more* compact. On the other hand, if you are not used to the operator, it is of course less readable.
Just as <<< and << are unreadable if you can't remember which is which.
Syntax is just something you have to get used to. When choosing syntax for a piece of code you might want to be aware of the syntactic prejudices of the code's intended audience, that's all.