Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TO ELSE OR NOT TO ELSE?
#1
Should we always use else statements?

By using else statements you are kind of doing the same thing that the programming gods Ken Thompson and Rob Pike recommend in GOLANG, which is to make sure you ALWAYS handle errors. Since python is dynamically typed, variables can hold stuff that you don't necessarily want.

def check_int(number):
    if type(number) == int:
        return number
    else:
        error_name = "check_int failure"
        error_details = "User did not pass an integer at"
        error_datetime = datetime.now()
        db_engine.execute(f"INSERT INTO log (error, details, datetime) VALUES({error_name}, {error_details}, {error_datetime);")
        return -1

        # or: raise ValueError('A very specific bad thing happened.') if you want runtime to stop?
        # or: render("404.html", details="dude, stop sending us strings homeslice")
I can give an example where this form of logging is important,

Say that I am controlling a remote control airplane with a remote server API.

I would like to know:
  • Is my server receiving the correct client session?
    (we can check it with if else and log whenever someone is hacking/spamming my very important server)

  • Check if my change in altitude is less than some safety measure else send an email to the engineers for quick help.
Reply


Messages In This Thread
TO ELSE OR NOT TO ELSE? - by rxndy - Apr-27-2019, 02:04 AM
RE: TO ELSE OR NOT TO ELSE? - by nilamo - Apr-29-2019, 10:03 PM
RE: TO ELSE OR NOT TO ELSE? - by rxndy - Apr-30-2019, 02:11 AM
RE: TO ELSE OR NOT TO ELSE? - by buran - Apr-30-2019, 07:13 AM
RE: TO ELSE OR NOT TO ELSE? - by rxndy - Apr-30-2019, 11:42 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020