
I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Search for a command to run...

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
No comments yet. Be the first to comment.
Different stages need different brains.

One Second Brain doesn't scale past one skull.

Style errors double when nobody enforces them.

Know who speaks before the skill runs TL;DR: Always define a clear role at the top of every skill file so you know whose perspective drives the execution. Common Mistake ❌ You write a skill full of

Everyone is talking about Loop Engineering. Apparently, you don't need to program anymore. TL;DR: Loop Engineering is the hottest AI workflow pattern of 2026. But it hides a dirty secret. The Tweet

TL;DR: How to remove repeated code
Don't Repeat Yourself
Copy/Paste Programming
No single source of truth
Duplicated code is a severe code smell, it leads to maintainability problems and ripple effects.
Start by identifying behavior duplication.
Once you find it, you will extract it into reusable functions or classes, reducing redundancy, creating a single source of truth, and simplifying future updates.
Behavior duplication is a sign of a missing abstraction you need to create.
As always, you should search for it in the real world.
Refactoring isn't a one-time event; it's an ongoing process that should be integrated into your development workflow.
Make a contextual copy of the repeated code
Parametrize what is different
Invoke the abstraction
Find a real-world metaphor for the abstraction
(This is the harder and not mechanical step)
(This is actual code generated by Google Gemini)
See a complete explanation in this talk
<?php
class AccessControlPanel {
private $users = [];
public function createRegularUser($username, $password, $email) {
$user = [
"username" => $username,
"email" => $email,
"type" => $this->regularUserRole(),
"creationDate" => $this->timeSource->currentTimestamp(),
"needsToChangePassword" = $this->needsToChangePassword(),
"loginPolicy" => $this->userLoginPolicy()
]
$this->users[] = $user;
$this->addCreationToJournal($user);
}
public function createAdminUser($username, $password, $email) {
$user = [
"username" => $username,
"email" => $email,
"type" => $this->regularUserRole(),
"creationDate" => $this->timeSource->currentTimestamp(),
"needsToChangePassword" = $this->needsToChangePassword(),
"loginPolicy" => $this->adminUserLoginPolicy()
]
$this->users[] = $user;
$this->addCreationToJournal($user);
return $user;
}
}
?>
<?php
class AccessControlPanel {
private $users = [];
// 1. Make a contextual copy of the repeated code
private function createUser(
$username,
$password,
$email,
$role,
$loginPolicy) {
$user = [
"username" => $username,
"email" => $email,
"type" => $role,
"creationDate" => $this->timeSource->currentTimestamp(),
"needsToChangePassword" => $this->needsToChangePassword(),
"loginPolicy" => $loginPolicy
];
$this->users[] = $user;
$this->addCreationToJournal($user);
return $user;
}
// 2. Parametrize what is different (in this case $role and $loginPolicy)
public function createRegularUser($username, $password, $email) {
// 3. Invoke the abstraction
return $this->createUser(
$username,
$password,
$email,
$this->regularUserRole(),
$this->userLoginPolicy());
}
public function createAdminUser($username, $password, $email) {
return $this->createUser(
$username,
$password,
$email,
$this->adminUserRole(),
$this->adminUserLoginPolicy());
}
// 4. Find a real-world metaphor for the abstraction
// private function createUser(
// $username,
// $password,
// $email,
// $role,
// $loginPolicy)
}
?>
[X] Semi-Automatic
The steps are defined but sometimes not about text duplication, but behavior duplication.
Since this is not a mechanical refactoring, you need good coverage on the code you modify.
You have a single source of truth, more compact code, and an easier-to-maintain solution.
Imagen de Rachealmarie en Pixabay
This article is part of the Refactoring Series.