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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to Re: Frivolous function names by dws
in thread Frivolous function names by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2014-07-12 10:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (239 votes), past polls