cover-img

Code Smell 65 - Variables Named after Types

Names should always indicate role.

25 March, 2023

3

3

0

Problems

  • Declarative
  • Design for Change
  • Coupling to accidental implementation

Solutions

  1. Rename your variable according to the role.

Sample Code

Wrong

public bool CheckIfStringHas3To7LowercaseCharsFollowedBy3or4Numbers(string string)
{
Regex regex = new Regex(@"[a-z]{2,7}[1-9]{3,4}")
var hasMatch = regex.IsMatch(string);
return hasMatch;
}

Right

public bool CheckIfStringHas3To7LowercaseharsFollowedBy3or4Numbers(string password)
{
Regex stringHas3To7LowercaseCharsFollowedBy3or4Numbers = new Regex(@"[a-z]{2,7}[1-9]{3,4}")
var hasMatch = stringHas3To7LowercaseCharsFollowedBy3or4Numbers.IsMatch(password);
return hasMatch;
}

Detection

This is a semantic rule. We can instruct our linters to warn us from using names related to existing classes, types o reserved words since they are too implementative.

Tags

  • Declarative

Conclusion

The first name we can across is related to an accidental point of view. It takes time to build a theory on the models we are building using our MAPPERS. Once we get there, we must rename our variables-

Relations

Code Smell 38 - Abstract Names

More Info

https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab

http://xunitpatterns.com/Intent%20Revealing%20Name.html

Credits

Photo by Sangga Rima Roman Selia on Unsplash

This idea came from this tweet

https://twitter.com/1377522389312008193


Types are essentially assertions about a program. And I think it’s valuable to have things be as absolutely simple as possible, including not even saying what the types are.

Dan Ingalls


Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

3

3

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.