DEV Community

Cover image for Code Smell 163 - Collection in Name
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

4 1

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
}
Enter fullscreen mode Exit fullscreen mode

Right

foreach (var customer in customers)
{
    // iterate with current customer
}
Enter fullscreen mode Exit fullscreen mode

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.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay