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 - XXIX) here
Comments are a code Smell. Getters are another code smell. Guess what?
TL;DR: Don't use getters. Don't comment getters
pragma solidity >=0.5.0 <0.9.0;
contract Property {
int private price;
function getPrice() public view returns(int) {
/* returns the Price */
return price;
}
}
pragma solidity >=0.5.0 <0.9.0;
contract Property {
int private _price;
function price() public view returns(int) {
return _price;
}
}
Code Smell 05 - Comment Abusers
Photo by Reimond de Zuñiga on Unsplash
Code should be remarkably expressive to avoid most of the comments. There'll be a few exceptions, but we should see comments as a 'failure of expression' until proven wrong.
Robert Martin
Software Engineering Great Quotes
Util classes are great to gather protocol.
TL;DR: Don't add accidental protocol to your classes
public class MyHelperClass {
public void print() { }
public void format() { }
// ... many methods more
// ... even more methods
public void persist() { }
public void solveFermiParadox() { }
}
public class Printer {
public void print() { }
}
public class DateToStringFormatter {
public void format() { }
}
public class Database {
public void persist() { }
}
public class RadioTelescope {
public void solveFermiParadox() { }
}
Code Smell 124 - Divergent Change
Code Smell 94 - Too Many imports
Code Smell 34 - Too Many Attributes
Photo by on
There is no code so big, twisted, or complex that maintenance can't make it worse.Gerald M. Weinberg
We buy debt for our future selves. It is payback time.
TL;DR: Don't leave TODOs in your code. Fix them!
public class Door
{
private Boolean isOpened;
public Door(boolean isOpened)
{
this.isOpened = isOpened;
}
public void openDoor()
{
this.isOpened = true;
}
public void closeDoor()
{
// TODO: Implement close door and cover it
}
}
public class Door
{
private Boolean isOpened;
public Door(boolean isOpened)
{
this.isOpened = isOpened;
}
public void openDoor()
{
this.isOpened = true;
}
public void closeDoor()
{
this.isOpened = false;
}
}
Photo by on
After you finish the first 90% of a project, you have to finish the other 90%.
Michael Abrash
Our code is more robust and legible. But we hide NULL under the rug.
TL;DR: Avoid Nulls and undefined. If you avoid them you will never need Optionals.
const user = {
name: 'Hacker'
};
if (user?.credentials?.notExpired) {
user.login();
}
user.functionDefinedOrNot?.();
// Seems compact but it is hacky and has lots
// of potential NULLs and Undefined
function login() {}
const user = {
name: 'Hacker',
credentials: { expired: false }
};
if (!user.credentials.expired) {
login();
}
// Also compact
// User is a real user or a polymorphic NullUser
// Credentials are always defined.
// Can be an instance of InvalidCredentials
// Assuming we eliminated nulls from our code
if (user.functionDefinedOrNot !== undefined) {
functionDefinedOrNot();
}
// This is also wrong.
// Explicit undefined checks are yet another code smell
This is a Language Feature.
We can detect it and remove it.
The good: remove all nulls from your code.
The bad: use optional chaining.
The ugly: not treating nulls at all.
Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)
Null: The Billion Dollar Mistake
How to Get Rid of Annoying IFs Forever
Photo by on
He who fights with monsters might take care lest he thereby become a monster. And if you gaze for long into an abyss, the abyss gazes also into you.
Nietzsche
Every developer compares attributes equally. They are mistaken.
TL;DR: Don't export and compare, just compare.
if (address.street == 'Broad Street') {
if (location.street == 'Bourbon St') {
// 15000 usages in a big system
// Comparisons are case sensitive
if (address.isAtStreet('Broad Street') {
}
// ...
if (location.isAtStreet('Bourbon St') {
}
// 15000 usages in a big system
function isAtStreet(street) {
// We can change Comparisons to
// case sensitive in just one place.
}
If some of our business rules change, we need to change a single point.
Code Smell 101 - Comparison Against Booleans
Code Smell 122 - Primitive Obsession
Photo by on
Behavior is the most important thing about software. It is what users depend on. Users like it when we add behavior (provided it is what they really wanted), but if we change or remove behavior they depend on (introduce bugs), they stop trusting us.
Michael Feathers