runtime
398 / 500
question 398 Software Architecture · medium

Refactor the duplicated SKU validation

add_product and rename_sku each contain a copy of the SKU rules, and the copies have started to drift in their messages. Extract one helper used by both, keeping every result and every error message exactly as the current code produces them (the checks compare with the current behaviour). The rules' text must appear only once.

Functionsadd_product(catalogue, sku, name) and rename_sku(catalogue, old, new) keep their names, parameters and behaviour
Rulesa SKU is stripped and upper-cased; it must look like ABC-12345 (3 letters, a dash, 5 digits), else ValueError("bad SKU: <original text>")
Structurethe pattern of the rule appears once in the program
Examples
calladd_product({}, ' spo-11681 ', 'Ultra Bike Light')returns'SPO-11681'
calladd_product({}, 'HOM-22847', 'Deluxe Vase')returns'HOM-22847'
calladd_product({}, 'ELE-24781', 'Organic Speaker')returns'ELE-24781'

Loading your progress…

Refactor the duplicated SKU validation — Python practice · Runtime