Scientific IT Services ETHZ
July 2025
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?
Continuously but Purposefully
Cryptic names for variables | ⇒ Rename to have clearer names |
Long function or script | ⇒ Break into smaller units (functions) |
Long class with a lot of different functionality | ⇒ Break into classes with single/fewer responsibilities |
Duplicated code | ⇒ Extract units (functions) and reuse |
Magic numbers | ⇒ Create named constants |
Dead code | ⇒ Delete (version control will keep it reachable) |
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 calculate_price(self):
# price is 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 calculate_price(self):
# price is 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 calculate_price(self):
# price is base price - 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 calculate_price(self):
# price is base price - 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
self.base_price = self.quantity * self.item_price
def calculate_price(self):
# price is 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)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
self.base_price = self.quantity * self.item_price
def calculate_price(self):
# price is 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)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
self.base_price = self.quantity * self.item_price
def calculate_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)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
self.base_price = self.quantity * self.item_price
def calculate_price(self):
return self.base_price - self.calculate_discount() \
+ self.calculate_shipping_cost()
def calculate_discount(self):
return max(0, self.quantity - 500) * self.item_price * 0.05
def calculate_shipping_cost(self):
return min(self.base_price * 0.1, 100.0)
def calculate_discount(quantity, item_price):
discount_rate = 0.05
minimum_quantity = 500
return max(0, quantity - minimum_quantity) \
* item_price * discount_rate
def calculate_shipping_cost(base_price):
shipping_cost_rate = 0.1
minimum_shipping_cost = 100
return min(base_price * shipping_cost_rate, minimum_shipping_cost)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
self.base_price = self.quantity * self.item_price
def calculate_price(self):
return base_price - calculate_discount(self.quantity, self.item_price) \
+ calculate_shipping_cost(self.base_price)
class ShoppingCartEntry:
def __init__(self, item_price, quantity):
self.item_price = item_price
self.quantity = quantity
def calculate_price(self):
# price is 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
self.base_price = self.quantity * self.item_price
def calculate_price(self):
return base_price - calculate_discount(self.quantity, self.item_price) \
+ calculate_shipping_cost(self.base_price)