DEV Community

Cover image for Code Smell 233 - Collections Count
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3

Code Smell 233 - Collections Count

You count collections or collections.count?

TL;DR: Chose narrow names

Problems

  • Bad Naming

Solutions

  1. Accurately describe your collections

Context

Names are significant and should not deceive the reader.

You name things and lose the scope of the name.

It is important to be accurate of the expected reference on the names.

Sample Code

Wrong

const standardModelParticles = {
  quarks: [
    {
      name: "Up",
      charge: "2/3",
      type: "Quark",
    },
    {
      name: "Down",
      charge: "-1/3",
      type: "Quark",
    },
    // ...
  ],
  leptons: [
    {
      name: "Electron",
      charge: "-1",
      type: "Lepton",
    },
    {
      name: "Muon",
      charge: "-1",
      type: "Lepton",
    },
    // ...
  ],
  gaugeBosons: [
    {
      name: "Photon",
      charge: "0",
      type: "Boson",
    },
    {
      name: "W Boson",
      charge: "±1",
      type: "Boson",
    },
    // ...
  ],
  higgsBoson: [
    {
      name: "Higgs Boson",
      charge: "0",
      type: "Scalar Boson",
    },
  ],
};

const quarks = standardModelParticles.quarks.length; 
// Bad name. It does not represent a count
// But a Collection of things
Enter fullscreen mode Exit fullscreen mode

Right

const standardModelParticles = {
}; // Same as the "Wrong" example

const quarksCount = standardModelParticles.quarks.length; 
Enter fullscreen mode Exit fullscreen mode

Detection

[X] SemiAutomatic

Some linters can check the types and names and infer a mistake

Tags

  • Namings

Conclusion

Take care of your names.

Use automatic refactor tools whenever you come across a bad name.

Relations

Disclaimer

Code Smells are my opinion.

Credits

Photo by Sandy Millar on Unsplash


Some people are good programmers because they can handle many more details than most people. But there are a lot of disadvantages in selecting programmers for that reason — it can result in programs that no one else can maintain.

Butler Lampson


This article is part of the CodeSmell Series.

Warp.dev image

Warp is the highest-rated coding agent—proven by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay