Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Thought Driven Development

Alexey Buzdin
September 26, 2015

Thought Driven Development

Some thought on the way we Develop and Test our products

Alexey Buzdin

September 26, 2015
Tweet

More Decks by Alexey Buzdin

Other Decks in Programming

Transcript

  1. DOES THIS CODE WORK? def trySend (Mail mail) { try

    { mailService.send mail } catch (ServerTimeOutException e) { /* TODO: Handle error here */ } }
  2. ZE CODE AGAIN def trySend (Mail mail) { try {

    mailService.send mail } catch (ServerTimeOutException e) { /* TODO: Handle error here */ } }
  3. ZE CODE AGAIN def trySend (Mail mail) { try {

    mailService.send mail } catch (ServerTimeOutException e) { /* TODO: Handle error here */ } } HOW MANY PATHS?
  4. ZE CODE AGAIN def trySend (Mail mail) { try {

    mailService.send mail } catch (ServerTimeOutException e) { /* TODO: Handle error here */ } }
  5. public byte[] scaleImage(InputStream is, int maxWidth, int maxHeight, ImageType type)

    throws IOException { if (is == null) throw new IOException(String.format("Input stream is unavailable for image type %s", type.getMime())); BufferedImage source = ImageIO.read(is); int width = source.getWidth(); int height = source.getHeight(); int scaledWidth = width; int scaledHeight = height; if (width > maxWidth) { scaledWidth = maxWidth; scaledHeight = (int) Math.ceil((scaledWidth * height * 1.0) / width); } if (scaledHeight > maxHeight) { scaledHeight = maxHeight; scaledWidth = (int) Math.ceil((scaledHeight * width * 1.0) / height); } source = (width < maxWidth || height < maxHeight) ? subImage(source, width, height, maxWidth, maxHeight) : scale(source, width, height, scaledWidth, scaledHeight); ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(source, type.getMime(), stream); return stream.toByteArray(); }
  6. CLASSIC TDD 1. ADD A TEST 2. RUN ALL TESTS

    3. WRITE SOME CODE 4. RUN TESTS 5. REFACTOR CODE 6. REPEAT
  7. Kent Beck stated that TDD: - encourages simple designs -

    inspires confidence Test-Driven Development
  8. Kent Beck stated that TDD: - encourages simple designs -

    inspires confidence TDD acts as a self-documenting code Test-Driven Development
  9. Kent Beck stated that TDD: - encourages simple designs -

    inspires confidence TDD acts as a self-documenting code Test-Driven Development but on which level?
  10. Kent Beck stated that TDD: - encourages simple designs -

    inspires confidence TDD acts as a self-documenting code Test-Driven Development but on which level? LETS THINK!
  11. def join(List<String> tokens, String separator) {
 def sb = new

    StringBuilder()
 boolean first = true
 for (String item : tokens)
 {
 if (first) first = false
 else sb.append separator
 sb.append item
 }
 return sb.toString
 } HOW MANY TEST WOULD YOU WRITE?
  12. def join(List<String> tokens, String separator) {
 def sb = new

    StringBuilder()
 boolean first = true
 for (String item : tokens)
 {
 if (first) first = false
 else sb.append separator
 sb.append item
 }
 return sb.toString
 } HOW MANY TEST WOULD MAKE YOU CONFIDENT?
  13. @Test def joinsStrings() { def actual = util.join(Arrays.asList("hello", "world"), ";")

    assertThat(actual, equalTo "hello;world") } IS ONE ENOUGH?
  14. Scenario: New Customer is retrieved by ID Given request body

    from file json/newCustomer.json When the client performs POST request on /customers Then status code is 201 And header Location end with customers/(.+) And assign variable "{(location)}" to header "Location" value When the client performs GET request on {(location)} Then status code is 200 And response body contains properties from file json/newCustomer.json Acceptance Test-Driven Development (ATDD)
  15. TAKE AWAYS LET TESTS GUIDE YOUR HAND TEST FOR EACH

    FAILURE ACCOUTERED TEST MEANINGFUL CODE
  16. TAKE AWAYS LET TESTS GUIDE YOUR HAND TEST FOR EACH

    FAILURE ACCOUTERED TEST MEANINGFUL CODE TEST-DOCUMENT PUBLIC API