Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be pattern matching every return value?

When I'm programming in Erlang should I be validating all return values from function calls for success via pattern matching even if i don't intend to use the return value? Most Erlang APIs I've seen so far don't throw exceptions on error (but return something like {error, Error}) so I must need to validate the return value yes? Any exceptions to this where I don't really need to worry about it?

like image 452
Jeremy Raymond Avatar asked Dec 24 '09 00:12

Jeremy Raymond


People also ask

Is pattern matching good?

The advantage of pattern matching is that it is very flexible and powerful at the same time. Within the pattern, the data structure can be dynamically disassembled. These parts can then be assigned directly to variables that only apply within this expression.

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.

Why do we match patterns?

Pattern matching is used to determine whether source files of high-level languages are syntactically correct. It is also used to find and replace a matching pattern in a text or code with another text/code. Any application that supports search functionality uses pattern matching in one way or another.

How does pattern matching work?

Pattern Matching works by "reading" through text strings to match patterns that are defined using Pattern Matching Expressions, also known as Regular Expressions. Pattern Matching can be used in Identification as well as in Pre-Classification Processing, Page Processing, or Storage Processing.


1 Answers

It's good programming style to crash as early as possible when something has gone wrong. Anything you execute after that will be done with the system in an unknown state.

Unless you intend to handle the error value and do something on it, you write your code for the successful case. As in the following little sad loop:

life() ->
  ok = work(),
  ok = rest(),
  life().
like image 189
Christian Avatar answered Oct 21 '22 09:10

Christian