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 - XXXI) here.
We learn if/else on our first programming day. Then we forget the else.
TL;DR: Be explicit. Even with Else.
Afterward, we Remove the IF and use polymorphism.
function carBrandImplicit(model) {
if (model === 'A4') {
return 'audi';
}
return 'Mercedes-Benz';
}
function carBrandExplicit(model) {
if (model === 'A4') {
return 'audi';
}
if (model === 'AMG') {
return 'Mercedes-Benz';
}
// Fail Fast
throw new Exception('Model not found);
}
This kind of smell brings a lot of public debate and hate.
Code Smell 36 - Switch/case/else if/else/if statements
How to Get Rid of Annoying IFs Forever
The biggest issue on software teams is making sure everyone understands what everyone else is doing.
Martin Fowler
Today, I expected a payment in my wallet. The balance was 0. I panicked.
TL;DR: Null is not 0. Error is not 0. just 0 is 0.
"""
Below code is automatically generated by code-davinci-002 on GTP3 Codex
1. check balance with blockchain
2. If blockchain is unreachable show 0 as the balance
"""
import requests
import json
def get_balance(address):
url = "//blockchain.info/q/addressbalance/" + address
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return 0
"""
Below code is automatically generated by code-davinci-002 on GTP3 Codex
1. check balance with blockchain
2. If blockchain is unreachable throw an error
"""
import requests
import json
def get_balance(address):
url = "//blockchain.info/q/addressbalance/" + address
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
raise BlockchainNotReachableError("Error reaching blockchain")
We can find patterns when an exception or return code is thrown and masked with a 0.
Code Smell 139 - Business Code in the User Interface
Code Smell 73 - Exceptions for Expected Cases
Null: The Billion Dollar Mistake
Code Smells are just my opinion.
My real criticism with Null is that it brings back again unnecessarily all the agony of having to choose whether to run your program fast without checking or run it slow with checking.
Tony Hoare (Null Inventor)
Software Engineering Great Quotes
You assign a value to a variable and use it but never change it.
TL;DR: Be declarative on mutability.
Refactoring 003 - Extract Constant
Sometimes we guess that a value can change with the MAPPER.
This will also avoid Magic Constants.
<?php
function configureUser() {
$password = '123456';
// Setting a password on a variable is another vulnerability
// And Code Smell
$user = new User($password);
// Notice Variable doesn't change
}
<?php
define("USER_PASSWORD", '123456')
function configureUser() {
$user = new User(USER_PASSWORD);
}
// or
function configureUser() {
$user = new User(userPassword());
}
function userPassword() : string {
return '123456';
}
// Case is an oversimplification as usual
We must challenge ourselves and refactor when the variable scope is clear, and we learn more about its properties and mutability.
Code Smell 116 - Variables Declared With 'var'
Code Smell 127 - Mutable Constants
Code Smell 107 - Variables Reuse
Code Smell 02 - Constants and Magic Numbers
Code Smells are just my opinion.
A complex system that works is invariably found to have evolved from a simple system that worked.
John Gall
Serious development is done by many different people. We have to start agreeing.
TL;DR: Don't mix different case conversions
{
"id": 2,
"userId": 666,
"accountNumber": "12345-12345-12345",
"UPDATED_AT": "2022-01-07T02:23:41.305Z",
"created_at": "2019-01-07T02:23:41.305Z",
"deleted at": "2022-01-07T02:23:41.305Z"
}
{
"id": 2,
"userId": 666,
"accountNumber": "12345-12345-12345",
"updatedAt": "2022-01-07T02:23:41.305Z",
"createdAt": "2019-01-07T02:23:41.305Z",
"deletedAt": "2022-01-07T02:23:41.305Z"
// This doesn't mean THIS standard is the right one
}
We can tell our linters about our company's broad naming standards and enforce them.
Code Smell 48 - Code Without Standards
What exactly is a name - Part I The Quest
Code Smells are just my opinion.
If you have too many special cases, you are doing it wrong.
Craig Zerouni
Maxint is a very good number for an invalid ID. We will never reach it.
TL;DR: Don't couple real IDs with invalid ones. In fact: Avoid IDs.
Then we invented The billion-dollar mistake.
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
#define INVALID_VALUE 999
int main(void)
{
int id = get_value();
if (id==INVALID_VALUE)
{
return EXIT_FAILURE;
// id is a flag and also a valid domain value
}
return id;
}
int get_value()
{
// something bad happened
return INVALID_VALUE;
}
// returns EXIT_FAILURE (1)
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
// No INVALID_VALUE defined
int main(void)
{
int id;
id = get_value();
if (!id)
{
return EXIT_FAILURE;
// Sadly, C Programming Language has no exceptions
}
return id;
}
get_value()
{
// something bad happened
return false;
}
// returns EXIT_FAILURE (1)
Code Smell 120 - Sequential IDs
Null: The Billion Dollar Mistake
Y2K22 - The Mistake That Embarrasses Us
Code Smells are just my opinion.
Bugs lurk in corners and congregate at boundaries.
Boris Beizer