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.

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.

This will do stuff if x is not zero, False, None or empty

if x:
  do stuff
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.