Uploaded image for project: 'Java'
  1. Java
  2. SONARJAVA-106

Rule S2301: Rule to detect selector/flag arguments in functions/methods

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.1
    • Fix Version/s: 3.0
    • Component/s: Rules
    • Labels:

      Description

      Hi folks,

      While rereading the book 'clean code' of Robert C. Martin I just wonder
      if sonar has a rule to detect 'selector arguments'.

      For anybody who need an example:

      public void printOut(String myName,boolean uppercase){
         if (uppercase){
      	System.out.print( myName.toUpperCase() );
         }
         else{
      	System.out.print( myName );
         }
      }
      

      could be written as two separate methods to decrease complexity:

      public void printOutAsItIs(String myName){
             System.out.print( myName );
      }
      public void printOutUppercase(String myName){
             System.out.print( myName.toUpperCase() );
      }

      For this request I prepared a possible implementation:

      A screenshot:

      My patch is here (on top of java-plugins-1.1): https://github.com/lkwg82/sonar-java/commit/9dcb5e1864d9deb6ce77c4f5e9c32e856b5a5505

      This is the discussion on the mailinglist: http://sonar.markmail.org/message/fvlcrk2p2wultkvm?q=list:org%2Ecodehaus%2Esonar%2Euser+selector/flag+arguments

        Activity

        Hide
        freddy.mallet Freddy Mallet added a comment -

        The rule might log a violation when :

        • A methods has at least 2 parameters
        • Detect if at least one of the boolean parameters is used to create a branch : if(myBooleanParameter)
        Show
        freddy.mallet Freddy Mallet added a comment - The rule might log a violation when : A methods has at least 2 parameters Detect if at least one of the boolean parameters is used to create a branch : if(myBooleanParameter)
        Hide
        benzonico OLD - Nicolas Peru added a comment -

        @Ann Could you create and link a rspec to this ticket ? thanks.

        Show
        benzonico OLD - Nicolas Peru added a comment - @Ann Could you create and link a rspec to this ticket ? thanks.
        Hide
        m.g.sonar OLD - Michael Gumowski added a comment -

        Ready for validation.

        Show
        m.g.sonar OLD - Michael Gumowski added a comment - Ready for validation.
        Hide
        ganncamp OLD - GAC added a comment -

        some changes to description, but looks good.

        Show
        ganncamp OLD - GAC added a comment - some changes to description, but looks good.
        Hide
        m.g.sonar OLD - Michael Gumowski added a comment -

        Description updated.

        Show
        m.g.sonar OLD - Michael Gumowski added a comment - Description updated.

          People

          • Assignee:
            m.g.sonar OLD - Michael Gumowski
            Reporter:
            lkwg82 Lars K. W. Gohlke
          • Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Due:
              Created:
              Updated:
              Resolved: