What are your thoughts on this coding practice?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

What are your thoughts on this coding practice?

Post 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!
User avatar
TheOdyssey
Citizen
Posts: 52
Joined: Mon Jul 13, 2015 4:29 pm
Location: Turn Around...

Re: What are your thoughts on this coding practice?

Post 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
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What are your thoughts on this coding practice?

Post 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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: What are your thoughts on this coding practice?

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What are your thoughts on this coding practice?

Post 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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: What are your thoughts on this coding practice?

Post 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
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: What are your thoughts on this coding practice?

Post 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.
Last edited by kikito on Sat Oct 03, 2015 2:45 pm, edited 1 time in total.
When I write def I mean function.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: What are your thoughts on this coding practice?

Post 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.
twitter | steam | indieDB

Check out quadrant on Steam!
Trumpet
Prole
Posts: 2
Joined: Sat Oct 03, 2015 10:36 pm

Re: What are your thoughts on this coding practice?

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests