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. 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 - XXXIII) here.
Let's continue...
Fatal error: Uncaught Error: Class 'logs_queries_web' not found in /var/www/html/query-line.php:78
Stack trace: #0 {main} thrown in /var/www/html/query-line.php on line 718
TL;DR: Catch your errors. Even the ones you don't expect.
Use a top-level handler.
Avoid languages favoring return codes.
Expect database and low-level errors.
Even in 2022, we can see "serious" websites showing casual users a stack or debugging message.
<?
Fatal error: Uncaught Error: Class 'MyClass'
not found in /nstest/src/Container.php:9
<?
// A user-defined exception handler function
function myException($exception) {
logError($exception->description())
// We don't show Exception to final users
}
// Set user-defined exception handler function
set_exception_handler("myException");
We can use mutation testing to simulate problems and see if they are handled correctly.
We need to keep maturing.
Our solutions shouldn't be sloppy.
We need to improve our reputation as serious software engineers.
Code Smells are just my opinion.
Photo by jesse orrico on Unsplash
80 percent of my problems are simple logic errors. 80 percent of the remaining problems are pointer errors. The remaining problems are hard.
Mark Donner
Software Engineering Great Quotes
Hashing guarantees two objects are different. Not that they are the same.
TL;DR: If you check for the hash, you should also check for equality
On Oct 7th, 2022, one of the larger blockchains had to be halted.
This news was shocking since most blockchains are decentralized by definition.
You can read a full article here:
How a Hacker Stole $566M USD Exploiting a Code Smell
public class Person {
public String name;
// Public attributes are another smell
@Override
public boolean equals(Person anotherPerson) {
return name.equals(anotherPerson.name);
}
@Override
public int hashCode() {
return (int)(Math.random()*256);
}
// This is just an example of non-correlation
// When using HashMaps we can make a mistake
// and guess the object is not present in the collection
}
public class Person {
public String name;
// Public attributes are another smell
@Override
public boolean equals(Person anotherPerson) {
return name.equals(anotherPerson.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
// This is just an example of non-correlation
}
Many linters have rules for hash and equality redefinition.
With mutation testing, we can seed different objects with the same hash and check our tests.
Every performance improvement has its drawbacks.
Caches and replications are notable examples.
We can (must) use them carefully.
Code Smell 150 - Equal Comparison
Code Smells are just my opinion.
This will surprise some of your readers, but my primary interest is not with computer security. I am primarily interested in writing software that works as intended.
Wietse Venema
Software Engineering Great Quotes
We need to make some changes. We need to be clear on why
TL;DR: Be declarative on your design or implementation decisions.
Sometimes we find arbitrary rules not so easily testable.
If we cannot write a failing test, we need to have a function with an excellent and declarative name instead of a comment.
// We need to run this process with more memory
set_memory("512k)
run_process();
increase_memory_to_avoid_false_positives();
run_process();
This is a semantic smell.
We can detect comments and warn us.
Code is prose. And design decisions should be narrative.
Code Smell 05 - Comment Abusers
Code Smell 75 - Comments Inside a Method
Code Smells are just my opinion.
Photo by Goh Rhy Yan on Unsplash
Programs, like people, get old. We can’t prevent aging, but we can understand its causes, limit its effects and reverse some of the damage.
Mario Fusco
Software Engineering Great Quotes
Don't make two or more things at once.
TL;DR: Try to be as atomic as possible in your methods
https://maximilianocontieri.com/refactoring-002-extract-method
If you name a method with 'And', you are probably missing an extract-and-break method opportunity.
calculatePrimeFactorsRemoveDuplicatesAndPrintThem()
// Three responsibilities
calculatePrimeFactors();
removeDuplicates();
printNumbers();
// Three different methods
// We can test them and reuse them
Some linters can warn us about methods including the term 'and'.
When making methods, it is very important to play some rubber duck story and tell ourselves if we are making things right.
%[https://maximilianocontieri.com/code-smell-85-and-functions]
Code Smells are just my opinion.
Photo by Scott Sanker on Unsplash
Learning the art of programming, like most other disciplines, consists of first learning the rules and then learning when to break them.
Joshua Bloch
Developing is great. refactoring is amazing. Don't make it at the same time
TL;DR: Don't change functionally and refactor at the same time.
Sometimes we detect a refactoring is needed for further development.
We are experts at learning.
We should put our solution on hold. Work on the refactoring, and continue with our solution.
getFactorial(n) {
return n * getFactorial(n);
}
// Rename and Change
factorial(n) {
return n * factorial(n-1);
}
// This is a very small example
// Things go works while dealing with huge code
getFactorial(n) {
return n * getFactorial(n);
}
// Change
getFactorial(n) {
return n * getFactorial(n-1);
}
// Run the tests
factorial(n) {
return n * factorial(n-1);
}
// Rename
This is a refactoring smell.
We should use a physical token.
Either we are in the refactoring stage or the developing stage.
Code Smells are just my opinion.
Photo by Dannie Jing on Unsplash
When I’m studying code, refactoring leads me to higher levels of understanding that I would otherwise miss. Those who dismiss comprehension refactoring as useless fiddling with the code don’t realize they never see the opportunities hidden behind the confusion.
Martin Fowler
5 more code smells are coming soon…