Gerymake
Posts: 41
Joined: Sun Jan 06, 2019 11:38 pm

Exception or if statement

Wed Jun 05, 2019 9:25 pm

Hello everyone,

I'm setting up an automated garden but I'm wondering whether this is the best approach to get readings from a DHT11 temperature sensor and check its content. Since the DHT-Adafruit library won't raise an error if it can't connect with the sensor I ought to check whether the reading is null or not. Here is what I thought about, is there room for improvement/more reliable method?

Code: Select all

def temp_humidity_read():
    humidity, temperature = Adafruit_DHT.read_retry(11, 4)
    if (temperature and humidity) is not None:
        sensor_reading_write('temperature', temperature)
        sensor_reading_write('humidity', humidity)
    else:
        logging.error('Could not read properly the temperature and'
        'humidity sensor')
Thanks in advance,
Gerard

ghp
Posts: 1352
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Exception or if statement

Thu Jun 06, 2019 5:05 pm

Hello, looks good. It depends a bit on how this method is used.
When looking to what this code is doing, there is a read and write combined. The method name does not give an idea about this.

Code: Select all

def temp_humidity_read_and_write():
   """ read temp. If available, write it. If not available, just do nothing"""
    humidity, temperature = Adafruit_DHT.read_retry(11, 4)
    if (temperature and humidity) is not None:
        sensor_reading_write('temperature', temperature)
        sensor_reading_write('humidity', humidity)
    else:
        logging.error('Could not read properly the temperature and'
        'humidity sensor')
Another point: when a cable is broken and the sensor will not deliver any data for a long time, there is no indicator about this problem. How to solve this depends on the context in which the code is running.

Heater
Posts: 12688
Joined: Tue Jul 17, 2012 3:02 pm

Re: Exception or if statement

Thu Jun 06, 2019 5:31 pm

The generally accepted good advice in software engineering circles is:

1) Use exceptions for exceptional circumstances. Things that should never normally happen.

2) Do not use Exceptions for general program control flow.

The DHT11 is notorious for being very difficult to read reliably, repeatedly. Even when it is connected properly. As such I tend to think read errors are the norm rather than an exceptional, unexpected circumstance. Ergo use traditional error checking and flow control with "if" and the like, rather than exceptions.
Last edited by Heater on Fri Jun 07, 2019 12:09 am, edited 2 times in total.

Gerymake
Posts: 41
Joined: Sun Jan 06, 2019 11:38 pm

Re: Exception or if statement

Thu Jun 06, 2019 8:10 pm

Heater wrote:
Thu Jun 06, 2019 5:31 pm
The generally accepted good advice in software engineering circles is:

1) Use exceptions for exceptional circumstances. Things that should never normally happen.

2) Do not use Exceptions for general program control flow.

The DHT11 is notorious for being very difficult to read reliably, repeatedly. Even when it is connected properly. As such I tend to think read errors are the norm rather than an exceptional, unexpected circumstance. Ego use traditional flow control with "if" and the like, rather than exceptions.
Therefore, I guess I opted for the right approach. Another possible situation would be the impossibility to connect to an MYSQL database. As this is an extraordinary event, might it be better to use a try and except statement?

Return to “Python”