visit
Most of these smells are just hints of something that might be wrong. Therefore, they are not required to be fixed per se… (You should look into it, though.)
You can find all the previous code smells (Part i - XXVIII) here.
Have you ever seen an IEngine in the wild?
TL;DR: Don't prefix or suffix your classes
public interface IEngine
{
void Start();
}
public class ACar
{
}
public class ImplCar
{
}
public class CarImpl
{
}
public interface Engine
{
void Start();
}
public class Vehicle
{
}
public class Car
{
}
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
Jamie Zawinski
Software Engineering Great Quotes
Accessing a database in domain objects is a code smell. Doing it in a constructor is a double smell.
TL;DR: Constructors should construct (and probably initialize) objects.
According to the single responsibility principle, they should only build valid objects
public class Person {
int childrenCount;
public Person(int id) {
childrenCount = database.sqlCall("SELECT COUNT(CHILDREN) FROM PERSON WHERE ID = " . id);
}
}
public class Person {
int childrenCount;
// Create a class constructor for the Main class
public Person(int id, int childrenCount) {
childrenCount = childrenCount;
// We can assign the number in the constructor
// Accidental Database is decoupled
// We can test the object
}
}
My belief is still, if you get the data structures and their invariants right, most of the code will kind of write itself.
Peter Deustch
Software Engineering Great Quotes
Some objects are always together. Why don't we split them?
TL;DR: Make cohesive primitive objects travel together
If two or more primitive objects are glued together, with business logic repeated and rules between them, we need to find the existing concept of the bijection.
public class DinnerTable
{
public DinnerTable(Person guest, DateTime from, DateTime to)
{
Guest = guest;
From = from;
To = to;
}
private Person Guest;
private DateTime From;
private DateTime To;
}
public class TimeInterval
{
public TimeInterval(DateTime from, DateTime to)
{
// We should validate From < To
From = from;
To = to;
}
}
public DinnerTable(Person guest, DateTime from, DateTime to)
{
Guest = guest;
Interval = new TimeInterval(from, to);
}
// Even Better...
public DinnerTable(Person guest, Interval reservationTime)
{
Guest = guest;
Interval = reservationTime;
}
Code Smell 122 - Primitive Obsession
Code Smell 27 - Associative Arrays
The heart of the software is its ability to solve domain-related problems for its user. All other features, vital though they may be, support this basic purpose.
Eric Evans
Software Engineering Great Quotes
We have heard a lot about NFTs. Now, we master the Fungible concept.
TL;DR: Respect the MAPPER. Make fungible what is Fungible in real-world and vice-versa.
Fungibility is the property of a good or a commodity whose individual units are essentially interchangeable and each of whose parts is indistinguishable from another part.
When mapping our objects with real ones, we sometimes forget about the partial model and build over the design.
public class Person implements Serializable {
private final String firstName;
private final String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
shoppingQueueSystem.queue(new Person('John', 'Doe'));
public class Person {
}
shoppingQueueSystem.queue(new Person());
// The identity is irrelevant for queue simulation
People think that computer science is the art of geniuses but the actual reality is the opposite, just many people doing things that build on each other, like a wall of mini stones.
Donald Knuth
Don't use boolean evaluation as a readability shortcut.
TL;DR: Don't use Boolean comparison for side effect functions.
userIsValid() && logUserIn();
// this expression is short circuit
// Does not value second statement
// Unless the first one is true
functionDefinedOrNot && functionDefinedOrNot();
// in some languages undefined works as a false
// If functionDefinedOrNot is not defined does
// not raise an error and neither runs
if (userIsValid()) {
logUserIn();
}
if(typeof functionDefinedOrNot == 'function') {
functionDefinedOrNot();
}
// Checking for a type is another code smell
Code Smell 140 - Short Circuit Evaluation
Code Smell 06 - Too Clever Programmer
A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match.
Bill Bryson