Infinite code smells!
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.)
People Mentioned
Companies Mentioned
Coin Mentioned
Photo by on
Infinite code, smells!
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.)
var fs = require('fs');
function logTextWasAdded(err) {
if(err) return console.log(err);
console.log('Information added');
};
function addData(error, actualText) {
if (error) return console.log(error);
actualText = actualText + '\n' + 'Add data';
fs.writeFile(fileWithData, actualText, logTextWasAdded);
}
var fileWithData = 'hello.world';
fs.readFile(fileWithData, 'utf8', addData);
Detection
This problem shines at the naked eye. Many linters can detect this complexity and warn us.
Tags
Readability
Complexity
Conclusion
Callback Hell is a very common problem in programming languages with futures or promises.
Callbacks are added in an incremental way. There's no much mess at the beginning.
Complexity without refactoring makes them hard to read and debug.
There are two ways to write code: write code so simple there are obviously no bugs in it, or write code so complex that there are no obvious bugs in it.
Tony Hoare
Code Smell 79 - TheResult
If a name is already used, we can always prefix it with 'the'.
TL;DR: don't prefix your variables.
Problems
Readability
Meaningless names
Solutions
Use intention revealing names.
Avoid Indistinct noise words.
Sample Code
Wrong
var result;
result = getSomeResult();
var theResult;
theResult = getSomeResult();
Right
var averageSalary;
averageSalary = calculateAverageSalary();
//..
var averageSalaryWithRaises;
averageSalaryWithRaises = calculateAverageSalary();
Detection
As with many of our naming conventions, we can instruct our linters to forbid names like theXxx....
Tags
Readability
Conclusion
Always use intention revealing names.
If your names collide use local names, extract your methods and avoid 'the' prefixes.
Photo by on
One difference between a smart programmer and a professional programmer is that the professional understands that clarity is king. Professionals use their powers for good and write code that others can understand.
Robert C. Martin
Code Smell 80 - Nested Try/Catch
Exceptions are a great way of separating happy path versus trouble path. But we tend to over-complicate our solutions.
TL;DR: Don't nest Exceptions. Nobody cares of what you do in the inner blocks.
Problems
Readability
Solutions
Refactor
Sample Code
Wrong
try {
transaction.commit();
} catch (e) {
logerror(e);
if (e instanceOf DBError){
try {
transaction.rollback();
} catch (e) {
doMoreLoggingRollbackFailed(e);
}
}
}
//Nested Try catchs
//Exception cases are
//more important than happy path
//We use exceptions as control flow
Right
try {
transaction.commit();
} catch (transactionError) {
this.withTransactionErrorDo(
transationError, transaction);
}
//transaction error policy is not defined in this function
//so we don't have repeated code
//code is more readable
//It is up to the transaction
//and the error to decide what to do
Detection
We can detect this smell using parsing trees.
Tags
Exceptions
Conclusion
Don't abuse exceptions, don't create Exception classes no one will ever catch, and don't be prepared for every case (unless you have a good real scenario with a covering test).
Happy path should always be more important than exception cases.