What's a better way of typing this?

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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: What's a better way of typing this?

Post by bartbes »

Code: Select all

variable = math.max(variable, 0)
or, in the style of the 'ternary' operator

Code: Select all

variable = variable < 0 and 0 or variable
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: What's a better way of typing this?

Post by Tesselode »

My goodness, why do you guys have to come up with such short and confusing ways to do things? How about things that make sense to the average (code) reader?
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: What's a better way of typing this?

Post by Taehl »

Short circuit evaluation does make sense (glorious sense, at that), as long as your mind isn't chained down by restrictive languages like C (before I get flamed by C-lovers: Yes, I know C can do it, but it doesn't promote the right /mindset/ to use such things).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: What's a better way of typing this?

Post by vrld »

Tesselode wrote:My goodness, why do you guys have to come up with such short and confusing ways to do things? How about things that make sense to the average (code) reader?
provided the expressions (and condition) are not too complicated, i find

Code: Select all

var = condition and expression_one or expression_two
easier to read and understand than

Code: Select all

if condition then
    var = expression_one
else
    var = expression_two
end
This way I can see whats happening reading only one line instead of three.

Also: because we can.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What's a better way of typing this?

Post by Robin »

Tesselode wrote:My goodness, why do you guys have to come up with such short and confusing ways to do things? How about things that make sense to the average (code) reader?
How is

Code: Select all

if someState then
    someState = false
else
    someState = true
end
easier to understand than

Code: Select all

someState = not someState
?

If you prefer the former to the latter, you might need to read The Daily WTF more often. ;)
Help us help you: attach a .love.
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: What's a better way of typing this?

Post by Tesselode »

I'm not talking about that one.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What's a better way of typing this?

Post by Robin »

What, bartbes' one?
Help us help you: attach a .love.
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: What's a better way of typing this?

Post by Tesselode »

bartbes wrote:

Code: Select all

variable = math.max(variable, 0)
or, in the style of the 'ternary' operator

Code: Select all

variable = variable < 0 and 0 or variable
This one.
User avatar
knorke
Party member
Posts: 240
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: What's a better way of typing this?

Post by knorke »

i was superproud when i invented

Code: Select all

players[i].is_ai = players[i].is_active and not players[i].is_ai
players[i].is_active = players[i].is_ai or not players[i].is_active
to toggle a player being a human player, ai player or not active when a button is clicked.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: What's a better way of typing this?

Post by thelinx »

Assuming I understand your setup correctly, this should work:

Code: Select all

players[i].is_ai,players[i].is_active = players[i].is_active,players[i].is_ai
Post Reply

Who is online

Users browsing this forum: No registered users and 65 guests