• Save
Software Craftsmanship - 2
Upcoming SlideShare
Loading in...5
×
 

Software Craftsmanship - 2

on

  • 5,835 views

2nd Meeting of the Software Craftsmanship in Israel Group

2nd Meeting of the Software Craftsmanship in Israel Group

Statistics

Views

Total Views
5,835
Views on SlideShare
2,065
Embed Views
3,770

Actions

Likes
0
Downloads
0
Comments
0

7 Embeds 3,770

http://blogs.microsoft.co.il 3639
http://feeds.feedburner.com 90
http://www.irefactor.net 27
http://translate.googleusercontent.com 7
http://beta.blogs.microsoft.co.il 4
http://webcache.googleusercontent.com 2
https://www.goodsmiles.info 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 Software Craftsmanship - 2 Presentation Transcript

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