visit
You can find all the previous code smells (Part i - XXXVII) here.
Let's continue...
You are FTX and your code allows special cases
TL;DR: Don't add hard business rules to your code.
if (currentExposure > 0.15 && customer != "Alameda") {
// Be extra careful not to liquidate
liquidatePosition();
}
customer.liquidatePositionIfNecessary(0.15);
// This follows the Tell, Don't ask principle
Code Smell 133 - Hardcoded IF Conditions
Code Smell 29 - Settings / Configs
Computer science inverts the normal. In normal science, you're given a world, and your job is to find out the rules. In computer science, you give the computer the rules, and it creates the world.
Alan Kay
Software Engineering Great Quotes
The first thing we read after the if the condition is the IF
TL;DR: You have the important else condition on the else.
fun addToCart(item: Any) {
if (!cartExists()) {
// Condition is negated
this.createCart();
this.cart.addItem(Item);
// Repeated Code
}
else {
// Normal case is on the else clause
this.cart.addItem(Item);
}
}
fun addToCart(item: Any) {
if (cartExists()) {
this.cart.addItem(Item);
}
else {
this.createCart();
this.cart.addItem(Item);
}
}
fun addToCartShorter(item: Any) {
if (!cartExists()) {
this.createCart();
}
this.cart.addItem(Item);
}
Code Smell 51 - Double Negatives
Code Smell 156 - Implicit Else
Beauty is more important in computing than anywhere else in technology because software is so complicated. Beauty is the ultimate defense against complexity.
D. Gelernter
Use contextual and local names
TL;DR: Don't repeat your parameters' names. Names should be contextual.
class Employee
def initialize(@employee_first_name : String, @employee_last_name : String, @employee_birthdate : Time)
end
end
class Employee
def initialize(@first_name : String, @last_name : String, @birthdate : Time)
end
end
Code Smell 174 - Class Name in Attributes
Code Smell 87 - Inconsistent Parameters Sorting
As a rule, software systems do not work well until they have been used, and have failed repeatedly, in real applications.
David Parnas
Bad actors are there. We need to be very careful with their input.
TL;DR: Sanitize everything that comes from outside your control.
We can also add assertions and invariants to our inputs.
Even better, we can work with Domain Restricted Objects.
user_input = "abc123!@#"
# This content might not be very safe if we expect just alphanumeric characters
import re
def sanitize(string):
# Remove any characters that are not letters or numbers
sanitized_string = re.sub(r'[^a-zA-Z0-9]', '', string)
return sanitized_string
user_input = "abc123!@#"
print(sanitize(user_input)) # Output: "abc123"
Code Smell 121 - String Validations
Code Smell 178 - Subsets Violation
Code Smell 15 - Missed Preconditions
Companies should make their own enterprise systems as often as network security companies should manufacture their own aspirin.
Phil Simon
Stop thinking of data as attributes. They are only needed to back your behavior
TL;DR: Don't focus on accidental properties. You won't need many of them.
Whenever they want to model a person or an employee, junior programmers or students add an attribute 'id' or 'name' without thinking if they are really going to need them.
We need to add attributes 'on-demand' when there's enough evidence. Objects are not 'data holders'.class PersonInQueue
attr_accessor :name, :job
def initialize(name, job)
@name = name
@job = job
end
end
class PersonInQueue
def moveForwardOnePosition
# implement protocol
end
end
Code Smell 144 - Fungible Objects
Code Smell 109 - Automatic Properties
Object thinking focuses our attention on the problem space rather than the solution space.
David West