0

Hi,

I'm curious about a few conventions.

If you have a function that can return a or b based on some conditionals, which do you think is the best/accepted notation?

def test(val):
   if val > 10: 
      return a
   else:
      return b

Or

def test(val):
   if val>10:
      return a
   return b

I often see both of these conventions used and wondered what you guys would recommend. Additionally, I commonly see these dual notations:

if x==True:
  do stuff

VS

if x:
  do stuff

Is there a preferred convention in this case?

Thanks.

Edited by hughesadam_87

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by sneekula
0

Second one is easier, the form without True is definately better in my opinion. The first condition with only returns I would actually use ternary condition:

def test(val):
    return a if val > 10 else b

else-less if I would usually use with condition that have not much connection to logic after if, if there is two balanced alternatives I prefer including else. For assert like guard ifs I do not use else.

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.