visit
You can find all the previous code smells (Part i - XXXV) here.
Mutation is good. Things change.
TL;DR: Don't change essential attributes or behavior
Refactoring 001 - Remove Setters
“No man ever steps in the same river twice. For it’s not the same river and he’s not the same man.”
const date = new Date();
date.setMonth(4);
const date = new Date("2022-03-25");
This is a semantic smell. We need to model which attributes/behaviors are essential and which are accidental.
Objects can mutate in accidental ways; not in essential ones.
Code Smells are just my opinion.
Changes in software design will eventually mean "one step forward, two steps back". It is inevitable.
Salman Arshad
We see small primitive data everywhere
TL;DR: Don't forget to model the smallest ones
Find responsibilities for small objects in the MAPPER.
Mapping to dates violates abstraction and fail-fast principles.
in the Wordle TDD Kata, we describe a Wordle word to be different than a String or Char(5), since they don't have the same responsibilities.
public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
}
public class Name {
private final String name;
public Name(String name) {
this.name = name;
// Name has its own creation rules, comparison etc.
// Might be different than a string
}
}
public class Person {
private final Name name;
public Person(Name name) {
// name is created as a valid one,
// we don't need to add validations here
this.name = name;
}
}
There's no silver bullet in choosing how and when to map something.
Code Smell 122 - Primitive Obsession
How to Create a Wordle with TDD in Javascript
Code Smells are just my opinion.
The secret to building large apps is never build large apps. Break your applications into small pieces. Then, assemble those testable, bite-sized pieces into your big application.
Justin Meyer
Invisible objects have rules we need to enforce at a single point.
TL;DR: Create Small objects and restrict your domain.
EmailAddresses are a subset of string.
Valid Ages are a subset of Real.
Ports are a subset of Integers.
A wordle word is a subset of String.
destination = "[email protected]"
destination = "destination.example.com"
// No error thrown
public class EmailAddress {
public String emailAddress;
public EmailAddress(String address) {
string expressions = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
if (!Regex.IsMatch(email, expressions) {
throw new Exception('Invalid address');
}
this.emailAddress = address;
}
}
destination = new EmailAddress("[email protected]");
Not to be confused with the anemic
Code Smell 122 - Primitive Obsession
Code Smells are just my opinion.
Every craftsman starts his or her journey with a basic set of good-quality tools.
Andrew Hunt
Every software has a list of known bugs. Why?
TL;DR: Don't track bugs. Fix them.
Stop calling it a Bug
Reproduce the Defect.
<?
function divide($numerator, $denominator) {
return $numerator / $denominator;
// FIXME denominator value might be 0
// TODO Rename function
}
<?
function integerDivide($numerator, $denominator) {
if (denominator == 0) {
throw new DivideByZero();
}
return $numerator / $denominator;
}
// we pay our debts
Code Smells are just my opinion.
In general, the longer you wait before fixing a bug, the costlier (in time and money) it is to fix.
Joel Spolsky
Bitwise operators are faster. Avoid these micro-optimizations.
TL;DR: Don't use bitwise operators unless your business model is bitwise logic.
const nowInSeconds = ~~(Date.now() / 1000)
const nowInSeconds = Math.floor(Date.now() / 1000)
Code Smell 20 - Premature Optimization
Code Smell 165 - Empty Exception Blocks
Code Smell 06 - Too Clever Programmer
Code Smell 129 - Structural Optimizations
Code Smells are just my opinion.
Watch the little things; a small leak will sink a great ship.
Benjamin Franklin