jung-hunsoo

jung-hunsoo

Pattern matching is an awesome feature. Many other languages use ifs a lot to support backward compatibility. This occurs often in the fast growing area including Android’s Kotlin.

Elixir and its libraries are not free from this problem too. But it can be resolved significantly by leveraging Pattern matching.

Here’s my concept:

# AS-IS
(prev) fn clean_obsolete_record(%{id: 31}), do: ...
(current) fn clean_unused_record(%{id: 31}), do: ...
(next) fn clean_unused_record_2(%{id: 31}), do: ...

# TO-BE
(prev) fn clean_obsolete_record(%{id: 31}), do: ...
(current) fn clean_obsolete_record(%{id: 31}, vsn: 2), do: ...
(next) fn clean_obsolete_record(%{id: 31}, vsn: 3), do: ...

This code convention provides three advantages:

  1. Backward-compatibility is achieved easily.
  2. Codes manageability is improved.

Is this feasible and useful approach?

Showing Posts 1 to 3

tty

tty

My preference would be:

(prev) fn clean_obsolete_record(%{id: 31}), do: ...
(current) fn clean_obsolete_record(%{id: 31, vsn: 2}), do: ...
(next) fn clean_obsolete_record(%{id: 31, vsn: 3}), do: ...

This makes it clear from a data perspective which function would execute. If you happen to store the function argument in a database or have to replay the data there is a clear indicator which function would execute.

YMMV

Fl4m3Ph03n1x

Fl4m3Ph03n1x

I prefer this approach. this pattern is not really new in Elixir when you need to control flow.
There is a pattern called Token:

Which fits nicely into this. Basically a Token contains all the information of your request and workflow, and it allows you to pattern match on versions like you are doing.

jung-hunsoo

jung-hunsoo OP

@tty Thanks. It would work too, since the pattern matching also compares parameters. What I’m thinking about is fn clean_obsolete_record(general_args, vsn: vsn), do: ... which separates general_args with a predefined vsn parameter. vsn can be overwritten If they are mingled in the collection. Also this approach enforces versioning to the developers.

@Fl4m3Ph03n1x Thanks. I understand that the tokening in the link page is one good example of pattern matching. (Pattern matching is so useful and make the codes cleaner than when guard.) For me, the original idea was kinda API versioning commonly used in API endpoint.

Versioning APIs will provide backward compatibility and eliminate deprecation problems significantly and easily.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews