Code Smell 150 — Equal Comparison

Code Smell 150 — Equal Comparison

Every developer compares attributes equally. They are mistaken.

TL;DR: Don’t export and compare, just compare.

Problems

  • Encapsulation break
  • Code Duplication
  • Information Hiding Violation
  • Anthropomorphism violation

Solutions

  1. Hide the comparison in a single method

Context

Attribute comparison is heavily used in our code.

We need to focus on behavior and responsibilities.

It is an object’s responsibility to compare with other objects. Not our own.

Premature Optimizers will tell us this is less performant.

We should ask them for real evidence and contrast the more maintainable solution.

Sample Code

Wrong

if (address.street == 'Broad Street') {
if (location.street == 'Bourbon St') {
// 15000 usages in a big system  
// Comparisons are case sensitive

Right

if (address.isAtStreet('Broad Street') {
}
// ...
if (location.isAtStreet('Bourbon St') {
}
// 15000 usages in a big system
function isAtStreet(street) {
// We can change Comparisons to case sensitive in just one place.
}

Detection

[X] Semi-Automatic

We can detect attribute comparison using syntax trees.

There can be good uses for primitive types as with many other smells.

Tags

  • Encapsulation

Conclusion

We need to put responsibilities in a single place.

Comparing is one of them.

If some of our business rules change we need to change a single point.

Relations

Credits

Photo by Piret Ilver on Unsplash

Behavior is the most important thing about software. It is what users depend on. Users like it when we add behavior (provided it is what they really wanted), but if we change or remove behavior they depend on (introduce bugs), they stop trusting us.

Michael Feathers

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code

Level Up Coding

Thanks for being a part of our community! Before you go:


Code Smell 150 — Equal Comparison 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

Code Smell 150 — Equal Comparison

Every developer compares attributes equally. They are mistaken.

TL;DR: Don’t export and compare, just compare.

Problems

  • Encapsulation break
  • Code Duplication
  • Information Hiding Violation
  • Anthropomorphism violation

Solutions

  1. Hide the comparison in a single method

Context

Attribute comparison is heavily used in our code.

We need to focus on behavior and responsibilities.

It is an object’s responsibility to compare with other objects. Not our own.

Premature Optimizers will tell us this is less performant.

We should ask them for real evidence and contrast the more maintainable solution.

Sample Code

Wrong

if (address.street == 'Broad Street') {
if (location.street == 'Bourbon St') {
// 15000 usages in a big system  
// Comparisons are case sensitive

Right

if (address.isAtStreet('Broad Street') {
}
// ...
if (location.isAtStreet('Bourbon St') {
}
// 15000 usages in a big system
function isAtStreet(street) {
// We can change Comparisons to case sensitive in just one place.
}

Detection

[X] Semi-Automatic

We can detect attribute comparison using syntax trees.

There can be good uses for primitive types as with many other smells.

Tags

  • Encapsulation

Conclusion

We need to put responsibilities in a single place.

Comparing is one of them.

If some of our business rules change we need to change a single point.

Relations

Credits

Photo by Piret Ilver on Unsplash

Behavior is the most important thing about software. It is what users depend on. Users like it when we add behavior (provided it is what they really wanted), but if we change or remove behavior they depend on (introduce bugs), they stop trusting us.

Michael Feathers

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code

Level Up Coding

Thanks for being a part of our community! Before you go:


Code Smell 150 — Equal Comparison 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-07-19T14:58:00+00:00) Code Smell 150 — Equal Comparison. Retrieved from https://www.scien.cx/2022/07/19/code-smell-150-equal-comparison/

MLA
" » Code Smell 150 — Equal Comparison." Maximiliano Contieri | Sciencx - Tuesday July 19, 2022, https://www.scien.cx/2022/07/19/code-smell-150-equal-comparison/
HARVARD
Maximiliano Contieri | Sciencx Tuesday July 19, 2022 » Code Smell 150 — Equal Comparison., viewed ,<https://www.scien.cx/2022/07/19/code-smell-150-equal-comparison/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 150 — Equal Comparison. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/19/code-smell-150-equal-comparison/
CHICAGO
" » Code Smell 150 — Equal Comparison." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2022/07/19/code-smell-150-equal-comparison/
IEEE
" » Code Smell 150 — Equal Comparison." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/07/19/code-smell-150-equal-comparison/. [Accessed: ]
rf:citation
» Code Smell 150 — Equal Comparison | Maximiliano Contieri | Sciencx | https://www.scien.cx/2022/07/19/code-smell-150-equal-comparison/ |

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.