Code Smell 184 — Exception Arrow Code

Code Smell 184 — Exception Arrow CodeArrow code is a code smell. Exception polluting is another. This is a mortal combination.TL;DR: Don’t cascade your exceptionsProblemsReadabilityComplexitySolutionsRewrite the nested clausesContextIn the same way arr…


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri

Code Smell 184 — Exception Arrow Code

Arrow code is a code smell. Exception polluting is another. This is a mortal combination.

TL;DR: Don’t cascade your exceptions

Problems

  • Readability
  • Complexity

Solutions

  1. Rewrite the nested clauses

Context

In the same way arrow code is hard to read, handling exceptions is a usual case when we must address the topics in a cascade way.

Sample Code

Wrong

class QuotesSaver {
public void Save(string filename) {
if (FileSystem.IsPathValid(filename)) {
if (FileSystem.ParentDirectoryExists(filename)) {
if (!FileSystem.Exists(filename)) {
this.SaveOnValidFilename(filename);
} else {
throw new I0Exception("File exists: " + filename);
}
} else {
throw new I0Exception("Parent directory missing at " + filename);
}
} else {
throw new ArgumentException("Invalid path " + filename);
}
}
}

Right

public class QuoteseSaver {
public void Save(string filename) {
if (!FileSystem.IsPathValid(filename)) {
throw new ArgumentException("Invalid path " + filename);
} else if (!FileSystem.ParentDirectoryExists(filename)) {
throw new I0Exception("Parent directory missing at " + filename);
} else if (FileSystem.Exists(filename)) {
throw new I0Exception("File exists: " + filename);
}
this.SaveOnValidFilename(filename);
}
}

Detection

[X] Semi-Automatic

Some linters warn us when we have this kind of complexity

Tags

  • Exceptions

Conclusion

Exceptions are less critical than normal cases.

If we need to read more exceptional code than normal then it is time to improve our code.

Relations

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Remy Gieling on Unsplash

An error doesn’t become a mistake until you refuse to correct it.

Orlando Aloysius Battista

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code


Code Smell 184 — Exception Arrow Code was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2022-12-06T12:37:03+00:00) Code Smell 184 — Exception Arrow Code. Retrieved from https://www.scien.cx/2022/12/06/code-smell-184-exception-arrow-code/

MLA
" » Code Smell 184 — Exception Arrow Code." Maximiliano Contieri | Sciencx - Tuesday December 6, 2022, https://www.scien.cx/2022/12/06/code-smell-184-exception-arrow-code/
HARVARD
Maximiliano Contieri | Sciencx Tuesday December 6, 2022 » Code Smell 184 — Exception Arrow Code., viewed ,<https://www.scien.cx/2022/12/06/code-smell-184-exception-arrow-code/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 184 — Exception Arrow Code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/06/code-smell-184-exception-arrow-code/
CHICAGO
" » Code Smell 184 — Exception Arrow Code." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2022/12/06/code-smell-184-exception-arrow-code/
IEEE
" » Code Smell 184 — Exception Arrow Code." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/12/06/code-smell-184-exception-arrow-code/. [Accessed: ]
rf:citation
» Code Smell 184 — Exception Arrow Code | Maximiliano Contieri | Sciencx | https://www.scien.cx/2022/12/06/code-smell-184-exception-arrow-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.