Page 1 of 1

What are your thoughts on this coding practice?

Posted: Fri Oct 02, 2015 11:35 pm
by Qcode
Hey,

Just recently I've been using a lot more ternary statements in my code. I really liked the convenience of it for assigning variables, etc. Condensing a five line statement into one line feels pretty nice.

What I'm more worried about is that I've also started condensing some statements like this:

Code: Select all

self.dead = self.health == 0
Where as before I would have written

Code: Select all

if self.health == 0 then
     self.dead = true
end
Which do you personally is the better practice? Like I said, I've been enjoying the convenience of writing shorter statements like the top code, but I'm worried if I use it too much my code is going to get less readable...

Let me know your thoughts!

Re: What are your thoughts on this coding practice?

Posted: Sat Oct 03, 2015 1:54 am
by TheOdyssey
In my opinion, the second one is miles better. Maybe you are trying to shorten code but to me it seems like the top one is a bit too confusing and could easily bring up some errors

Re: What are your thoughts on this coding practice?

Posted: Sat Oct 03, 2015 2:57 am
by Jasoco
TheOdyssey wrote:In my opinion, the second one is miles better. Maybe you are trying to shorten code but to me it seems like the top one is a bit too confusing and could easily bring up some errors
I don't think so. If you're the one coding it, the top would be perfectly understandable later on. I love doing it that way. A lot less code. But both are literally the same.

It's up to you, OP. When you're glancing through your code, does that first line ever confuse you at all? If you need to you could use a visual cue to make it easier to pick out by putting it in parenthesis:

Code: Select all

self.dead = (self.health == 0)
Then at a glance you'll associate the parenthesis as representing a single value.

Just like this:

Code: Select all

value = math.min(value + 1, 1000)
Is the same as this:

Code: Select all

value = value + 1
if value > 1000 then
    value = 1000
end
But the top one is just so much less code. Though unless you understand what math.min and math.max do you might be confused. I wonder if either one is more or less optimal. I mean would math.min add any more time to execution than checking if a value is greater than something every frame? Probably not. I'm pretty sure math.min literally does the same thing as the lower code more or less.

Re: What are your thoughts on this coding practice?

Posted: Sat Oct 03, 2015 2:59 am
by Inny
As a tweak, the first one can be made more clear, and it's usually my practice, to demark conditionals with parenthesis:

Code: Select all

self.dead = (self.health == 0)
For me, it makes it more clear that the value from the condition is being stored from that statement.

Edit: Hah, me and Jasoco both made the same suggestion at the same time.

Re: What are your thoughts on this coding practice?

Posted: Sat Oct 03, 2015 3:04 am
by Jasoco
Inny wrote:Edit: Hah, me and Jasoco both made the same suggestion at the same time.
Wow. I didn't know anyone else would think of that.

Re: What are your thoughts on this coding practice?

Posted: Sat Oct 03, 2015 5:42 am
by ivan
I think both examples are perfectly fine,
and I use both, often leaning towards the second example
since it makes the code shorter horizontally.

One thing to note is that both examples are not identical, it should be:

Code: Select all

self.dead = false
if self.health == 0 then
     self.dead = true
end

Re: What are your thoughts on this coding practice?

Posted: Sat Oct 03, 2015 9:17 am
by kikito
The first option is the Lua way. It's what you will find in code written by people familiar with Lua. It takes less vertical space to express the same concept. It is the one I recommend in general.

The second option is more easily understood by people not familiar with Lua, or new to programming. I would use that style if I were doing a tutorial for those people, but not in general.

So decide what your audience is, and choose your style accordingly.

I would also like to say that one-liners are not always the best option - there is a complexity limit below witch it makes sense to split it up in several lines and/or instructions. I think this implementation of clamp is near the limit:

Code: Select all

function clamp(x, min, max)
  return math.max(math.min(max, x), min)
end
Even to those experienced with Lua, it takes some seconds to understand what this does. If this was a bit more complex I would split it.

Re: What are your thoughts on this coding practice?

Posted: Sat Oct 03, 2015 10:41 am
by undef
The first one is more elegant.

I would only use the second if I'm explaining something to a beginner, and even then I would show the first afterwards.

Re: What are your thoughts on this coding practice?

Posted: Sun Oct 04, 2015 7:16 pm
by Trumpet
If you are coding purely for yourself, then go for it.

Otherwise, whether you are part of a team, expect others to see the code (your project is open source, you need help with certain snippets, etc.) you should use the second, easier to read choice. As a programmer you have to be aware that your software is a discussion between programmers, and the easier it is to communicate an idea the better, unless you are making a noticable efficiency sacrifice.