Uploaded image for project: 'Kafka'
  1. Kafka
  2. KAFKA-3768

Replace all pattern match on boolean value by if/elase block.

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 0.10.1.0
    • None
    • None

    Description

      Scala recommend use if/else block instead of pattern match on boolean values.

      For example:

      Comparasion.scala
      class Comparasion {
      
          def method1(flag: Boolean): String = {
                flag match {
                   case true => "TRUE"
                   case false => "FALSE"
                 }
          }
      
        def method2(flag: Boolean): String = {
                if(flag) {
                         "TRUE"
                   }else {
                     "FALSE"
                   }
                }
      
      }
      

      Byte code comparison between method1 and method2:
      scala>javap -cp Comparasion

      Comparasion.class
      Compiled from "<console>"
      public class Comparasion {
        public java.lang.String method1(boolean);
          Code:
             0: iload_1
             1: istore_2
             2: iconst_1
             3: iload_2
             4: if_icmpne     13
             7: ldc           #9                  // String TRUE
             9: astore_3
            10: goto          21
            13: iconst_0
            14: iload_2
            15: if_icmpne     23
            18: ldc           #11                 // String FALSE
            20: astore_3
            21: aload_3
            22: areturn
            23: new           #13                 // class scala/MatchError
            26: dup
            27: iload_2
            28: invokestatic  #19                 // Method scala/runtime/BoxesRunTime.boxToBoolean:(Z)Ljava/lang/Boolean;
            31: invokespecial #23                 // Method scala/MatchError."<init>":(Ljava/lang/Object;)V
            34: athrow
      
        public java.lang.String method2(boolean);
          Code:
             0: iload_1
             1: ifeq          9
             4: ldc           #9                  // String TRUE
             6: goto          11
             9: ldc           #11                 // String FALSE
            11: areturn
      
        public Comparasion();
          Code:
             0: aload_0
             1: invokespecial #33                 // Method java/lang/Object."<init>":()V
             4: return
      }
      

      method1 have 23 line of byte code and method2 have only 6 line byte code. Pattern match are more expensive comparison to if/else block.

      Attachments

        Activity

          People

            satendra Satendra Kumar
            satendra Satendra Kumar
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 1h
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified