Python Forum

Full Version: a preferred choice os the two lines?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
is there a preferred way of coding stuff like this (choose one line of these two):

working_column = self.columns[-1] if len(self.columns) > 0 else [0]
working_column = [0] if len(self.columns) < 1 else self.columns[-1]
or does it just not matter and is left to personal preference or arbitrary chance?
Personally, I tend to put the outcome I expect is more likely first.
why not just
working_column = self.columns[-1] if self.columns else [0]
i personally avoid single line conditionals at all
(May-16-2017, 12:15 PM)metulburr Wrote: [ -> ]i personally avoid single line conditionals at all

do you have a reason for that which you believe others should consider?
(May-17-2017, 08:38 AM)Skaperen Wrote: [ -> ]do you have a reason for that which you believe others should consider?

because I also avoid ternary operator - in my opinion it's less readable, but it is personal preference.
different opinions
I actually like ternary operations. It makes it very clear that you're always setting a new value to the variable, which can otherwise get lost along the way once the code is passed around a few people.
If I have to read it twice I'd prefer a normal conditional
i like ternary operators when what i am having the code match ehat i am thinking.  this would include a case where i am assigning a single target with a value or a different value or a case where an expression needs to vary based on a conditional.  this is something i did even before i programmed in C.  way back in my macro assembler days i wrote a set of macros to do structured programming.  i included the capability to use a conditional to establish a value apart from the code that would store it, or use it in some way.  i recognized that logic could have meanings like this and sought to make my code express the meaning as well as it could.  so finding that C and Pike supported ?: and Python had a way (even if it was different) made me even happier with these languages.