Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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]
[marto]: only when I'm bored
[LanX]: we should certainly avoid Amsterdam! ! !
[marto]: kidding. The area in question isn't great. I'd no idea these facilities existed in that part of town.
LanX vile place ...
[LanX]: well, me was kidding!
[marto]: One of the main train stations is right across the road, so handy from that perspective, and there are some nice hotels in the area, but it's not great after dark, or sometimes during the day to be honest
[LanX]: not easy to find good and affordable venues ...
[marto]: hmm, there are several universities and colleges handy that I'd have thought would offer such services, given this is out of term time, I've no experience of paying for venues :)
[LanX]: OK ...
[LanX]: I have no idea how Mark scouted, giving that he lives in London ... but I think he mentioned his wife being Scottish

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2017-12-14 15:46 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (396 votes). Check out past polls.