visit
You can find all the previous code smells (Part i - XXXIX) here.
Let's continue...
TL;DR: Be very careful with Javascript Arrays.
const arrayWithFixedLength = new Array(3);
console.log(arrayWithFixedLength); // [ <5 empty items> ]
console.log(arrayWithFixedLength[0]); // Undefined
console.log(arrayWithFixedLength[1]); // Undefined
console.log(arrayWithFixedLength[3]); // Undefined
console.log(arrayWithFixedLength[4]); // Undefined
console.log(arrayWithFixedLength.length); // 3
const arrayWithTwoElements = new Array(3, 1);
console.log(arrayWithTwoElements); // [ 3, 1 ]
console.log(arrayWithTwoElements[0]); // 3
console.log(arrayWithTwoElements[1]); // 1
console.log(arrayWithTwoElements[2]); // Undefined
console.log(arrayWithTwoElements[5]); // Undefined
console.log(arrayWithTwoElements.length); // 2
const arrayWithTwoElementsLiteral = [3,1];
console.log(arrayWithTwoElementsLiteral); // [ 3, 1 ]
console.log(arrayWithTwoElementsLiteral[0]); // 3
console.log(arrayWithTwoElementsLiteral[1]); // 1
console.log(arrayWithTwoElementsLiteral[2]); // Undefined
console.log(arrayWithTwoElementsLiteral[5]); // Undefined
console.log(arrayWithTwoElementsLiteral.length); // 2
Code Smell 06 - Too Clever Programmer
Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)
Code Smell 93 - Send me Anything
Code Smells are just my opinion.
When someone says, "I want a programming language in which I need only say what I want done," give him a lollipop.
Alan J. Perlis
Software Engineering Great Quotes
struct WEBBExoplanet {
name: String,
mass: f64,
radius: f64,
distance: f64,
orbital_period: f64,
}
struct WEBBGalaxy {
name: String,
classification: String,
distance: f64,
age: f64,
}
struct Exoplanet {
name: String,
mass: f64,
radius: f64,
distance: f64,
orbital_period: f64,
}
struct Galaxy {
name: String,
classification: String,
distance: f64,
age: f64,
}
Code Smell 141 - IEngine , AVehicle, ImplCar
Code Smell 174 - Class Name in Attributes
What exactly is a name - Part II Rehab
Code Smells are my opinion.
The most dangerous kind of waste is the waste we do not recognize.
Shigeo Shingo
Software is about contracts and ambiguous contracts are a nightmare
TL;DR: Keep your code explicit
tenCentimeters = 10
tenInches = 10
tenCentimeters + tenInches
# 20
# this error is based on the hidden assumption of a unit (any)
# and caused the Mars Climate Orbiter failure
class Unit:
def __init__(self, name, symbol):
self.name = name
self.symbol = symbol
class Measure:
def __init__(self, scalar, unit):
self.scalar = scalar
self.unit = unit
def __str__(self):
return f"{self.scalar} {self.unit.symbol}"
centimetersUnit = Unit("centimeters", "cm")
inchesUnit = Unit("inches", "in")
tencCentimeters = Measure(10, centimeters)
tenInches = Measure(10, inches)
tenCentimeters + tenInches
# error until we introduce a conversion factor
# in this case the conversion is constant
# inches = centimeters / 2.54
Code Smell 02 - Constants and Magic Numbers
Coupling - The one and only software design problem
Code Smells are my opinion.
A human organization is just as much an information system as any computer system. It is almost certainly more complex, but the same fundamental ideas apply. Things that are fundamentally difficult, like concurrency and coupling, are difficult in the real world of people, too.
Dave Farley
A code that either returns or no returns at all
TL;DR: Check carefully your boolean expressions
# Gratuitous boolean expressions
if a > 0 and True:
print("a is positive")
else:
print("a is not positive")
if a > 0:
print("a is positive")
else:
print("a is not positive")
Code Smell 101 - Comparison Against Booleans
How to Get Rid of Annoying IFs Forever
Code Smells are just my opinion.
The central enemy of reliability is complexity.
Daniel Geer
An object that appears and disappears mysteriously
TL;DR: Add the necessary indirection layers, but no more.
public class Driver
{
private Car car;
public Driver(Car car)
{
this.car = car;
}
public void DriveCar()
{
car.driveCar();
}
}
Car porsche = new Car();
Driver homer = new Driver(porsche);
homer.DriveCar();
Car porsche = new Car();
porsche.driveCar();
// We don't need the driver
Remove middleman objects if they are not needed.
Code Smells are my opinion.
The art of programming is the art of organizing complexity, of mastering multitude and avoiding its bastard chaos as effectively as possible.
E. W. Dijkstra