<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code example &#8211; Rafael Bernard Araujo</title>
	<atom:link href="https://rafael.bernard-araujo.com/categoria/technology/programming/code-example/feed" rel="self" type="application/rss+xml" />
	<link>https://rafael.bernard-araujo.com</link>
	<description>desenvolvendo... while(!success){  try(); }</description>
	<lastBuildDate>Sun, 20 Sep 2026 22:29:32 +0000</lastBuildDate>
	<language>pt-BR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">21941730</site>	<item>
		<title>Introduce Parameter Object &#124; Refactoring Patterns</title>
		<link>https://rafael.bernard-araujo.com/introduce-parameter-object-refactoring-patterns.php</link>
					<comments>https://rafael.bernard-araujo.com/introduce-parameter-object-refactoring-patterns.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Wed, 22 Apr 2026 05:45:10 +0000</pubDate>
				<category><![CDATA[Code example]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Refactoring Patterns]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[refactoring patterns]]></category>
		<category><![CDATA[rust]]></category>
		<category><![CDATA[software architecture]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2357</guid>

					<description><![CDATA[This refactoring pattern involves grouping parameters that naturally go together into a single object. When you see a group of data items that regularly travel together, appearing in function after function, it\'s a sign they should be combined into a single object. Benefits: - Reduces the number of parameters (improved readability) - Groups related data [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>This refactoring pattern involves grouping parameters that naturally go together into a single object. When you see a group of data items that regularly travel together, appearing in function after function, it\'s a sign they should be combined into a single object. Benefits: - Reduces the number of parameters (improved readability) - Groups related data together (better organisation) - Makes relationships between data explicit - Easier to add new related data without changing function signatures - Enables moving behaviour related to the data into the new class - Reduces errors from parameter ordering mistakes When to use: - When you have a group of parameters that often appear together - When you see the same parameters in multiple function signatures - When parameters represent a cohesive concept - When you need to add more related parameters - When parameter ordering becomes confusing See <a href="https://refactoring.com/catalog/introduceParameterObject.html">https://refactoring.com/catalog/introduceParameterObject.html</a> <strong>BEFORE: Multiple primitive parameters passed around</strong> Problems: - Too many parameters (hard to remember and maintain) - Parameters are related, but the relationship is not explicit - Easy to mix up parameter order - Adding new related data requires changing all function signatures - Difficult to add validation or behaviour for the group ```php declare(strict_types=1);</p>
<p>namespace RefactoringPatterns;</p>
<p>class IntroduceParameterObject<br />
{<br />
public function amountInvoicedBefore(<br />
\DateTimeImmutable $startDate,<br />
\DateTimeImmutable $endDate,<br />
string $customerName,<br />
string $customerId,<br />
string $customerEmail<br />
): float {<br />
$amount = 0;<br />
foreach ($this-getInvoices() as $invoice) { if ($this-&gt;isInDateRangeBefore($invoice, $startDate, $endDate) &amp;&amp; $this-&gt;isForCustomerBefore($invoice, $customerName, $customerId, $customerEmail)) { $amount += $invoice[\'amount\']; } } return $amount; } private function isInDateRangeBefore( array $invoice, \DateTimeImmutable $startDate, \DateTimeImmutable $endDate ): bool { return $invoice[\'date\'] &gt;= $startDate &amp;&amp; $invoice[\'date\'] &lt;= $endDate; } private function isForCustomerBefore( array $invoice, string $customerName, string $customerId, string $customerEmail ): bool { return $invoice[\'customerId\'] === $customerId; } public function demonstratePattern(): void { $startDate = new \DateTimeImmutable(\'2024-01-01\'); $endDate = new \DateTimeImmutable(\'2024-03-31\'); $customerName = \'John Doe\'; $customerId = \'C001\'; $customerEmail = \'john@example.com\'; echo BEFORE (5 separate parameters):\n; $amountBefore = $this-&gt;amountInvoicedBefore( $startDate, $endDate, $customerName, $customerId, $customerEmail ); echo Amount invoiced: $ . number_format($amountBefore, 2) . \n; echo Issues: Too many parameters, unclear relationships\n\n; } } <code><code> **AFTER: Using parameter objects for related data** Benefits: - Reduced the chance of parameter ordering errors - Clear, self-documenting function signatures - Related data is explicitly grouped - Easy to add new related fields without changing signatures - Can add behaviour and validation to the parameter objects </code></code>php declare(strict_types=1);</p>
<p>namespace RefactoringPatterns;</p>
<p>class IntroduceParameterObject<br />
{<br />
public function amountInvoicedAfter(DateRange $dateRange, Customer $customer): float<br />
{<br />
$amount = 0;<br />
foreach ($this-getInvoices() as $invoice) { if ($this-&gt;isInDateRangeAfter($invoice, $dateRange) &amp;&amp; $this-&gt;isForCustomerAfter($invoice, $customer)) { $amount += $invoice[\'amount\']; } } return $amount; } private function isInDateRangeAfter(array $invoice, DateRange $dateRange): bool { return $dateRange-&gt;contains($invoice[\'date\']); } private function isForCustomerAfter(array $invoice, Customer $customer): bool { return $invoice[\'customerId\'] === $customer-&gt;id; } public function demonstratePattern(): void { echo AFTER (2 parameter objects):\n; $dateRange = new DateRange($startDate, $endDate); $customer = new Customer($customerId, $customerName, $customerEmail); $amountAfter = $this-&gt;amountInvoicedAfter($dateRange, $customer); echo Amount invoiced: $ . number_format($amountAfter, 2) . \n; echo Benefits: Clear grouping, can add validation and behavior\n\n; } } /<strong> <em> Parameter Object: DateRange </em> Encapsulates a range of dates with validation and behavior */ class DateRange { public function <strong>construct( private readonly \DateTimeImmutable $startDate, private readonly \DateTimeImmutable $endDate ) { if ($endDate &lt; $startDate) { throw new \InvalidArgumentException(\'End date must be after start date\'); } } public function getStartDate(): \DateTimeImmutable { return $this-&gt;startDate; } public function getEndDate(): \DateTimeImmutable { return $this-&gt;endDate; } /<strong> <em> Behavior: Check if a date is within the range </em>/ public function contains(\DateTimeImmutable $date): bool { return $date &gt;= $this-&gt;startDate &amp;&amp; $date &lt;= $this-&gt;endDate; } /</strong> <em> Behavior: Get the duration of the range in days </em>/ public function getDurationInDays(): int { return $this-&gt;startDate-&gt;diff($this-&gt;endDate)-&gt;days; } } /*<em> </em> Parameter Object: Customer <em> Encapsulates customer-related data with validation </em>/ class Customer { public readonly string $id; public readonly string $name; public readonly string $email; public function </strong>construct(string $id, string $name, string $email) { if (empty($id)) { throw new \InvalidArgumentException(\'Customer ID cannot be empty\'); } if (empty($name)) { throw new \InvalidArgumentException(\'Customer name cannot be empty\'); } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException(\'Invalid email address\'); } $this-&gt;id = $id; $this-&gt;name = $name; $this-&gt;email = $email; } /</strong> <em> Behavior: Get customer display name </em>/ public function getDisplayName(): string { return {$this-&gt;name} ({$this-&gt;email}); } } <code><code> Another example - Coordinates passed as primitives Before </code></code>php declare(strict_types=1);</p>
<p>namespace RefactoringPatterns;</p>
<p>class IntroduceParameterObject<br />
{<br />
public function calculateDistanceBefore(<br />
float $x1,<br />
float $y1,<br />
float $x2,<br />
float $y2<br />
): float {<br />
$dx = $x2 - $x1;<br />
$dy = $y2 - $y1;<br />
return sqrt($dx <em> $dx + $dy </em> $dy);<br />
}</p>
<pre><code>public function findNearbyLocationsBefore(
    float $centerX,
    float $centerY,
    float $radius,
    array $locations
): array {
    $nearby = [];
    foreach ($locations as $location) {
        $distance = $this-calculateDistanceBefore( $centerX, $centerY, $location[\'x\'], $location[\'y\'] ); if ($distance <= $radius) { $nearby[] = $location; } } return $nearby; } public function demonstratePattern(): void { echo Example 2: Location Distance Calculation\\n; echo -----------------------------------------\\n; $locations = [ [\'name\' => \'Store A\', \'x\' => 10.0, \'y\' => 20.0], [\'name\' => \'Store B\', \'x\' => 15.0, \'y\' => 25.0], [\'name\' => \'Store C\', \'x\' => 50.0, \'y\' => 50.0], ]; echo BEFORE (4 coordinate parameters):\\n; $nearbyBefore = $this->findNearbyLocationsBefore(10.0, 20.0, 10.0, $locations); echo Found  . count($nearbyBefore) .  nearby locations\\n; echo Issues: Easy to mix up x1, y1, x2, y2 parameters\\n\\n; } } ``<code> After </code>``php declare(strict_types=1);</code></pre>
<p>namespace RefactoringPatterns;</p>
<p>class IntroduceParameterObject<br />
{<br />
public function calculateDistanceAfter(Point $point1, Point $point2): float<br />
{<br />
return $point1-distanceTo($point2); } public function findNearbyLocationsAfter( Point $center, float $radius, array $locations ): array { $nearby = []; foreach ($locations as $locationData) { $location = new Point($locationData[\'x\'], $locationData[\'y\']); if ($center-&gt;distanceTo($location) &lt;= $radius) { $nearby[] = $locationData; } } return $nearby; } public function demonstratePattern(): void { // Example 2: Coordinate calculations echo Example 2: Location Distance Calculation\n; echo -----------------------------------------\n; $locations = [ [\'name\' =&gt; \'Store A\', \'x\' =&gt; 10.0, \'y\' =&gt; 20.0], [\'name\' =&gt; \'Store B\', \'x\' =&gt; 15.0, \'y\' =&gt; 25.0], [\'name\' =&gt; \'Store C\', \'x\' =&gt; 50.0, \'y\' =&gt; 50.0], ]; echo AFTER (Point parameter object):\n; $center = new Point(10.0, 20.0); $nearbyAfter = $this-&gt;findNearbyLocationsAfter($center, 10.0, $locations); echo Found  . count($nearbyAfter) .  nearby locations\n; echo Benefits: Point object can now have behavior (distanceTo method)\n\n; echo === Key Benefits ===\n; echo 1. Function signatures are clearer and more maintainable\n; echo 2. Related data is explicitly grouped together\n; echo 3. Behavior can be added to parameter objects\n; echo 4. Easier to extend without breaking existing code\n; echo 5. Reduced chance of parameter ordering mistakes\n; echo 6. Parameter objects can enforce validation rules\n; } } /*<em> </em> Parameter Object: Point <em> Encapsulates 2D coordinates with geometric operations </em>/ class Point { public function __construct( private readonly float $x, private readonly float $y ) { } public function getX(): float { return $this-&gt;x; } public function getY(): float { return $this-&gt;y; } /<strong> <em> Behavior: Calculate distance to another point </em>/ public function distanceTo(Point $other): float { $dx = $other-&gt;x - $this-&gt;x; $dy = $other-&gt;y - $this-&gt;y; return sqrt($dx <em> $dx + $dy </em> $dy); } /</strong> <em> Behavior: Create a new point offset from this one </em>/ public function offset(float $dx, float $dy): Point { return new Point($this-&gt;x + $dx, $this-&gt;y + $dy); } } ```</p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/introduce-parameter-object-refactoring-patterns.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2357</post-id>	</item>
	</channel>
</rss>
