DEV Community

Discussion on: Code Smell: No AND in Function name

Collapse
 
n1ru4l profile image
Laurin Quast

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!

Collapse
 
briwa profile image
briwa

I might be missing the obvious, but couldn't you just do:

findPostRecords({ userId, status })

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.

Collapse
 
n1ru4l profile image
Laurin Quast • Edited

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!

Thread Thread
 
andrejnaumovski profile image
Andrej Naumovski

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.

Thread Thread
 
n1ru4l profile image
Laurin Quast

This becomes a mess when only certain object properties should be used together.

Thread Thread
 
seanmclem profile image
Seanmclem

If it takes a standard query object for what to filter by