Software Quality via Unit Testing

  • Published on
    15-Jan-2015

  • View
    538

  • Download
    3

Embed Size (px)

DESCRIPTION

Software quality is critical to consistently and continually delivering new features to our users. This talk covers the importance of software quality and how to deliver it via unit testing, Test Driven Development and clean code in general. This is the deck from a talk I gave at Desert Code Camp 2013.

Transcript

  • 1. SOFTWARE QUALITYVIA UNIT TESTINGShaun AbramApril 20, 2013Email: Shaun@Abram.comBlog: shaunabram.comTwitter: @shaunabramLinkedIn: linkedin.com/in/sabram These slides available at shaunabram.com/dcc13

2. 2Software Quality via Unit TestingThe value of software designAutomated testingClean codeGoal:Deliver value to our users 3. 3Software Quality via Unit TestingThe value of software design 4. 4Why should we care about good design in software? We need less focus on quality so we can add more features Do we really need unit tests? Refactoring doesnt change what the code does, so why bother?How do you respond? 5. 5Why should we care about good design in software?Take the moral high ground? 6. 6Why should we care about good design in software?We need to have economic reasons Remember: Our goal is to deliver value to our users 7. 7What is Quality in software anyway?Intuitive GUIFew defectsModular Design 8. 8What is Quality in software anyway?Intuitive GUIFew defectsVisible to userModular Design Transparent to user 9. 9Fowlers Design Stamina Hypothesis 10. 10Technical debtThe eventual consequences of poor design in a codebase 11. 11Technical debtThe eventual consequences of poor design in a codebaseInterest payments can come in the form of:1. Bugs2. Just understanding what the heck the current code does3. Refactoring4. Completing unfinished work 12. 12Technical debtPay down?Accept? But, dont build bad on top of bad 13. Design StaminaDesignstamina to continually and consistently deliver functionalityfaster and with less bugs to our usersBusiness value to our clients 14. 14Clean code that works The value of software design Automated testing 15. 15Unit testingA unit test is a piece of code that executes a specificfunctionality (unit) in the code, and Confirms the behavior or result is as expected. Determines if code is fit for useExample 16. 1 17. 1 18. 1 19. 1 20. 2 21. 2 22. 2 23. 2 24. 25What unit tests provideUnit tests dont necessarily help find bugs.Instead, unit tests: Drive design 25. 26What unit tests provideUnit tests dont necessarily help find bugs.Instead, unit tests: Drive design The tests act as the first user of the code, making you think about: What should this code do Border conditions (0, null, -ve, too big) Force you to use good design: Short, focused methods Dependency Injection Writing a class is different from using a class! 26. 27What unit tests provideUnit tests dont necessarily help find bugs.Instead, unit tests: Drive design Act as safety buffers by finding regression bugs Provide documentation 27. 28What unit tests provideUnit tests dont necessarily help find bugs.Instead, unit tests: Drive design Act as safety buffers by finding regression bugs Provide documentationCan also be used on legacy codebases 28. 29Unit testing limitations1. Can not prove the absence of bugs2. Lots of code (x3-5)3. Some things difficult to test 29. 30So should we unit test?Not only should we unit test,We should let unit tests drive development and design Test Driven Development (TDD) 30. 31Test Driven Development (TDD) 31. 32Test Driven Development (TDD) 32. 33Test Driven Development (TDD) 33. 34Test Driven Development (TDD)Red - Green Refactor: the TDD MantraNo new functionality without a failing testNo refactoring without passing tests 34. 35Test Driven Development (TDD) ExampleWrite a simple StringCalculator class with a method Integer add(String numbers)A String of comma separated numbers should return theirsum e.g. 1,2,10 should return 13.A single number String should return that number e.g. 3should return 3An empty String should return 0For brevity, our test will focus on valid inputs. 35. 36Test Driven Development (TDD) ExampleWrite a simple StringCalculator class with a method Integer add(String numbers)Code demo 36. 3 37. Refactor? 38. Refactor? 39. These tests act like the originaldeveloper looking over yourshoulder and advising you, longafter that developer has left 40. 58Test Driven Development (TDD)Red - Green Refactor: the TDD MantraNo new functionality without a failing testNo refactoring without passing tests 41. 59Clean code that works The value of software design Automated testing 42. 60Clean code that works The value of software design Automated testing Clean code 43. 61 Feedback XKCD 44. 6 45. 63Code SmellsWhat are code smells? Certain structures in code suggest (sometimes they scream for) thepossibility of refactoring.Martin Fowler. Refactoring: Improving the design of existing code 46. 64Code Smells Duplicated code Long switch/if statements Long methodsEven one line methods can be OK:if ( (account != null) && ( (account.getBalance() > 0) || (!account.overdraftLimitReached()) ) { }if (account.hasFundsAvailable()) {} 47. 65Code Smells Duplicated code Long switch/if statements Long methods Poor method names int process(int id) { //bad! int calculateAccountBalance(int accountID) { //better 48. 66Code Smells Duplicated code Long switch/if statements Long methods Poor method names In-line comments Large classes Symptoms Too many methods (>10 public?) Too many instance variables is every instance variable used in every method? Solutions Eliminate redundancy / duplicated code Extract new/sub classes 49. 67Clear Code Make the intent of your code clear Code should be clear, concise and easy to understandHow many times will the code you are about to write be read? Studies show poor readability correlates strongly with defect density1Avoid attrition & complete re-writes1"Learning a Metric for Code Readability," IEEE Transactions on Software Engineering, 09 Nov.2009. IEEE computer Society Digital Library. IEEE Computer Society 50. 68Summary Good design gives us the stamina to continually andconsistently deliver business value Unit tests are an integral part of good design; TDD is evenbetter Good design can also simply be cleaner code;Aggressively refactor to achieve this! Final thought: Every time you are in a piece of code,just make one small improvement! 51. 69Recommended ReadingTest Driven Development Growing Object-OrientedKent BeckSoftware, Guided by TestsFreeman & PryceRefactoring: Improving the Design Effective Unit Testingof Existing CodeLasse KoskelaMartin Fowler, Kent Beck et. al. 52. Questions?All slides available at: shaunabram.com/dcc13Email: Shaun@Abram.comBlog: shaunabram.comTwitter: @shaunabramLinkedIn: linkedin.com/in/sabram 7