SIS
July 2022
The process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal structure.
Martin Fowler, Refactoring (1999)
William Opdyke‘s PhD Thesis (1992)
A little non-coding writing example:
When should you edit your writing?
All the time! 🧐
😳 Cryptic names for variables, etc. | ⇒ Rename to have clearer names |
🥱 Long function / script body | ⇒ Break into smaller units |
🤬 Duplicated code | ⇒ Extract units and share |
🤔 Learn a better algorithm | ⇒ Replace algorithm |
logisticMap <- function(x) { 3.6 * x * (1 - x) }
x <- 0.3
symbols <- c("symbol 1", "symbol 2", "stuff", "more stuff", "lorem ipsum")
numberSequence <- c()
symbolSequence <- c()
for (i in 1:100) {
x <- logisticMap(x)
numberSequence <- c(numberSequence, x)
x <- logisticMap(x)
symbolSequence <- c(symbolSequence, symbols[floor(x * 5) + 1])
}
logisticMap <- function(x) { 3.6 * x * (1 - x) }
pickSymbol <- function(symbols, x) {
symbols[floor(x * length(symbols)) + 1]
}
x <- 0.3
symbols <- c("symbol 1", "symbol 2", "stuff", "more stuff", "lorem ipsum")
numberSequence <- c()
symbolSequence <- c()
for (i in 1:100) {
x <- logisticMap(x)
numberSequence <- c(numberSequence, x)
x <- logisticMap(x)
symbolSequence <- c(symbolSequence, pickSymbol(symbols, x))
}
logisticMap <- function(x) { 3.6 * x * (1 - x) }
pickSymbol <- function(symbols, x) {
symbols[floor(x * length(symbols)) + 1]
}
x <- 0.3
symbols <- c("symbol 1", "symbol 2", "stuff", "more stuff", "lorem ipsum")
numberSequence <- numeric(100)
symbolSequence <- character(100)
for (i in 1:100) {
x <- logisticMap(x)
numberSequence[i] <- x
x <- logisticMap(x)
symbolSequence[i] <- pickSymbol(symbols, x)
}
logisticMap <- function(x) { 3.6 * x * (1 - x) }
pickSymbol <- function(symbols, x) {
symbols[floor(x * length(symbols)) + 1]
}
x <- 0.3
symbols <- c("symbol 1", "symbol 2", "stuff", "more stuff", "lorem ipsum")
numberSequence <- numeric(N)
symbolSequence <- character(N)
N <- 100
for (i in 1:N) {
x <- logisticMap(x)
numberSequence[i] <- x
x <- logisticMap(x)
symbolSequence[i] <- pickSymbol(symbols, x)
}
def increment_depth(request):
temp = request["depth"]
temp = temp + 1
request["depth"] = temp
return temp
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
def price(self):
# price is base price - quantity discount + shipping
return self.quantity * self.item_price - \
max(0, self.quantity - 500) * self.item_price * 0.05 + \
min(self.quantity * self.item_price * 0.1, 100.0)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
def price(self):
# price is base price - quantity discount + shipping
return self.quantity * self.item_price \
- max(0, self.quantity - 500) * self.item_price * 0.05 \
+ min(self.quantity * self.item_price * 0.1, 100.0)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
def price(self):
# price is base price - quantity discount + shipping
base_price = self.quantity * self.item_price
return base_price \
- max(0, self.quantity - 500) * self.item_price * 0.05 \
+ min(base_price * 0.1, 100.0)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
def price(self):
# price is base price - quantity discount + shipping
return self.quantity * self.item_price - \
max(0, self.quantity - 500) * self.item_price * 0.05
+ min(self.quantity * self.item_price * 0.1, 100.0)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
def price(self):
# price = base price - discount + shipping
return self.quantity * self.item_price - \
max(0, self.quantity - 500) \
* self.item_price * 0.05 \
+ min(self.quantity * self.item_price * 0.1,
100.0)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
def price(self):
# price = base price - discount + shipping
return self.base_price() - \
max(0, self.quantity - 500) \
* self.item_price * 0.05 \
+ min(self.base_price() * 0.1,
100.0)
def base_price(self):
return self.quantity * self.item_price