class: center, middle, red # .book.light[On the] Art .book.light[of] Refactoring --- class: dark #Why? * Code grows over time * You never get it right the first time * Faster than you'd think * It's a joy --- class: dark #.light[Beautiful] is better than .light[Ugly] ```javascript (function () { var CONSTANT = 2; var x = "stuff" ? (CONSTANT%2 ? "Bob" : "Cow") : CONSTANT return (function () { for (var i = 0; i != x * CONSTANT ? "stuff" : "stuff2"; i++) { if (x == "Cow") { for (var j = 0; j < i; j++) { if (x + CONSTANT) { console.log("stuff"); }else { alert("hello"); }if (i == 10) { return; } } }} })() })() ``` --- class: dark #.light[Explicit] is Better than .light[Implicit] ```cs RunForYourLife(true, true, false, true, false); RunForYourLife(regulateBreathing: true, zigZag: true, pantsPulledUp: false, watchOutForZombies: true, keepFingerOnTrigger: false); ``` --- class: dark #.light[Simple] is Better than .light[Complex] ```python def process_data(file): data = readfile(file) data = strip_unnecessary_things(data) data = extract_important_parts(data) return data def process_data(file): f = open(file) data = f.read().split("\n") whitespace = re.compile(r"\s") temp = [] for line in data: temp.append(whitespace.sub(line, "")) mid = re.compile(r"\d{15}") temp2 = [] for line in temp: temp2.append(mid.find(line)) return data ``` --- class: dark #.light[Complex] is better than .light[Complicated] ```cs interface IDataForStrippingAndExtracting {} public abstract class AbstractFactoryBaseClassFileProcessor {} public class FileProcessorFactory : AbstractFactoryBaseClassFileProcessor {} public abstract class AbstractFactorStriper {} public class FactoryStriper : AbstractFactorStriper {} public abstract class AbstractFactorExtractor {} public class FactoryExtractor : AbstractFactorExtractor {} ... FileProcessorFactory fileProcessor = FileProcessorFactory.newProcessor(); fileProcessor.Process(file); FactoryStriper strip = FactoryStriper.newStriper(); strip.StripIData((IDataForStrippingAndExtracting) fileProcessor.fileData); FactoryExtractor extract = FactoryExtractor.newExtractor(); extract.ExtractIData((IDataForStrippingAndExtracting) strip.stripData); ``` --- class: dark #.light[Flat] is Better than .light[Nested] ```javascript if (stuff) { if (thisIsCool) { if (!otherstuff) { for (var i = 0; i < 10; i ++) { doit(); } } } else { if (weWin()) { if (weDontLose()) { followThrough(); } } } } ``` --- class: dark ##Continued... ```javascript if (stuff && thisIscool && !otherstuff) { for (var i = 0; i < 10; i ++) { doit(); } } else if (weWin() && weDontLose()) { followThrough() } ``` --- class: dark #.sparse[Sparse] is Better than .dense[Dense] ```python s=getstuff(True,2,"now") s = get_stuff(True, 2, "now") ``` ```cs private void populateRMInfo(popOption option) { if (option == popOption.getInfo) { //Get the latest data on the Employee //Populates the LoginID field } else if (option == popOption.popInfo) { //Populates some Data } } ``` ??? I've read that if you confine too many rats in too small a living space, they become sociopathic. That's density for you. I've seen no reason to suspect that code, concepts, data structures, interfaces, programmers or managers have an advantage over rats in this respect. Give yourself some room to breathe and stretch: couple interfaces loosely, let a subsystem deal with a little rather than a lot, leave a little whitespace for flowers to grow between tokens, fail soft on feathers instead of hard on rocks, don't do two things at a time until you're pretty sure you can do one, a function with 14 arguments-- or a module with 14 classes --is a bad idea, don't fall for the idea that between any two abstractions you always need to compromise on a third. --- class: dark #.light[Readability] Counts .unreadable[ If others can't read it, they won't know what it means. ] --- class: dark #.light[Special cases] aren't special enough to .light[break the rules] --- class: dark #.light[Errors] should never pass .light[silently] ```cs try { doIt() } catch {} try { doIt() } catch(Exception ex) { Log(ex); } ``` --- class: center, middle, red #If the .light[implementation] is .light[hard] to explain, it's a .light[bad] idea. --- class: center, middle, red #If the .light[implementation] is .light[easy] to explain, it .light.italics[may] be a .light[good] idea.