© 2020 Morgan C. Benton code4your.life
I hate this term.
Usually we think of "side effects" as the unwanted impacts of a medication or some actions.
However, in programming it is the opposite. In software, "side effects" describe intended outcomes and are what make software useful to people.
© 2020 Morgan C. Benton code4your.life
Side effects are generally unpredictable and involve read/write or input/output operations, such as:
The technical term for this is non-deterministic behavior
© 2020 Morgan C. Benton code4your.life
Systems or processes that always produce the same result, i.e. predictable:
Systems or process that produce unpredictable, sometimes random, results:
© 2020 Morgan C. Benton code4your.life
"side effects"
Side effects are what makes software useful.
It doesn't matter how robust your machine learning algorithm, or meteorological model, or gaming physics engine if the results of those computations can't be written to a disk, displayed on a screen, or transmitted across a network, your software does nothing useful.
© 2020 Morgan C. Benton code4your.life
Side effects are hard to test.
If you can't predict what a user is going to type into a form, or whether writing to the database is successful, or whether data requested from a URL returns as expected, it is hard to know if errors are caused by bad code or by some other failure of the system.
Such errors can diminish or eliminate the positive effects of software.
© 2020 Morgan C. Benton code4your.life
© 2020 Morgan C. Benton code4your.life
for x in range(1, 101):
if x % 15 is 0:
print('FizzBuzz') # <== side effect
elif x % 3 is 0:
print('Fizz') # <== side effect
elif x % 5 is 0:
print('Buzz') # <== side effect
else:
print(x) # <== side effect
def fizzbuzz(x):
return 'FizzBuzz' if x % 15 is 0 else x
def fizz(x):
return 'Fizz' if x % 3 is 0 else x
def buzz(x):
return 'Buzz' if x % 5 is 0 else x
for x in range(1, 101):
print(fizz(buzz(fizzbuzz(x)))) # <== side effect
Mostly Untestable
Mostly Testable
© 2020 Morgan C. Benton code4your.life
© 2020 Morgan C. Benton code4your.life
"Pure" functions:
© 2020 Morgan C. Benton code4your.life
"""
Process: User types in a number representing MPG,
which gets converted to KPL and printed.
"""
# CONSTANTS
KPM = 1.60934 # kilometers/mile
GPL = 0.264172 # gallons/liter
# IMPURE
def convert():
mpg = input('What is your MPG?')
print('Your KPL is: ' + (mpg * KPM * GPL))
# PURE
def convert(mpg):
return mpg * KPM * GPL
mpg = input('What is your MPG?')
print('Your KPL is: ' + convert(mpg))
© 2020 Morgan C. Benton code4your.life
"""
Process: Open a file that contains a list of
tempertature readings, and calculate
the average.
"""
# IMPURE
def calculateAvgTemp():
f = open('temps.txt') # <== side effect
temps = f.read().split()
avg = 0
for x in temps:
avg += float(x) / len(temps)
print(avg) # <== side effect
# PURE
def calculateAvgTemp(data):
temps = data.split()
avg = 0
for x in temps:
avg += float(x) / len(temps)
return avg
f = open('temps.txt')
print(calculateAvgTemp(f.read()))
© 2020 Morgan C. Benton code4your.life
25.8
15.6
22.0
19.2
23.3
21.9
temps.txt
"""
Process: Retrieve the current weather for Washington, DC
from the metaweather.com free REST API, search
the results to find today's weather and output
the weather state.
See: https://www.metaweather.com/api/
"""
# Import 3rd party library for making HTTP requests
from requests import get
from datetime import datetime
# CONSTANTS
URL = 'https://www.metaweather.com/api/location/2514815/'
TODAY = datetime.today().strftime('%Y-%m-%d')
# IMPURE
def getTodaysWeather():
result = get(URL) # <== side effect
data = result.json()
for day in data['consolidated_weather']:
if day['applicable_date'] == TODAY:
print(day['weather_state_name']) # <== side effect
# PURE
def getTodaysWeather(days):
for day in days:
if day['applicable_date'] == TODAY:
return day['weather_state_name']
else:
return 'Unknown'
data = get(URL).json()
print(getTodaysWeather(data['consolidated_weather']))
© 2020 Morgan C. Benton code4your.life
© 2020 Morgan C. Benton code4your.life