Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Frivolous function names

by dws (Chancellor)
on Dec 06, 2001 at 00:35 UTC ( #129741=note: print w/replies, xml ) Need Help??

in reply to Frivolous function names

Just today I ran across the need to have a pair of functions, one of which would try to fetch something and return undef if it couldn't, the other of which would try to do the fetch, and would create the thing if it wasn't there.

On the theory that names are most useful from the client's perspective, and not the service's, how do these differ? If the client doesn't need to know whether the Foo it is requesting from the service is potentially created on-the-fly, then getFoo() is a perfectly reasonable name. To use a name like getOrCreateFoo() can reveal to the client aspects of the service that are none of the client's business.

In cases where a client does legitimately need to make a distinction between whether the service already has a Foo, naming gets problematic. Assuming that the service has getFoo() and newFoo() functions, then getOrCreateFoo() is really behavior that a client could synthesize, but which the service is absorbing as a convenience to its clients. In these cases, I've tended toward a scheme of findFoo() and getFoo(), where the former means "give me one if you've got it" and the latter means "give me one". In both cases, though, a client needs to check for none. Even in the case where a new object is going to be created on the fly, things can happen, and the burden of verifying is always on the client.

The problem of a service absorbing convenience functions into its API is a tricky one. On one hand it prevents duplication of code in clients, while on the other hand it introduces bloat into the API.

Replies are listed 'Best First'.
Re (tilly) 2: Frivolous function names
by tilly (Archbishop) on Dec 06, 2001 at 01:00 UTC
    The client did need to know.

    There is a sharp distinction between attempting to access things and trying to create/overwrite/modify them. I have a need to expose that distinction.

    As for the find/get distinction, I thought about that but decided against it because where get appears elsewhere in this codebase it generally means "fetch" and doesn't autovivify. If you and those around are consistent about it, find/get is a doable proposition, but I don't want to deal with someone in 3 months saying, "Why did it just create that?" and having to say, "Well, didn't you know to use find instead, unlike how it works everywhere else?"

    I figure that people won't accidentally summon, and if they do try summoning, they won't be terribly shocked if summoning does something rather different than getting does. (Nor am I terribly shocked at the idea that a summoned thing could have been conjured up out of nothing.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129741]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2019-08-20 11:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found