refactoring

Recursos de programación de refactoring
Yesterday we had a coding dojo in Clojure Developers Barcelona meetup. We practiced together doing the Mars Rover kata. I had already done this kata in Java and C++ before (several times) using the state pattern to eliminate all the conditionals on the rover direction and the command pattern to decouple the translation from the messages sent to the rover into commands acting on it. As usual I used a mix of TDD and work on the REPL to code this Clojure version. To document the process I committed...
Two weeks ago I facilitated an introduction to TDD in a Scala Developers Barcelona event. First, I gave a very short introductory talk about TDD, (these are the slides). After the talk, I coded a simple exercise to help the attendants get a feeling of the TDD flow. I tried to stress the importance of creating a list of examples first and choosing the next test so that the code could grow in small steps. I also talked about wishful programming, going from red to green quickly and the importance...
I've just done the Password validator kata in Clojure. It's a basic kata that, in my opinion, is very useful to think about how we choose the next test so that it helps us grow the code in small steps. In this kata you have to validate a password. The conditions that it has to fulfill to be valid are: It should have more than 6 charactersIt should contain at least one upper case characterIt should contain at least one lower case characterIt should contain at least one underscoreIt should contai...
I'd like to show how using underscore.js library can simplify a JavaScript code. This is the original code: It wasn't tested, so I wrote some tests before start refactoring it: Once I had the tests in place, I separated the responsibility of converting strings into dates from the traversing of the object: and extracted it to another factory: that I could directly test: Finally I used underscore.js library to simplify all the JavaScript plumbing that was happening in the object traversing inside...
I've just done the LCD Digits kata in Clojure. In this kata you have to create an LCD string representation of an integer value using a 3x3 grid of space, underscore, and pipe characters for each digit. Each digit is shown below (using a dot instead of a space) ._. ... ._. ._. ... ._. ._. ._. ._. ._.|.| ..| ._| ._| |_| |_. |_. ..| |_| |_||_| ..| |_. ._| ..| ._| |_| ..| |_| ..|Example: 910._. ... ._.|_| ..| |.|..| ..| |_|These are the tests...
I've just dome the Happy Numbers kata in Clojure. It's a simple kata that can be a lot of fun. I used a mix of TDD and REPL-driven development to code it. These are the resulting tests in Midje: and this is the code: To document the process I commited the code after every passing test and every refactoring. I also commited the REPL history. You can find the commits step by step hereand the code in this repository in GitHub. I used memoize to avoid repeated computations. In the next days I will...
I've recently revisited the Java version of Conway's Game of Life that I did nearly a year ago, to refactor it using some of the ideas I used in my more recent Clojure version. This is the UML for the refactored design:I eliminated the LivingCells class that appeared in the previous design.I pushed a lot of code from the GameOfLife class into Generation to eliminate a bit of feature envy. This is GameOfLife's code now: and this is Generation's one: I also refactored the tests eliminating all the...
Álvaro and I have been pairing lately to do a subset of the Bank Account kata that Sandro Mancuso proposed as an exercise in his Crafting Code course. We had to write and make the following acceptance test pass using Outside-In TDD: Given a client makes a deposit of 1000.00 on 01/04/2014And a withdrawal of 100.00 on 02/04/2014And a deposit of 500.00 on 10/04/2014When she prints her bank statementThen she would seeDATE | AMOUNT | BALANCE10/04/2014 | 500.00 | 1400.0002/04/2014 | -100.00 | 900.000...
I've refactored the Numbers Spelling kata I recently did in Clojure to remove some duplication. Before refactoring it, I had to make the old spell-out-number-over-99 and spell-out-number-up-to-99 functions similar enough for the duplication pattern to emerge. Then I could use the same function for both cases and also made some other minor changes such as using when-not form and renaming several bindings and arguments. This is the refactored code: You can find the code in this repository in GitH...
These are the links mentioned in our last conversation about the 15th chapter: Talks: La economía del refactoring. Una visión desde la gestión económica del proyecto. by Xavi GostMIT 6.001 SICP, 1986 Lecture 3A by Harold Abelson Posts: Object Functional Patterns at C2 Wiki - por Garajeando