Purpose is to make items:
Often, this is achieved through abstraction. Abstraction is the process of removing characteristics of something to reduce it some a more high level concept
"Don't repeat yourself" (DRY) is about reducing repetition in code. The same code/configuration should ideally not be written in multiple places.
Defined as:
"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system"
How can we clean this up?
import sys
if len(sys.argv) != 2:
sys.exit(1)
num = int(sys.argv[1])
if num == 2:
for i in range(10, 20):
result = i ** 2
print(f"{i} ** 2 = {result}")
elif num == 3:
for i in range(10, 20):
result = i ** 3
print(f"{i} ** 3 = {result}")
else:
sys.exit(1)
How can we improve this?
import jwt
encoded_jwt = jwt.encode({'some': 'payload'}, 'applepineappleorange', algorithm='HS256')
print(jwt.decode(encoded_jwt, 'applepineappleorange', algorithms=['HS256']))
"Keep it Simple, Stupid" (KISS) principles state that a software system works best when things are kept simple. It is the believe that complexity and errors are correlated.
Your aim should often be to use the simplest tools to solve a problem in the simplest way.
Example 1: Write a python function to generate a random string with up to 50 characters that consist of lowercase and uppercase characters
Example 2: Write a function that prints what day of the week it is today
Example 3: Handling command line arguments
python3 commit.py -m "Message"
python3 commit.py -am "All messages"
Encapsulation: Maintaining type abstraction by restricting direct access to internal representation of types (types include classes)
Example:
Consider this code:
What if we wanted to store points in polar coordinates?
class Point:
def __init__(self, x,y):
self.x = x
self.y = y
def distance(start, end):
return sqrt((end.x - start.x)**2 + (end.y - start.y)**2)
Example:
Consider this code:
Can we prevent stealing spots in the queue?
class Queue:
def __init__(self):
self.entries = []
def enqueue(self, entry):
self.entries.append(entry)
def dequeue(self):
return self.entries.pop(0)
Similar to "You aren't gonna need it" (YAGNI) that says a programmer should not add functionality until it is needed.
Top-down thinking says that when building capabilities, we should work from high levels of abstraction down to lower levels of abstraction.
Question 1: Given two Latitude/Longitude coordinates, find out what time I would arrive at my destination if I left now. Assume I travel at the local country's highway speed
"Poor software quality costs more than $500 billion per year worldwide" – Casper Jones
Systems Sciences Institute at IBM found that it costs four- to five-times as much to fix a software bug after release, rather than during the design process
Often, our default tendency is to write bad code. Why?
Bad code: Easy short term, hard long term
Good code: Hard short term, easy long term
Restructuring existing code without changing its external behaviour.
Typically this is to fix code or design smells and thus make code more maintainable