AstonJ
There is a wikipedia definition on this but it doesn’t really cover how you can go further in functional programming. I feel PragDave’s comment (from his course) is better from an Elixir perspective:
I know I’m like a broken record, but I can’t help it. I’m really excited to see how a functional approach makes it so much easier to honor the SRP, both at the function and module level. The fact that state is passed around, and not simply shared, means that functions can and modules can both be split and moved around.
What does the SRP mean to you? How far do you go? How strictly to you adhere to these (or your own) guidelines?
Trending in Discussions
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...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
AstonJ
I’m moving my thoughts out into a separate post here
To me the SRP means:
I’m not sure if this counts as part of the SRP, but apps being split into components could be closely related as well.
peerreynders
Related: From the horses mouth NDC 2012 Norway: Robert C. Martin - The Single Responsibility Principle (51:08)
[quote]Modules should only be concerned with one thing[/quote]
“Responsibility” is a bit vague and its scope is sometimes open to interpretation. “Things that change for the same reason should be grouped together” seems a much clearer way of describing what SRP is all about - and it’s a notion that can be more sensibly applied at varying levels of granularity.
At higher levels of granularity we enter the realm of loose coupling vs. high cohesion (and the boundary that separates both).
Ultimately the idea is to keep change (over time) contained in highly focused areas and minimize “ripple effects” to dependent parts.
PS: Scott Wlaschin likes to point out:
ericgray
Thank you for recommending elixir for programmers by Dave Thomas. The course is fantastic and very insightful. The more I learn about FP and Elixir the more my eyes are becoming opened to a better way of writing programs.
I really like the idea of SRP. I usually start with a function that does too much and then chop it up into little digestible chunks, i.e. multiple function definitions
I also really really like Microservices. Of course you shouldn’t go crazy with it but apps being split into components makes a lot of sense to me. I’ve worked on monolithic apps where things were just out of control and unmanageable. I think Elixir is tailor made for SRP and microservice style development. One thing I’m unsure about is the database layer. I know Dave thinks Phoenix should only be a web interface and know nothing about the database but databases can be complicated to configure and use as a microservice.
hubertlepicki
I have a theory that a lot of these things we learned when doing OOP, no longer make sense when you switch to functional programming. This is precisely because they solve problems that are a result of using Object-Oriented programming in first place.
Take this single responsibility principle for a review, and compare this to how things from standard library work. In either version (one responsibility, one reason to change), this is being broken all the time.
In Elixir & Erlang, modules group together the related functions. That’s it, there’s no big theory behind it. These functions may perform one or multiple things, but as long as it makes sense to keep them in the same module - they are kept there.
hubertlepicki
It might be pretty good idea to apply this principle to actors however. In particular, breaking down complicated processes to a bunch of related ones. One to keep the state, one to handle the transitions logic (FSM), another one to persist changes etc. Each one being responsible for one thing and one thing only
AstonJ
He talks about splitting things into roles/who might need them - so separate code that’s related to a designer, code that generates reports for a clerk, code responsible for reports for a manager etc. He also suggests that’s ok to repeat code as it may change for one or the other, for example, a clerk and manager may need the same code to begin with but one of their needs might change in the future - so this kind of separation allows that kind of change easily.
Would you agree that’s the gist of it?
For me I think that adds quite a bit of mental overhead, and that it may be simpler to just think in terms of what something does, and create functions and modules focused on that one thing (which would then also allow things to be reused). Perhaps I am being a bit too lazy… or just need a few other examples to better picture it.
You should definitely check out the video in @peerreynders’s post here:
Where Dan talks about the Replaceable Component Architecture (see Peer’s links in the post as they jump right to the appropriate parts).
I’m in the same boat. I hope Dave does another course but this time jumps right into the application (rather than teaching the language basics) and covers databases. I’m going to email him with the suggestion once I’ve finished the course
In the meantime, check out my post and the link in it here (again thanks to Peer for that link!)
I’m glad you are enjoying Dave’s course too - I love it! Am 90% through it now - can’t wait to finish it
I think this is one of the reasons why people are so excited about Elixir (and similar languages) because they make it so easy to follow good principles like the SRP
One of the things that surprised me in Dave’s course is when he got to a Phoenix view and said that it was about 35 lines which seemed a bit large to him
(I was like, you should see my Rails models
) and said that’s normally a good indication that there is a group of code related to one thing, and sure enough there were 22 lines related to one thing (state) and we extracted that out into a dedicated
_statehelper.This also falls in line with the ‘fits in my head’ mantra, where you shouldn’t be looking at code that is bigger than your head (i.e no scrolling!). “If you have to page up and down - it’s probably too complicated”.
I love that talk btw - so many good ideas presented in it
(Thanks Peer!)
hubertlepicki
I think @pragdave might be importing some wisdom from his object-oriented life ;). I was thinking about it when I first opened Elixir’s own source code files, or sources of prominent Elixir libraries like Ecto, and was shocked by the large files containing hundreds of lines of code (+many more lines of documentation). In Erlang they even add a lot of unit tests at the end of the file to keep the tests close to the module they test! (moduledoc in Elixir but more popular I think)
But then, I realized what is a reason for having a short classes and it suddenly made more sense. The reason why we want to keep our classes short and clean is that we can mentally model how instances of this class will behave, and how they will modify the object’s attributes in it’s life cycle. So - we couple state to the functions that operate on this state, and we want this to be as small as possible - otherwise we easily loose track of how this works, and bugs sneak in.
In Elixir, the module has no state. The main reason for keeping classes short does not apply here anymore. Of course, it becomes tiresome to work with files that are thousands lines long, but there is no such thing as internal state of object to be concerned with.
Having said the above: again, the object-oriented wisdom of keeping classes short does have it’s place in Elixir, and again this is when you create processes (GenServer etc). I think it makes total sense to keep them short in terms of lines of code and focused on one thing.
kokolegorille
I like this picture taken from Scott Wlaschin youtube video on functional design pattern.
But I also enjoyed Uncle Bob’s reply to it.
AstonJ
Personally I feel shorter modules can impact readability and therefore make it easier to ‘make sense’ of what’s going on. I also think they’re less off-putting and fatigue-inducing; when faced with having to make sense of one huge file or a few smaller ones, I think the smaller ones are more digestible and therefore might be more appealing.
There is another example in Dave’s course when working with Genservers. He says one way to go about things is to use a single file, but his preferred way used three. When comparing the two, I preferred his way of breaking things apart too.
I know it probably sounds like I’m on commission (I’m not! I just really REALLY love it!) but I highly recommend his course, even to experienced developers like yourself - if nothing more than to see how other experienced developers are going about things
hubertlepicki
I… thanks for the kind words but I’m still learning all things Erlang & Elixir