Code Smell 163 - Collection in Name

Photo by Mick Haupt on Unsplash

Code Smell 163 - Collection in Name

Have you ever seen a CustomerCollection?

TL;DR: Don't use 'collection' in your name. It is too abstract for concrete concepts.

Problems

Solutions

  1. Rename the collection with a specific name.

Context

Naming is very important.

We need to deal a lot with collections.

Collections are amazing since they don't need nulls to model the absence.

An empty collection is polymorphic with a full collection.

We avoid nulls and IFs.

We often use bad and vague names instead of looking for good names in the MAPPER.

Sample Code

Wrong

foreach (var customer in customerCollection)
{
    // iterate with current customer
}

foreach (var customer in customersCollection)
{
    // iterate with current customer
}

Right

foreach (var customer in customers)
{
    // iterate with current customer
}

Detection

[X] Semi-Automatic

All linters can detect a bad naming like this.

It can also lead to false positives so we must be cautious.

Tags

  • Naming

Conclusion

We need to care for all our clean code, variables, classes, and functions.

Accurate names are essential to understand our code.

Relations

More Info

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Mick Haupt on Unsplash


Alzheimer's Law of Programming: Looking at code you wrote more than two weeks ago is like looking at code you are seeing for the first time.

Dan Hurvitz


This article is part of the CodeSmell Series.