Satendra Kumar created KAFKA-3768:
-------------------------------------

             Summary: Replace all pattern match on boolean value by if/elase 
block.
                 Key: KAFKA-3768
                 URL: https://issues.apache.org/jira/browse/KAFKA-3768
             Project: Kafka
          Issue Type: Bug
            Reporter: Satendra Kumar
            Priority: Minor


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

For example:

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
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 6 line  byte code. Pattern match are more 
expensive comparison to if/else block.




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to