State Setters boilerplate: suggestions, counter-arguments?
See original GitHub issueI find that I am creating a lot of boilerplate setters for each StoreState object.
E.g.
const JobStore = {
jobData: {},
jobBoard: [],
jobList: [],
setJobData: action((state, payload) => {
const json = JSON.stringify(payload, ' ', 4)
state.jobData = json
}),
setJobBoard: action((state, payload) => {
state.jobBoard = payload
}),
setJobList: action((state, payload) => {
state.jobList = payload
}),
I have to do this because my thunks cannot directly set the state (which I understand the reason for that).
But it is a common feature in OO Programming to be able to automate the creation of property accessors (get/setters) for Object properties.
The userStoreState() makes the getters trivial for store properties. What about some sort of shorthand we can use in a thunk for property setters (a helper function?). I am only talking about items in the same store slice, (eg. setState() and not setStoreState() - but you could have that for folks that want to live dangerously).
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
programming languages - A defense for boilerplate?
What may look like boring, repetitive boilerplate to you as you're writing ... all languages are as wasteful as the getters and setters...
Read more >"I still stubbornly believe the whole “private members accessed via ...
Version 1: I pass the sprite object to the renderer, and the renderer gets the texture contained in the sprite and draws it,...
Read more >How to avoid having to write boilerplate accessor code
So, my strategy for avoiding boilerplate code: Try to avoid getters and setters. When in need of some pure data class I declare...
Read more >Strategic Scala Style: Principle of Least Power
"Setter Injection" refers to instantiating an object, and then setting some variable onto that object which it will use when you call methods...
Read more >The Language I Wish Go Was - Bob Nystrom - stuffwithstuff
I hate the boilerplate of Java and the compile times of C++. ... You have no idea what state it's in. ... Property...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

One can also make this a bit more robust by using the immer {original} function:
@DrYSG - I am going to shy away from introducing additional abstractions as being described above. My gut feeling is that this should belong in user-land. It is fairly easy to introduce your own helpers/abstractions in order to achieve what you feel is necessary on a per case basis.
Thanks for going through this in detail though. Perhaps others can learn from your journey if they visit this issue. 👍