hectorsq

hectorsq

Using protocols for better code organization

Hello!

I am writing an application that translates JSON messages to SOAP messages. Currently I am handling 16 JSON messages and 20 SOAP messages.

I have defined embedded schemas to validate the incoming JSON messages and to build the XML SOAP message using heex templates.

I have a Parse module with a multiple clause parse function for each incoming message. I also have a Convert module with a multiple clause convert function for each JSON message. This same pattern is repeated in other modules and each module has hundreds of code lines.

I feel this is a smell for bad code organization. Am I right?

I have the idea to use protocols to reorganize my app. I would move each parse and convert function to the corresponding schema file. Is it a good idea?

Would my code be more object oriented (which I want to avoid)?

I appreciate any comments.

Most Liked

dimitarvp

dimitarvp

For 7.5 years with Elixir I only ever needed to implement a protocol for one thing – Jason for our own structs in the project.

Make sub-modules and have them implement behaviours – one for parsers and one for converters. Behaviours are not strictly needed but at least if you make a mistake in an implementing module’s function’s signature then your IDE + LSP will warn you.

Your central parser module still has to detect to which specialized parser module it must send the data. What you’ll gain is that the number of coding lines in it will be drastically reduced because you will only keep all the pattern-matching function heads, but now their bodies will only contain code like ParserTypeXYZ.parse(input_data) and nothing else.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement