DEV Community

Cover image for Code Smell 81 - Result
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

2 2

Code Smell 81 - Result

result = ???

TL;DR: Use good names always. Result is always a very bad name.

Problems

  • Readability

Solutions

  1. Rename result.

  2. If you don't know how to name it, just name the variable with the same name as the last function call.

  3. Don't use IDEs without automatic refactors.

Sample Code

Wrong

var result;

result = lastBlockchainBlock();
//

// Many function calls

addBlockAfter(result);
Enter fullscreen mode Exit fullscreen mode

Right

var lastBlockchainBlock;

lastBlockchainBlock = findlastBlockchainBlock();
//...

// Many function calls 
// we should refactor them to minimize space
// between variable definition and usage

addBlockAfter(lastBlockchainBlock);
Enter fullscreen mode Exit fullscreen mode

Detection

We must forbid the word result to be a variable name.

Tags

  • Readability

Conclusion

Result is an example of generic and meaningless names.

Refactoring is cheap and safe.

Always leave the campground cleaner than you found it.

When you find a mess on the ground, clean it, doesn’t matter who did it. Your job is to always leave the ground cleaner for the next campers.

Relations

More info

Credits

Photo by KMA . on Unsplash


Code is like humor. When you have to explain it, it’s bad.

Cory House


This article is part of the CodeSmell Series.

Dynatrace image

Observability should elevate – not hinder – the developer experience.

Is your troubleshooting toolset diminishing code output? With Dynatrace, developers stay in flow while debugging – reducing downtime and getting back to building faster.

Explore Observability for Developers

Top comments (6)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
mcsee profile image
Maxi Contieri

Hi Tim.

Thank you very much for your possitive feedback.
You are absolutly right.
I try to write code smells in many different languages to be language agnostic.
In this case, I chose JS (which I don't master as you see)
I made some mistakes.
Now, with your advice, I've corrected them.
Thank you very much!

Collapse
 
plaintextnerds profile image
Tim Armstrong

Cool, keep up the good work! I'll delete my comment.

Thread Thread
 
mcsee profile image
Maxi Contieri

There's no need. Thank YOU Tim!

Collapse
 
tudorhulban profile image
Tudor Hulban

hi,
code is about intent and with result we are sharing at the earliest the function exit value.

anyway your example is exaggerated as one could define result / res closer to function exit and also your function looks like doing too much :).

Collapse
 
mcsee profile image
Maxi Contieri

of course it is exagerated to show result problems.
even dough you define it and use it on next line you should definitively choose a better name.
Refactors are free

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay