Function should adhere to the Single Responsibility Principle - meaning it should do one thing and one thing only. So if your function name inclu...
For further actions, you may consider blocking this person and/or reporting abuse
Great article!
Especially when doing database operations I quickly find myself naming my methods/functions something like this:
findPostRecordsWhereUserIdAndStatus({ userId, status })
.This obviously becomes worse with more parameters involved in a query. For me, it is always a trade-off between exactly knowing what the method/function does and having an awfully long name.
I am curious how you and other devs deal with such things, let me know!
I might be missing the obvious, but couldn't you just do:
If you're using Typescript/Flow (or Eslint to some degree, but I have to check), the arguments would be shown in your IDE so that you would know them when you type them, without having the need to be part of the function name.
That works well when you always have the same criteria for finding post records. However, once you have to fetch post records under different conditions you need to overload (which is messy is not so cool in js).
Nevertheless, I can see how your approach could work for projects that are not too big!
You don't need to overload. Just make the function take a single object as an argument with optional properties, then filter out the undefined ones at the start.
This becomes a mess when only certain object properties should be used together.
If it takes a standard query object for what to filter by
Hi! Thanks for sharing.
SRP seems so easy to understand, but in fact it is not.
Toy examples are good to make a point, but hardly match the complexity of a real system.
So even in your example, the function which makes the breakfast is gonna call all three the function you defined (eggs, tea, flour).
Does that respect the SRP? It depends on the context.
Alrought having 'And' in a function name is a smell, what about the same one renamed without the 'And'?
The definition of SRP itself causes these kind of conflicts, which is why I use a different one, which takes into account what is the effect of SRP:
Functions X respects SRP if it has only one reason to change.
Nice article π
Though you should be wary of carrying the idea of single responsibility too far. Otherwise, you'll end up in the JavaScript/NPM world of
is-odd
,is-even
andleft-pad
all managing their own single responsibilities!Fair point, thanks for noting that π
Sounds like a great idea for a custom tslint/eslint rule
It totally could be π€©
I use method names: drink(), add(), find(), insert(), update(), delete(), etc.
In the example of 2 functions, makeTea and addSugar, what would you name a function that eventually needs to call both functions?
You have a new function...
This was a really nice way of explaining this important topic! Thanks :)
Awesome, glad it helped! π