Skip to content

Instantly share code, notes, and snippets.

View isaacssemugenyi's full-sized avatar

Isaac Ssemugenyi isaacssemugenyi

View GitHub Profile
"""
**************************************
ADDRESS BOOK (REFACTORED VERSION)
Behavior preserved, design improved
**************************************
"""
from datetime import datetime
// Existing global state (KEEP)
let contacts = [];
let lastId = 0;
/**
* NEW: AddressBook class
* We introduce it but DO NOT break existing code
*/
class AddressBook {
constructor(store, clock = () => new Date().toISOString()) {
/***************************************
* Characterization Tests for AddressBook
***************************************/
describe('Legacy Address Book – Characterization Tests', () => {
beforeEach(() => {
// Reset module state before every test
jest.resetModules();
});
√ adds a contact successfully with valid data
√ does not add contact when name is missing
√ does not add contact with duplicate phone number
√ finds a contact by phone number
√ returns null when searching for non-existent phone
√ updates an existing contact
√ returns false when updating a non-existent contact
√ deletes an existing contact
√ returns false when deleting non-existent contact
√ searches contacts by keyword across name, phone, and email
/**************************************
* ADDRESS BOOK (LEGACY / MESSY VERSION)
**************************************/
let contacts = [];
let lastId = 0;
function addContact(name, phone, email, address) {
if (name == null || name === "") {
console.log("Name is required");
/***********************
* Simple Grading System
***********************/
let students = [];
let totalProcessed = 0;
function doIt(
name,
/************************
* Clean Grading System
* Refactored for clarity,
* structure, and safety
************************/
/* ===== Constants (No Magic Numbers) ===== */
const SUBJECT_COUNT = 4;
const ATTENDANCE_BONUS_THRESHOLD = 90;
const ATTENDANCE_BONUS_POINTS = 5;
/***********************
* Simple Grading System
***********************/
let students = [];
let totalProcessed = 0;
function doIt(
name,
// config with dotenv
impl Config {
pub fn from_env() -> Result<Self, ConfigError> {
let s = config::Config::builder()
.add_source(config::Environment::default())
.build()?;
s.try_deserialize()
}
}
package builder;
import java.util.ArrayList;
import java.util.List;
public class Car {
private String make;
private String name;
private String color;
private int model;