chrisliaw

chrisliaw

Is it common to create accessor functions for struct fields?

Hi,

I’m wondering is it my thinking process or this is the norm among the Elixir developer for the use of Struct and accessor functions (get/set)?

For example:

defmodule Session do
    defstruct [:name]

    # this is just example as there might be 
    # more transformation before putting the value inside the struct
    def set_name(%Session{} = session, name) do
        %Session{session | name: String.upcase(name)}
    end
    
    # likewise there might be more transformation before returning the value
    def get_name(%Session{} = session) do
        String.downcase(session.name)
    end
end

The reason to create the accessor is somehow (probably OO habit die hard, or not?) along the argument of to isolate the user of the struct to access the key directly to allow future changes to the struct key without affecting the caller. For example later if the struct change the field to :customer_name, caller using the get_name() function would not aware the field has changes its name.

But this pretty much feels like OO thinking in me. Is that an alternative or is that a sensible way to design this structure?

Cons is now get/set is everywhere inside the struct module.

Thanks!

Regards

First Post!

Eiji

Eiji

There are lots of packages dedicated for structs:
Search results for: struct access @ hex.pm

Many of them adds support for Access behaviour, so you can use it with *_in functions like get_in.

That’s rather not about FP or OO paradigms, but about struct documentation. Some parts may be private, so if they are not well documented then the developer assumes that specific keys are private and should be not changed by hand. Documenting all fields would require a support for them in future as long as you would not hard deprecate them.

User-defined types | Typespecs reference @ Elixir documentation

Not really objective if you do not update the object … No worries for that. If it’s not a “simple struct” then you can follow Phoenix generators i.e. use context-based functions to fetch data from database and use struct (in ecto it’s called schema) module to define other helper functions like get_full_name etc.

Most Liked

krasenyp

krasenyp

Speaking of dependency injection, in FP it’s called an argument to a function. I really don’t understand why most people writing Elixir just don’t like passing dependencies via function arguments. If you don’t state what your logic depends on, it depends on everything and it’s almost impossible to isolate and test.

sodapopcan

sodapopcan

Thank you! I kept seeing people talking about DI in this thread and another and kept thinking I was missing something.

Ehn, “constructor” is a legit FP term :upside_down_face: I do hate when people say “method” though, mostly because I feel that deep down they really do believe they are somehow still methods :sweat_smile:

Near the end of my time with OO, I started feeling getters and setters were pretty pointless, feeling like a holdover over for when code like this was acceptable:

user = new User
user.set_name("Bender Bending Rodríguez")
user.set_email("bender@planetexpress.com")

Maybe it still is? I dunno. But I view it as a giant smell if method names prefixed with get_ and set_ doing anything or than returning the raw property value. It’s just plain confusing. If you do this, you not longer have a “getter” or “setter” and the method name should reflect this.

In terms of renames, I’ve never been in a situation where renaming a field didn’t result in a project-wide rename. I haven’t worked on any truly massive projects before (biggest was ~1 million lines of Ruby) and of course it would be different for an app vs a library. I have heard someone on this forum lament not being able to change the name of a field in their library, but there are still other ways to deal with the mismatch (which I believe have been alluded to already?)

So many other thoughts on this thread but I’ll leave it at that!

D4no0

D4no0

If you get out of your head the idea that tying data structures + behavior to modules is a good idea, you will understand that operating with data structures directly, without tying them to additional logic that is part of the same module, is easier to reason about in the code and more maintainable.

I honestly don’t like the fact that structs are tied to a module, there are technical reasons why it was done like this, however at the same time it sends a wrong message to people coming from OOP languages. The scope of the structs is to have something that resembles enforceable types, that can be checked easily by tools like dialyzer, tying behavior to those types is not the most optimal way to write code.

If you are just starting out, my advice is to just avoid using structs until you get a good hang on how to write elixir, use guards/pattern match to achieve the same guarantees structs offer. If you’ll use data structures that are not tied to any modules for some time, it will start to click in place the ideology where you design pipelines that transform data, instead of imperative logic tied to a module.

Where Next?

Popular in Discussions Top

AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20142 166
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18634 126
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

We're in Beta

About us Mission Statement