Software Craftsmanship<br />Meeting #2<br />http://www.flickr.com/photos/renfield/17375998/in/set-388814/<br />
Agenda<br />Bad Code / Good Code<br />Code Smells<br />Tools<br />Hands On<br />Code Dojo<br />Identifying Smells *<br />
WTF<br />    publicstaticbooleanisValidNumber(Integer number) {<br />methodName = "isValidNumber";<br />        Logger log...
WTF<br />while(true)<br />{<br />if(mainType == 7)<br />    {<br />subType = 4;<br />break;<br />    }<br />if(mainType ==...
WTF<br />DirectoryInfo dir = new DirectoryInfo(pathToDelete);<br />// subDirs<br />DirectoryInfo[] subDirs = dir.GetDirect...
Code Smell<br />http://www.flickr.com/photos/brian-fitzgerald/3334353375/<br />http://www.flickr.com/photos/yomi955/119282...
Code Smells<br />Duplicated Code<br />Same code structure in more than one place:<br />Same expression in two methods of t...
Code Smells<br />
Demo<br />Clone Detector<br />
Code Smells<br />Long Method<br />A good technique is to look for comments. They often signal a semantic distance between ...
Code Smells<br />
Demo<br />Comments, please!<br />
Code Smells<br />Large Class<br />Many instance variables indicates that a class tries to do too much<br />Many instance v...
Code Smells<br />
Demo<br />How Many Lines?<br />
Code Smells<br />Long Parameter List<br />In the past we passed in as parameters everything needed by a routine. With obje...
Code Smells<br />
Code Smells<br />Long Parameter List<br />Avoid output parameters (ref, out)<br />Avoid boolean arguments<br />Avoid more ...
Demo<br />To Query or To Modify?<br />
Code Smells<br />Switch Statements<br />The problem with switch statements is essentially that of duplication i.e. the sam...
Code Smells<br />
Demo<br />How to Process Request<br />
Code Smells<br />Speculative Generality<br />A class that exists to support some future and imaginary feature.<br />“We mi...
Code Smells<br />Comments<br />When you feel the need to write a comment, first try to refactor the code so that any comme...
Code Smells<br />Feature Envoy<br />A method that seems more interested in a class other than the one it actually is in<br...
Code Smells<br />
Code Smells<br />Message Chains<br />A client asks one object for another object, which the client then asks for yet anoth...
Hands On<br />Dojo<br />Code Smells<br />http://www.flickr.com/photos/renfield/351556388/in/photostream/<br />
Roman Numbers: ToString()<br />0<br />Nulla<br />Numerals<br />
Roman Numbers: ToString()<br />I<br />The values of each roman letter can be additive<br />II<br />The larger numerals mus...
2nd Meeting of the Software Craftsmanship Group<br />http://www.flickr.com/photos/psd/2086641/<br />
Upcoming SlideShare
Loading in …5
×

Software Craftsmanship - 2

7,220 views
7,084 views

Published on

2nd Meeting of the Software Craftsmanship in Israel Group

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
7,220
On SlideShare
0
From Embeds
0
Number of Embeds
3,852
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • CMMI 5Complicated? Smart Ass?
  • WHY?
  • Duplicated Logic on a “Page” or “Code Behind”
  • Examples:Execute similarities (CloneDetector) on the BlogEngine:DbRoleProvider, DbBlogProvider, DbMembershipProvider – Initialize method – 99% is duplicated. Also, Category and Post are duplicated on Title and Description, although they inherit from BusinessBase.Tools:http://www.getatomiq.com/overview/http://www.redhillconsulting.com.au/products/simian/http://clonedigger.sourceforge.net/
  • Decomposition (valid reasons to create a method):Reduce ComplexityIndirectionRemove Duplicate CodeSupport SubclassingHide SequencesSimplify Complicated Boolean Tests
  • Examples:BlogEngine.Core.Providers.XmlProvider. XMLBlogProvider.Posts.SelectPost – a huge methods with a lot of responsibilities.CardGame.BlackJackGame.DealNewGame – proves the newspaper paradigm
  • Examples:BlogEngine.Core.Providers.BlogProvider – violates SR Principle and also defines a lot of different methods, has more than 200 lines.BlogEngine.Core.SyndicationGenerator – has more than 900 lines.BlogEngine.Core.API.MetaWeblog.MetaWeblogHandler – has more than 700 lines, most of the functions are private.
  • Examples:- Method receiving booleanarg: CardGame.Card. The constructor Card takes a boolean argument, specifying whether the card is up or not. This is hard to read!!!- BlogEngine.Core.API.MetaWeblog.MetaWeBlogHandler.LookupCategoryGuidByName: Does 2 things: Returns whether found and returns the Category
  • Refactor the MovieRent’s Rent and Movie classes to use State Pattern
  • Examples:- BlogEngine.Core.API.MetaWebBlogHandler.ProcessRequest : A huge switch case.
  • Software Craftsmanship - 2

    1. 1. Software Craftsmanship<br />Meeting #2<br />http://www.flickr.com/photos/renfield/17375998/in/set-388814/<br />
    2. 2. Agenda<br />Bad Code / Good Code<br />Code Smells<br />Tools<br />Hands On<br />Code Dojo<br />Identifying Smells *<br />
    3. 3. WTF<br /> publicstaticbooleanisValidNumber(Integer number) {<br />methodName = "isValidNumber";<br /> Logger logger = LoggingHelper.getLogger(LOGGER);<br />logger.entering (CLASS, methodName);<br /> // parse this number. If you get an NFE, then its <br /> // not valid, return false<br />try {<br />Integer.parseInt(number.toString());<br /> } catch(Exception ex) {<br />logger.fine(methodName + " returns false");<br />logger.fine("Number Format Exception when parsing");<br /> return false; <br /> }<br />logger.fine(methodName + " returns true");<br />logger.exiting(CLASS, methodName); <br /> return true; <br /> }<br />Logging Entry<br />WTF?<br />Logging Exit<br />
    4. 4. WTF<br />while(true)<br />{<br />if(mainType == 7)<br /> {<br />subType = 4;<br />break;<br /> }<br />if(mainType == 9)<br /> {<br />subType = 6;<br />break;<br /> }<br />if(mainType == 11)<br /> {<br />subType = 9;<br />break;<br /> }<br />break;<br />}<br />/*<br />=============<br />checkSize<br />Make sure the image is a power of 2.<br />=============<br />*/<br />intcheckSize(int x) {<br />if (x == 2 || x == 4 || x == 8 || x == 16 || x == 32 || x == 64 || x == 128 || x == 256 || x == 512)<br />return 1;<br />else<br />return 0;<br />}<br />WTF?<br />WTF?<br />
    5. 5. WTF<br />DirectoryInfo dir = new DirectoryInfo(pathToDelete);<br />// subDirs<br />DirectoryInfo[] subDirs = dir.GetDirectories();<br />foreach(DirectoryInfosubDirinsubDirs)<br />{<br /> // subSubDirs<br />DirectoryInfo[] subSubDirs = subDir.GetDirectories();<br />foreach(DirectoryInfosubSubDir in subSubDirs)<br /> {<br /> // subSubSubDirs<br />DirectoryInfo[] subSubSubDirs = subSubDir.GetDirectories();<br />foreach(DirectoryInfosubSubSubDirinsubSubSubDirs)<br /> {<br /> // subSubSubSubDirs<br />DirectoryInfo[] subSubSubSubDirs = subSubSubDir.GetDirectories();<br />foreach(DirectoryInfosubSubSubSubDirinsubSubSubSubDirs)<br /> {<br /> // ********************************************<br /> // should be enough; if not, I can add more here<br /> // ********************************************<br />WTF?<br />WTF?<br />
    6. 6. Code Smell<br />http://www.flickr.com/photos/brian-fitzgerald/3334353375/<br />http://www.flickr.com/photos/yomi955/1192827822/<br />Code Smell - In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem<br />http://en.wikipedia.org/wiki/Code_smell<br />
    7. 7. Code Smells<br />Duplicated Code<br />Same code structure in more than one place:<br />Same expression in two methods of the same class<br />Same expression in two sibling subclasses<br />Same expression in two unrelated classes<br />
    8. 8. Code Smells<br />
    9. 9. Demo<br />Clone Detector<br />
    10. 10. Code Smells<br />Long Method<br />A good technique is to look for comments. They often signal a semantic distance between the intension of the code and the implementation of the code<br />The net effect is that you should be much more aggressive about decomposing methods<br />The desired length should be 5-8 lines<br />
    11. 11. Code Smells<br />
    12. 12. Demo<br />Comments, please!<br />
    13. 13. Code Smells<br />Large Class<br />Many instance variables indicates that a class tries to do too much<br />Many instance variables can indicate a duplicate code<br />Too much code is prime breeding ground for duplicated code, chaos, and death <br />
    14. 14. Code Smells<br />
    15. 15. Demo<br />How Many Lines?<br />
    16. 16. Code Smells<br />Long Parameter List<br />In the past we passed in as parameters everything needed by a routine. With objects it’s not required; instead enough parameters passed in to the method in order to allow it to get everything rest it needs<br />Long parameter lists are hard to understand<br />Long parameter lists inconsistent and difficult to use<br />Long parameter lists are forever changed as more data is required <br />
    17. 17. Code Smells<br />
    18. 18. Code Smells<br />Long Parameter List<br />Avoid output parameters (ref, out)<br />Avoid boolean arguments<br />Avoid more than 3 arguments<br />2 is preferable<br />1 is better<br />0 is the best<br />Introduce Parameter Object<br />Refactoring Step<br />Separate Query From Modifier<br />Refactoring Step<br />One return value.<br />
    19. 19. Demo<br />To Query or To Modify?<br />
    20. 20. Code Smells<br />Switch Statements<br />The problem with switch statements is essentially that of duplication i.e. the same switch statement scattered about a program in different places<br />Most times you see a switch statement you should consider polymorphism <br />
    21. 21. Code Smells<br />
    22. 22. Demo<br />How to Process Request<br />
    23. 23. Code Smells<br />Speculative Generality<br />A class that exists to support some future and imaginary feature.<br />“We might need this one day …”<br />If you really do need it later, you can add it easily using Refactoring.<br />It doesn’t mean you don’t need to anticipate requirements and features.<br />
    24. 24. Code Smells<br />Comments<br />When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous<br />
    25. 25. Code Smells<br />Feature Envoy<br />A method that seems more interested in a class other than the one it actually is in<br />Data and the behavior that references that data usually change together, but there are exceptions:<br />Visitor<br />Strategy<br />
    26. 26. Code Smells<br />
    27. 27. Code Smells<br />Message Chains<br />A client asks one object for another object, which the client then asks for yet another object, which the client then asks for yet another another object, and so on<br />Can be seen as a long line of GetThis() methods, or as a sequence of temps<br />Any change to the intermediate relationships causes the client to have to change<br />Account.Owner.OwnerDetails.Address.Zip<br />
    28. 28. Hands On<br />Dojo<br />Code Smells<br />http://www.flickr.com/photos/renfield/351556388/in/photostream/<br />
    29. 29. Roman Numbers: ToString()<br />0<br />Nulla<br />Numerals<br />
    30. 30. Roman Numbers: ToString()<br />I<br />The values of each roman letter can be additive<br />II<br />The larger numerals must be placed to the left of the <br />smaller numerals<br />III<br />At the fourth iteration, a subtractive principle may be<br />employed, with the base placed before the higher base:<br />IIII or IV <br />The subtracted digit must be at least one tenth of the<br />value of the larger numeral. Therefore: 99 isn’t IC but<br />rather XCIX<br />
    31. 31. 2nd Meeting of the Software Craftsmanship Group<br />http://www.flickr.com/photos/psd/2086641/<br />

    ×