Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Defining global variables

Defining global variables

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by Gore   (207 posts)  Bio
Date Reply #15 on Tue 16 Jan 2007 02:52 PM (UTC)
Message
Style wise: which is a better way to see if a variable is true?

if variable == 1 then
--blah
end

or

if variable then
--blah
end

?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #16 on Tue 16 Jan 2007 03:28 PM (UTC)
Message
The second one is probably better of those two. For the first, you should test against true, not one. Otherwise, you have to worry about it being 2, 3, 4, and so forth. (Lua has the boolean type, so it should be used.)

The second case will be true unless the variable is nil or false.

Come to think of it, if the variable is 0, it will be true as well, which might or might not be what you want.

Testing against 'true' is probably what you want, really. And if you're not testing against a boolean, then you should have whatever test you are looking for, e.g. if var > 0 or whatever.

Stylistically it's generally better to make explicit conditions just so it's clear what's going on.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #17 on Sat 20 Jan 2007 03:35 AM (UTC)
Message
I don't like:


if variable == true then


The "if" test is already checking if the expression is true or not, and thus it is like saying "if this is true is true".

Quote:

Lua has the boolean types ...


This is true, however Lua values can have boolean types, Lua variables are untyped. Thus you can't make a variable a boolean, it just might happen to contain a boolean value at a certain time.

A variable might in fact contain nil (which would be considered false in an "if" statement), or true or false.

Only two value are considered false in an "if" test: nil and false. Anything else is true, including numeric zero.

I certainly wouldn't say:


if variable == 1 then


The implication is that a value of 0 is false, whereas 0 is still considered true.

Of course, you could write:


if variable == 0 then
-- this is the false branch
end -- if


But I think that is rather convoluted.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #18 on Sat 20 Jan 2007 03:51 AM (UTC)
Message
In case I haven't made myself clear in the above post:


  • I would use the boolean type (ie. true or false) rather than 0/1
  • I would say:


    if variable then ...


    ... rather then "if x == true" or "if x == 1"

  • In extreme cases you could use the value nil to indicate "I don't know if it is true or false". However otherwise, uninitialized variables will default to nil, which is effectively false.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


72,379 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.