This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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})` | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function findHolidays( | |
| maxPrice: Currency, | |
| startDate: Date, | |
| endDate: Date, | |
| minPrice: Currency) { | |
| // Notice that maxPrice and minPrice are swapped by mistake | |
| // Also, dates are mixed | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Article { | |
| final DateTime publishDate; | |
| final String title; | |
| final String content; | |
| Article({ | |
| required this.publishDate, | |
| required this.title, | |
| required this.content, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Article { | |
| final DateTime date; | |
| final String title; | |
| final String content; | |
| Article({ | |
| required this.date, | |
| required this.title, | |
| required this.content, | |
| }); |
NewerOlder