Skip to content

Instantly share code, notes, and snippets.

View mcsee's full-sized avatar
🏠
Working from home

mcsee mcsee

🏠
Working from home
View GitHub Profile
@mcsee
mcsee / fixing_real_hotspot.py
Last active December 28, 2025 13:16
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
# You analyze git history first:
# git log --format=format: --name-only |
# grep -E '\.py$' | sort | uniq -c | sort -rn
# Results show PaymentProcessor changed 47 times this month
# And it does not have good enough coverage
# LegacyAuthenticator: 0 changes in 3 years
# Focus on the actual hotspot:
class PaymentProcessor:
@mcsee
mcsee / refactoring_stable_legacy.py
Created December 24, 2025 13:06
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
# This authentication module hasn't changed in 3 years
# It's deprecated and will be removed next quarter
# But you spend a week "improving" it
class LegacyAuthenticator:
def authenticate(self, user, pwd):
# Original messy code from 2019
if user != None:
if pwd != None:
if len(pwd) > 5:
@mcsee
mcsee / password_reset_normalized.py
Last active December 17, 2025 00:49
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
import unicodedata
def normalize_email(email):
# Convert to NFKC normalized form
normalized = unicodedata.normalize('NFKC', email)
# Ensure only ASCII characters
try:
normalized.encode('ascii')
except UnicodeEncodeError:
@mcsee
mcsee / password_reset.py
Last active December 15, 2025 16:04
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
def reset_password(email_from_ui):
# email_from_ui = "victim@gmàil.com"
# (attacker's Unicode address from UI)
# Database with utf8mb4_unicode_ci collation
# treats 'à' = 'a', so this query finds:
# victim@gmail.com stored in the database
cursor.execute(
"SELECT * FROM users WHERE email = %s",
(email_from_ui,)
@mcsee
mcsee / SecureRepository.php
Created December 13, 2025 22:48
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
<?php
final class UserRepository {
private Database $database;
public function __construct(Database $database) {
$this->database = $database;
}
public function find(UserId $id): User {
@mcsee
mcsee / UserRepository.php
Created December 13, 2025 22:47
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
<?php
class UserRepository {
public function find($id){
$conn = mysqli_connect(
"localhost", // Pull Request comment - Bad indentation
"root",
"password123",
"app"
);
@mcsee
mcsee / McpMessageParserMetaprogrammingTest.php
Last active December 14, 2025 15:58
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
<?php
use PHPUnit\Framework\TestCase;
final class McpMessageParserTest extends TestCase {
private function invokePrivateMethod(
$object,
$methodName,
array $parameters = []
) {
@mcsee
mcsee / MethodObject.php
Last active December 14, 2025 15:58
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
<?php
final class McpMessageParser {
private $raw;
public function parse() {
// Step 5: Replace the private method call
// with the new object
$stripper = new CharacterStripper($this->raw);
return $stripper->strip();
@mcsee
mcsee / McpParser.php
Created December 8, 2025 14:22
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
<?php
final class McpMessageParser {
private $raw;
public function parse() {
return $this->stripStrangeCharacters($this->raw);
}
// This is the private method me need to test
@mcsee
mcsee / safe.rs
Last active November 29, 2025 21:10
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
fn load_and_validate(max: usize) -> Result<Vec<Feature>, String> {
let raw: Vec<Result<Feature, Error>> = load_features_from_db();
if raw.len() > max {
return Err(format!(
"too many features: {} > {}",
raw.len(), max
));
}