SiteKickr Web Development

Using simple boolean operators to replace conditional statements

The conditional statement (if..then.else) is Programming 101 and used in almost any useful chunk of code out there. But, they can certainly add to "code-clutter" when used in the most basic circumstances. Take, for example, the following logic:

if item1 != '':
  print item1
elif item2 != '':
  print item2
elif item3 != '':
  print item3

Pretty basic, right? The example above is written using Python syntax. Although, Python offers an exceedingly simpler approach to the exact same result:

print (item1 or item2 or item3)

item1 is checked for boolean True, which Python defines as not False, not an empty string and not None. If it cannot reach a True value, it'll move on to item2. It's worthwhile to explore your favorite scripting languages documentation to see if a similar syntax is valid. Believe me, it's a time saver!