Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern matching vs if-else

Tags:

scala

I'm novice in Scala. Recently I was writing a hobby app and caught myself trying to use pattern matching instead of if-else in many cases.

user.password == enteredPassword match {   case true => println("User is authenticated")   case false => println("Entered password is invalid") } 

instead of

if(user.password == enteredPassword)   println("User is authenticated") else   println("Entered password is invalid") 

Are these approaches equal? Is one of them more preferrable than another for some reason?

like image 301
Soteric Avatar asked Feb 13 '12 19:02

Soteric


People also ask

Is pattern matching faster than if else?

It turned out that pattern matching in their example was significantly faster than if-else tests. Even though the code doesn't utilize any special pattern match cases that would not be possible with if-else tests, it just compares integers.

What is pattern matching in programming?

Pattern matching in computer science is the checking and locating of specific sequences of data of some pattern among raw data or a sequence of tokens. Unlike pattern recognition, the match has to be exact in the case of pattern matching.

What is pattern matching in Scala?

Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the switch statement in Java and it can likewise be used in place of a series of if/else statements.

Does Java have pattern matching?

From Java 16 onwards, we can do pattern matching using the instanceof operator. It can be used on if statements, and we can use it as type guards on variable assignments and returns. This lets us use a variable without further casting after a type check. This is a new syntax added to the language.


1 Answers

class MatchVsIf {   def i(b: Boolean) = if (b) 5 else 4   def m(b: Boolean) = b match { case true => 5; case false => 4 } } 

I'm not sure why you'd want to use the longer and clunkier second version.

scala> :javap -cp MatchVsIf Compiled from "<console>" public class MatchVsIf extends java.lang.Object implements scala.ScalaObject{ public int i(boolean);   Code:    0:   iload_1    1:   ifeq    8    4:   iconst_5    5:   goto    9    8:   iconst_4    9:   ireturn  public int m(boolean);   Code:    0:   iload_1    1:   istore_2    2:   iload_2    3:   iconst_1    4:   if_icmpne   11    7:   iconst_5    8:   goto    17    11:  iload_2    12:  iconst_0    13:  if_icmpne   18    16:  iconst_4    17:  ireturn    18:  new #14; //class scala/MatchError    21:  dup    22:  iload_2    23:  invokestatic    #20; //Method scala/runtime/BoxesRunTime.boxToBoolean:(Z)Ljava/lang/Boolean;    26:  invokespecial   #24; //Method scala/MatchError."<init>":(Ljava/lang/Object;)V    29:  athrow 

And that's a lot more bytecode for the match also. It's fairly efficient even so (there's no boxing unless the match throws an error, which can't happen here), but for compactness and performance one should favor if/else. If the clarity of your code is greatly improved by using match, however, go ahead (except in those rare cases where you know performance is critical, and then you might want to compare the difference).

like image 183
Rex Kerr Avatar answered Sep 20 '22 06:09

Rex Kerr