Show off clean code examples

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Show off clean code examples

Post by Roland_Yonaba »

Ortimh wrote:In case I only want to return true value if func function returns true, so I shouldn't replace

Code: Select all

if (func()) then return true end
with

Code: Select all

return func()
because if I return func function returned value then it returns either true or false.
You are definitely right, but The Great Kikito's not wrong either.
In Lua, any non-nil value is considered to be true. Only nil and false are falsy values.
In that case, if you have a function which returns a non-nil value which is also not false, the returned value will pass a truth test.

Here is an exceprt from the online free book PiL (Programming In Lua), taken from chapter 2.2:
The boolean type has two values, false and true, which represent the traditional boolean values. However, they do not hold a monopoly of condition values: In Lua, any value may represent a condition. Conditionals (such as the ones in control structures) consider false and nil as false and anything else as true. Beware that, unlike some other scripting languages, Lua considers both zero and the empty string as true in conditional tests.
Here is an example:

Code: Select all

local function identity(value) return value end
local function test(f, v)
  if f(v) then 
    print("true!")
  else 
    print("false!")
  end
end

test(identity, 1) -- > "true!" -- number 1 is true
test(identity, 0) -- > "true!" -- even 0 is considered to be true
test(identity, true) -- > "true!" -- true is obviously true
test(identity, "") -- > "true!"  -- any string, even an empty one, is true
test(identity, {}) -- > "true!" -- any table, even an empty one, is true
test(identity, function() end) -- > "true!" -- any function, even an empty function, is true
test(identity, false) -- > "false!"  -- false is obviously false
test(identity, nil) -- > "false!" -- nil is also a falsy value
That's why The Great Kikito's point is, in my opinion, totally relevant.
But of course, you are not completely wrong, since you can't compare the value to a boolean straight. It won't work. Therefore, if you need your function to return explicitely a boolean value, you can use a very simple trick with the "not" operator. So, instead of :

Code: Select all

if (func()) then return true end
You can have :

Code: Select all

return not not func()
Hope this helps.
User avatar
Ortimh
Citizen
Posts: 90
Joined: Tue Sep 09, 2014 5:07 am
Location: Indonesia

Re: Show off clean code examples

Post by Ortimh »

Well I thought

Code: Select all

if (func()) then return true end
only return value when func function returned value is true, so it won't return any value when func function returned value is false. I think no value and falsy value are completely different.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Show off clean code examples

Post by zorg »

they are both false in a sense, but you can indeed test explicitly if the return value is nil, or not (Rudimentary ternary logic ftw :3)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Show off clean code examples

Post by Robin »

These are the cases the difference between nil and false is important:
  • A flag that should default to true.
  • A cache, where false is a legit cached value and nil means "value not in cache, do the expensive thing to get the value and put it in the cache".
  • You're writing a serialisation library.
In all other cases*, the difference between nil and false is irrelevant.

*Except the ones I forgot.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Semrush [Bot] and 15 guests