Is it okay to return end like this?

Give me all the boxes you want, but I need to know something. Is it okay to return end like this?


if ply.Credits <= 19 then
PrintMessage( HUD_PRINTTALK, "Player "..ply:Nick().." doesn't have enough credits for the HK416. Cost: 20 credits; your credits: "..ply.Credits..". Sorry!" )
return end


I’ve seen most people return end like this:


if ply.Credits <= 19 then return end

Is it okay to return end the way I did in the code that has PrintMesage? Or would I put it on the same line, sort of like


if ply.Credits <= 19 then PrintMessage(HUD_PRINTTALK, "Test" ) return end

?

And yes, I have this in a function.

Yes

All three would work, but remember (for reasons of clarity) that you aren’t returning ‘end’, you’re returning nothing and then ending the block (if that makes sense)

Or…

[lua]
if ply.Credits <= 19 then
PrintMessage( HUD_PRINTTALK, “Test” )
return
end
[/lua]

makes more sense than

[lua]
if ply.Credits <= 19 then
PrintMessage( HUD_PRINTTALK, “Test” )
return end
[/lua]

Can you clarify? Are you basically saying that my first example that had PrintMessage would work?

It works, but it’s not good syntax. ‘end’ is not a value that can be returned, end is an operator that marks the end of a control structure/statement. What’s really happening is you’re calling return with no argument, so it returns nil.


if ply.Credits <= 19 then
    PrintMessage( HUD_PRINTTALK, "Player "..ply:Nick().." doesn't have enough credits for the HK416. Cost: 20 credits; your credits: "..ply.Credits..". Sorry!" )
    return nil
end



if ply.Credits <= 19 then return nil end


if ply.Credits <= 19 then PrintMessage(HUD_PRINTTALK, "Test" ) return nil end

So instead of just return, I should put return nil is what you’re saying?

[editline]15th May 2015[/editline]

How is it not good syntax if it returns nil either way?

No. ‘return’ by itself implicitly returns nil, so ‘return nil’ is (usually) redundant. What he’s saying is that you shouldn’t be putting ‘return’ and ‘end’ on the same line unless the entire block is on one line, because it’s harder to read.

bad:
[lua]if condition then
– do stuff
return end[/lua]

ok:
[lua]if condition then return end[/lua]
[lua]if condition then
– do stuff
return
end[/lua]

They both do the same thing. Doing return versus return nil isn’t the bad syntax. The bad syntax is not formatting your control structures. Having single line if then return end statements are very common, but they can cause issues when you’re debugging code and have to read into all the if statements in order to find the return that’s causing issues. By the book good syntax is to have your if (conditions) on one line, indent everything that will execute when this condition is met, and then have your end/until or any other ‘bottom/end’ operators lined up with the ‘top/opening’ operators on its own line.

If you do this then it becomes easier to read the code and figure out what statements are being ‘entered’ due to conditions. I use single line if then return end statements in my code, so by no means am I saying it’s terrible and you’re terrible if you do it. It’s just harder to read.

Long story short:
When using multi-line if statements make sure you put your end operator on it’s own line.

Oh. So technically I don’t need to use return nil, or should I? That’s all I need to know.

[editline]15th May 2015[/editline]

Oh. So technically I don’t need to use return nil, and I can just use return? Or should I use return nil? That’s all I need to know.

Do return nil so you understand that you’re not returning ‘end’.

Oh. That’s just for learning sake? Just plain return would work too? Well, so I know I’m not returning end might as well return nil but wondering would just plain return also work? What would work better, programming-wise?

Yes it’s just for your learning sake. If you see that ‘nil’ is after the return you should understand that ‘something’ is still being returned, even though nil is invalid/nothing. Once you understand that then you can stop putting nil after return (unless you have something you actually want to return). There is no advantage to return versus return nil. Read up on Lua Control Statements.

Thanks