0

Say I have a function whose input is an array of fruit objects, and returns a list of *cloned) apples from that list, with each cloned apple mutated with an additional property visible on it. Would a good name be extractApplesAndSetVisibility, or is there a better name?

  • 1
    Do you have a single reason you are doing these two things? What is that single reason? – candied_orange Aug 20 '16 at 17:33
1

You shouldn't. The fact that you used the word "and" both in your description of what the function does and even in the name of the function strongly suggests that your function is not doing one thing. It is doing two things, one thing and another thing.

If you split it into two functions, then your naming problem is solved, because both of the functions already have standardized names (in fact, you probably won't have to name them at all, because both of them are very likely to already exist in your language's core or standard libraries).

A function that filters a collection is usually called filter, also sometimes select (Smalltalk, Ruby), where (SQL, XQuery, LINQ), find_all (Ruby); in Relational Algebra it is called a Selection or Restriction. A function that transforms elements of a collection is usually called map, also sometimes collect (Smalltalk, Ruby), transform (C++), select (SQL, XQuery, LINQ); in Relational Algebra it is called a Projection.

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.