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

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement