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 / race_simulator_test.py
Last active October 27, 2025 02:36
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
def test_set_speed():
car = Formula1Car("Red Bull")
car.set_speed(320)
assert car.speed == 320, (
f"Expected speed to be 320 km/h, "
f"but got {car.speed} km/h"
)
def test_accelerate():
car = Formula1Car("Red Bull")
@mcsee
mcsee / race_simulator_many_asserts.py
Created October 26, 2025 22:21
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
def test_car_performance():
car = Formula1Car("Red Bull")
car.set_speed(320)
assert car.speed == 320
car.accelerate(10)
assert car.speed == 330
car.brake(50)
assert car.speed == 280
car.change_tire("soft")
assert car.tire == "soft"
@mcsee
mcsee / login-secure.js
Created October 18, 2025 13:54
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
import bcrypt from 'bcrypt';
app.post('/login', async (req, res) => {
const { username, password } = req.body;
const user = await Users.findOne({ username });
if (!user) return res.status(401).send('Invalid credentials');
const valid = await bcrypt.compare(password, user.password);
if (!valid) return res.status(401).send('Invalid credentials');
res.send('Login successful');
});
@mcsee
mcsee / login-insecure.js
Created October 18, 2025 13:53
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
// Borrowed from "Beyond Vibe Coding"
app.post('/login', async (req, res) => {
const { username, password } = req.body;
const user = await Users.findOne({ username });
if (!user) return res.status(401).send("No such user");
if (user.password === password) {
res.send("Login successful!");
} else {
res.status(401).send("Incorrect password");
@mcsee
mcsee / BankService.cs
Last active October 12, 2025 15:13
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
// 1. Identify business exceptions
public class BusinessException : Exception {}
public class InsufficientFunds : BusinessException {}
// 2. Identify technical exceptions
public class TechnicalException : Exception {}
public class DatabaseUnavailable : TechnicalException {}
public void Withdraw(int amount) {
// 3. Use the correct hierarchy
@mcsee
mcsee / BankService.cs
Last active October 12, 2025 15:13
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
public void Withdraw(int amount) {
if (amount > Balance) {
throw new Exception("Insufficient funds");
// You might want to show this error to end users
}
if (connection == null) {
throw new Exception("Database not available");
// Internal error, log and notify operators.
// Fail with a more generic error
}
@mcsee
mcsee / reified.ts
Last active October 5, 2025 18:39
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
// 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})`
);
}
@mcsee
mcsee / parameters.ts
Last active October 5, 2025 18:39
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
function findHolidays(
maxPrice: Currency,
startDate: Date,
endDate: Date,
minPrice: Currency) {
// Notice that maxPrice and minPrice are swapped by mistake
// Also, dates are mixed
}
@mcsee
mcsee / published.dart
Created September 28, 2025 14:57
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
class Article {
final DateTime publishDate;
final String title;
final String content;
Article({
required this.publishDate,
required this.title,
required this.content,
});
@mcsee
mcsee / article.dart
Created September 28, 2025 14:56
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
class Article {
final DateTime date;
final String title;
final String content;
Article({
required this.date,
required this.title,
required this.content,
});