DEV Community

Cover image for Code Smell 87 - Inconsistent Parameters Sorting
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

2 2

Code Smell 87 - Inconsistent Parameters Sorting

Be consistent with the parameters you use. Code is prose.

TL;DR: Don't confuse you readers. Keep the order.

Problems

  • Readability

  • Consistency

Solutions

  1. Refactor and change parameters order.

  2. Use named parameters

Sample Code

Wrong

function giveFirstDoseOfVaccine(person, vaccine) {
  //
}

function giveSecondDoseOfVaccine(vaccine, person) {
  //
}


giveFirstDoseOfVaccine(jane, pfizer);
giveSecondDoseOfVaccine(jane, pfizer);  //Unnoticed mistake
Enter fullscreen mode Exit fullscreen mode

Right

function giveFirstDoseOfVaccine(person, vaccine) {
  //
}

function giveSecondDoseOfVaccine(person, vaccine) {
  //
}


giveFirstDoseOfVaccine(jane, pfizer);
giveSecondDoseOfVaccine(jane, pfizer);  //jane is immunized
Enter fullscreen mode Exit fullscreen mode

Detection

  • Some very smart linters may be able to compare arguments and hint for possible mistakes.

Tags

  • Readability

Conclusion

This is a very simple smell.

Readability is very important to avoid mistakes.

Relations

Credits

Photo by Lance Grandahl on Unsplash


Computers are good at following instructions, but not at reading your mind.

Donald Knuth


This article is part of the CodeSmell Series.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
jamesrweb profile image
James Robb

Usually for cases like this I would create a factory to avoid the ambiguity, something like:

type Vaccine = "pfizer" | "moderna"
type Patient = {
    name: string;
}
type PatientWithCoronaVaccineDetails = Patient & {
    vaccine: Vaccine;
    doses: number;
}

function vaccineFactoryForPatient(patient: PatientWithCoronaVaccineDetails) {
  // ...

  return {
    giveFirstDoseOfVaccine() {
        // ...
    },
    giveSecondDoseOfVaccine() {
        // ...
    }
  }
}
}
Enter fullscreen mode Exit fullscreen mode

This way we can avoid this category of issues in the first place.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay