Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity

Wrap messy parameters into a single meaningful entity.


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Transform scattered inputs into one clear object

TL;DR: Wrap messy parameters into a single meaningful entity.

Problems Addressed 😔

Related Code Smells 💨

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xviii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ii-o96s3wl4

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxvi

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxix

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-x-i7r34uj

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxix

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-viii-8mn3352

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxviii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xix

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-iv-7sc3w8n

Steps 👣

  1. Identify multiple parameters of the same type
  2. Create a meaningful entity to group them
  3. Add missing validation rules to fail fast
  4. Replace function signatures with the new entity
  5. Adjust all callers to pass the entity
  6. Add context-specific names to improve clarity

Sample Code 💻

Before 🚨

function findHolidays(
 maxPrice: Currency, 
 startDate: Date,
 endDate: Date, 
 minPrice: Currency) {
  // Notice that maxPrice and minPrice are swapped by mistake
  // Also, dates are mixed
}

After 👉

// 2. Create a meaningful entity to group them  

class PriceRange {
  constructor(public min: Currency, public max: Currency) {
    if (min > max) {
      throw new Error(
        `Invalid price range: min (${min}) `+
        `cannot be greater than max (${max})`
      );
    }
    if (min < 0) {
      throw new Error(
        `Invalid price range: min (${min}) cannot be negative`);
    }
  }
}

class Interval {
  // 3. Add missing validation rules to fail-fast
  constructor(public start: Date, public end: Date) {
    if (start > end) {
      throw new Error(
        `Invalid date range: start (${start.toISOString()})  ` + 
        `cannot be after end (${end.toISOString()})`
      );
    }
  }
}

class HolidaySearchCriteria {
  constructor(
    public priceRange: PriceRange,
    public dateRange: Interval
  ) {}
}

function findHolidays(criteria: HolidaySearchCriteria): Holiday[] {
  // 1. Identify multiple parameters of the same type  
  // No need to call validate() - already validated in constructors
  // 4. Replace function signatures with the new entity  
  const { priceRange, dateRange } = criteria;
  // 5. Adjust all callers to pass the entity  
  // 6. Add context-specific names to improve clarity 

  return database.query({
    price: { $gte: priceRange.min, $lte: priceRange.max },
    startDate: { $gte: dateRange.start },
    endDate: { $lte: dateRange.end }
  });
}

try {
  const criteria = new HolidaySearchCriteria(
    new PriceRange(500, 1000),  // ✅ Valid
    new Inteval(
      new Date('2025-06-01'), 
      new Date('2025-06-15')
    )
  );

  findHolidays(criteria);

  // ❌ This will throw immediately
  // Great for UI and API validation
  new PriceRange(1000, 500);

} catch (error) {
  console.error(error.message);
}

Type 📝

  • Semi-Automatic

Safety 🛡️

Many IDEs support this pattern.

Why is the Code Better? ✨

You avoid order confusion and increase readability.

You make functions easy to extend with new parameters.

You bring semantic meaning to the input data.

You eliminate the risk of passing arguments in the wrong order since the object properties have explicit names.

You make function calls self-documenting because each value clearly indicates its purpose.

You simplify adding new optional parameters without breaking existing code.

You enable better IDE support with autocomplete showing parameter names.

You create opportunities to reuse the parameter object type across related functions.

You fail fast, asserting on the relations among parameters.

How Does it Improve the Bijection? 🗺️

You move closer to a one-to-one map between the business concept of a "search request" and your code model.

You stop treating the data as loose numbers and give them an explicit identity that matches the domain.

In the real world, you describe searches using named criteria rather than ordered lists.

When you ask someone to "search for products with a minimum price of 50 and a maximum price of 100," you use named concepts.

This refactoring mirrors that natural language structure in your code.

The SearchCriteria becomes a first-class concept that maps directly to how searching works in the real world.

Refactor with AI 🤖

Ask AI to scan your codebase for functions that use two or more parameters of the same type.

Instruct it to propose an entity name, generate the type or class, and rewrite both the function and its callers to use the new entity.

Suggested Prompt: 1. Identify multiple parameters of the same type 2. Create a meaningful entity to group them 3. Add missing validation rules to fail fast 4. Replace function signatures with the new entity 5. Adjust all callers to pass the entity 6. Add context-specific names to improve clarity

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | You | You | | Gemini | Gemini | | DeepSeek | DeepSeek | | Meta AI | Meta AI | | Grok | Grok | | Qwen | Qwen |

Tags 🏷️

  • Primitive Obsession

Level 🔋

  • [x] Intermediate

Related Refactorings 🔄

https://refactoring.guru/es/introduce-parameter-object?embedable=true

https://hackernoon.com/refactoring-013-eliminating-repeated-code-with-dry-principles?embedable=true

https://hackernoon.com/refactoring-019-how-to-reify-email-addresses?embedable=true

Also known as

https://refactoring.guru/es/introduce-parameter-object?embedable=true

Credits 🙏

Image by Gerd Altmann on Pixabay


This article is part of the Refactoring Series.

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings?embedable=true

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2025-10-06T02:57:07+00:00) Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity. Retrieved from https://www.scien.cx/2025/10/06/refactoring-034-wrapping-messy-parameters-into-a-single-meaningful-entity/

MLA
" » Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity." Maximiliano Contieri | Sciencx - Monday October 6, 2025, https://www.scien.cx/2025/10/06/refactoring-034-wrapping-messy-parameters-into-a-single-meaningful-entity/
HARVARD
Maximiliano Contieri | Sciencx Monday October 6, 2025 » Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity., viewed ,<https://www.scien.cx/2025/10/06/refactoring-034-wrapping-messy-parameters-into-a-single-meaningful-entity/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/06/refactoring-034-wrapping-messy-parameters-into-a-single-meaningful-entity/
CHICAGO
" » Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2025/10/06/refactoring-034-wrapping-messy-parameters-into-a-single-meaningful-entity/
IEEE
" » Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2025/10/06/refactoring-034-wrapping-messy-parameters-into-a-single-meaningful-entity/. [Accessed: ]
rf:citation
» Refactoring 034 – Wrapping Messy Parameters Into a Single Meaningful Entity | Maximiliano Contieri | Sciencx | https://www.scien.cx/2025/10/06/refactoring-034-wrapping-messy-parameters-into-a-single-meaningful-entity/ |

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.