I recently picked up 'Clean Code' by Robert Martin and have taken to refactoring my recent project. I'm realizing how important it is to name variables and functions in a way that clearly expresses their intent.

One of my functions is called addEvent(). But the problem is, this function sometimes adds 1 event and sometimes adds multiple events, depending on the arguments it's given.

I'm trying to come up with a suitable name for this function but nothing really sounds right to me:

addEventOrEvents() ?

addEvents() ?

addOneOrMoreEvents() ?

I'm also aware that a function is only supposed to do one thing. So maybe I need to break this up into more than one function? But in that case, I can't think of what the function that determines how many events to add would be called!

  • 2
    So why not addEvents if that is indeed what it does? – user42401 Sep 29 '17 at 16:23
  • Sometimes it only adds 1 event. Wouldn't that imply that it's always adding more than 1 event? – Ethan Fischer Sep 29 '17 at 16:25
  • 8
    Is it important to know for the caller of this function whether one or multiple events were added? If a case that currently needs only one event is changed to require multiple events, is that of enough importance to tell the caller of this function about the change? If not, then I would simply ignore the fact that the function sometimes adds only a singe event and go for the spelling addEvents. – Bart van Ingen Schenau Sep 29 '17 at 16:26
  • That makes sense. I expect this function will most often be used to add a single event, and sometimes multiple events. But it isn't really important for the caller of the function to know whether one or multiple events are added. Thank you, I think you've convinced me addEvents() makes the most sense. – Ethan Fischer Sep 29 '17 at 16:33
  • Without a bit more context I find it hard to write a meaningful answer. Can you clarify? What’s the complete signature of that function? How is it used? Is it important for the caller if one or more events are added? – besc Sep 30 '17 at 8:25

If you call your function "addEvents" then I would assume that it is capable of handling a single event correctly, and not only two or more. I might even assume that it can handle zero events correctly.

Now I had to use a function that assumed an array of items, and lots of code calling it that created an item, created an empty array, pushed the item to the array, passed the array. That's inconvenient. A function using a variable number of arguments might be nicer. If it is inconvenient to call the addEvents function, you could have addEvents and addEvent, and one of them calls the other.

Some languages make it very easy to create an array, like @[item] in Objective-C is an array containing that one item. Swift is especially nice: You can declare a function to take a variable number of arguments. It actually takes an array, the compiler turns your arguments into an array, or you can actually pass an array. So you could call addEvents (event) or addEvents (event1, event2, event3) or addEvents (eventsArray) and it all works.

  • Java's varargs addEvent(Event... events) also works well for this, auto creating the array for you. – user949300 Sep 30 '17 at 20:54
  • Agreed, functions that take a list normally take a list of one. – Loren Pechtel Sep 30 '17 at 23:52

The obvious answer is:

  • addEvent adds one event as the name says
  • addEvents adds more than one event and would call addEvent subsequentially.

Functions that do more than one thing are a candidate for refactoring.

  • IMO this is taking SRP too far. Unless there is some dramatic difference between adding one and adding N events, which is possible but unlikely, a single method call is simpler. What if the calling code has an array of events? In your scheme, they have to test their length and then decide which method to call. Cumbersome. – user949300 Sep 30 '17 at 20:53
  • @user949300 taking things too far is exactly what these "principles" are all about. – Frank Hileman Oct 3 '17 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.