It smells because there are likely many instances where it could be edited or improved.
Most of these smells are just hints of something that might be wrong. They are not required fixed per se… (You should look into it though.)
Let's continue...
Changing a collection while traversing might lead to unexpected errors
TL;DR: Do not modify collections while traversing them
We over-optimize our solutions with the prejudice that copying collections are expensive.
This is not true for small and medium-size collections.
Languages iterate collections in many different ways.
Modifying them is generally not safe.
Collection<Integer> people = new ArrayList<>();
// here we add elements to the collection...
for (Object person : people) {
if (condition(person)) {
people.remove(person);
}
}
// We iterate AND remove elements
Collection<Integer> people = new ArrayList<>();
// Here we add elements to the collection...
List<Object> iterationPeople = ImmutableList.copyOf(people);
for (Object person : iterationPeople) {
if (condition(person)) {
people.remove(person);
}
}
// We iterate a copy and remove it from original
coll.removeIf(currentIndex -> currentIndex == 5);
// Or use language tools (if available)
Many languages provide control both in compile and run-time
This is something we learn in our first courses.
It happens a lot in the industry and real-world software
Code Smell 53 - Explicit Iteration
Photo by Christine Roy on Unsplash
Bugs are bugs. You write code with bugs because you do. If it’s a safe language in the sense of run-time safe, the operating system crashes instead of doing a buffer overflow in a way that’s exploitable.
- Ken Thompson
Software Engineering Great Quotes
If you work on unit testing, sooner or later you will face this dilemma
TL;DR: Don't test your private methods.
We test our classes and methods.
At some point, we rely on auxiliary computations and we need to test them in a white-box way.
<?
final class Star {
private $distanceInParsecs;
public function timeToReachLightToUs() {
return $this->convertDistanceInParsecsToLightYears($this->distanceInParsecs);
}
private function convertDistanceInParsecsToLightYears($distanceInParsecs) {
return 3.26 * $distanceInParsecs;
// function is using an argument which is already available.
// since it has private access to $distanceInParsecs
// this is another smell indicator.
// We cannot test this function since it is private
}
}
<?
final class Star {
private $distanceInParsecs;
public function timeToReachLightToUs() {
return new ParsecsToLightYearsConverter($this->distanceInParsecs);
}
}
final class ParsecsToLightYearsConverter {
public function convert($distanceInParsecs) {
return 3.26 * $distanceInParsecs;
}
}
final class ParsecsToLightYearsConverterTest extends TestCase {
public function testConvert0ParsecsReturns0LightYears() {
$this->assertEquals(0, (new ParsecsToLightYearsConverter)->convert(0));
}
// we can add lots of tests and rely on this object
// So we don't need to test Star conversions.
// We can yet test Star public timeToReachLightToUs()
// This is a simplified scenario
}
This is a semantic smell.
We can only find metaprogramming abuse on some unit frameworks.
With this guide, we should always choose the method object solution.
Code Smell 21 - Anonymous Functions Abusers
Code Smell 18 - Static Functions
Photo by Dan Nelson on Unsplash
Just as it is a good practice to make all fields private unless they need greater visibility, it is a good practice to make all fields final unless they need to be mutable.
- Brian Goetz
Software Engineering Great Quotes
Use entity domain names to model entity domain objects.
TL;DR: Don't name your variables as Data.
We use 'data' a lot in our variables.
We are used to doing it.
Using this kind of name favors the anemic treatment of objects.
We should think about domain-specific and role-suggesting names.
if (!dataExists()) {
return '<div>Loading Data...</div>';
}
if (!peopleFound()) {
return '<div>Loading People...</div>';
}
We can check for this substring on our code and warn our developers.
Data is everywhere if you see the world as only data.
We can never see the data we manipulate.
We can only infer it through behavior.
We don't know the current temperature. We observe our thermometer pointing at 35 Degrees.
Our variables should reflect the domain and role they are fulfilling.
Naming them as 'data' is lazy and hinders readability.
Code Smell 65 - Variables Named after Types
What exactly is a name - Part II Rehab
Twenty percent of all input forms filled out by people contain bad data.
- Dennis Ritchie
Software Engineering Great Quotes
Have you encountered classes without behavior? Classes are their behavior.
TL;DR: Remove all empty classes.
Many developers still think classes are data repositories.
They couple different behavior concepts with returning different data.
class ShopItem {
code() { }
description() { }
}
class BookItem extends ShopItem {
code() { return 'book' }
description() { return 'some book'}
}
// concrete Class has no real behavior, just return different 'data'
class ShopItem {
constructor(code, description) {
// validate code and description
this._code = code;
this._description = description;
}
code() { return this._code }
description() { return this._description }
// dd more functions to avoid anemic classes
// getters are also code smells, so we need to iterate it
}
bookItem = new ShopItem('book', 'some book);
// create more items
Several linters warn us of empty classes.
We can also make our own scripts using metaprogramming.
Classes are what they do, their behavior.
Empty classes do nothing.
Code Smell 26 - Exceptions Polluting
Code Smell 60 - Global Classes
Photo by Kelly Sikkema on Unsplash
An error arises from treating object variables (instance variables) as if they were data attributes and then creating your hierarchy based on shared attributes. Always create hierarchies based on shared behaviors, side.
- David West
Software Engineering Great Quotes
Booleans are natural code smells. Returning and casting them is sometimes a mistake.
TL;DR: Don't return true or false. Be declarative.
Dealing with low-level abstractions, we usually return booleans.
When we create complex and mature software, we start to forget about this primitive obsession and care about real-world rules and identities.
boolean isEven(int num) {
if(num%2 == 0) {
return true;
} else {
return false;}
}
boolean isEven(int numberToCheck) {
// We decouple the what (to check for even or odd)
// With how (the algorithm)
return (numberToCheck % 2 == 0);
}
Many linters can check syntactic trees and look for explicit true/value returns.
Search on code libraries for return true statements and try to replace them when possible.
Code Smell 36 - Switch/case/elseif/else/if statements
Photo by engin akyurt on Unsplash
The good news is: Anything is possible on your computer. The bad news is: Nothing is easy.
- Ted Nelson
Software Engineering Great Quotes
And that’s all for now…
The next article will explain 5 more code smells!