Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Thursday, April 10, 2008

[Programming] The Proper naming pattern: Extract the guts of foo() into fooProper()

Often in programming you will have two versions of a function: one that serves as an outer shell, and the "real" function itself. Suppose you have extracted the guts of addAttachment() into addAttachment2().

A difficulty confronts you: What do you call the new function? addAttachment2() is a lousy name.

A wonderful adjective can help us here: proper. We would call the new function addAttachmentProper():

FliE1

The function proper is *the* function, the one that does the work. I live in Victoria, but not in Victoria proper (the central part of the city). "proper" is a convenient term to denote The Actual Function.

More examples:
  • iconUrl() and iconUrlProper()

  • broadcast() and broadcastProper()

  • url() and urlProper()

  • delete() and deleteProper()

8 Comments:

  • Names and naming conventions are interesting, and I would love to know more on your thoughts around this topic.

    I know that I always have a hard time coming up with meaningful class names for CSS, that are descriptive of the content they label, but at the same time flexible enough to be used throughout a site.

    Any recommendations of books/sites to get naming inspiration from Jon?

    By Anonymous Anonymous, at 4/11/2008 2:22 a.m.  

  • Hey Paul - Alas, I wish I knew of a good book on naming. Will let you know if I come across one.

    By Blogger Jonathan, at 4/11/2008 7:19 p.m.  

  • I may set up a wiki on the matter then. I've just been thinking lately about terms like shim, that was a name often given to 'spacer GIFs' and how the name is derived from a real world engineering example. There was another I saw, but I forget it's name for the moment. What other real world techniques and methods can provide names for common HTML/CSS components and styles I wonder?

    By Anonymous Anonymous, at 4/14/2008 2:19 p.m.  

  • Good idea Paul. Actually I know of a few "Pattern" books that name various programming patterns. But I have yet to see a book on the principles of creating pattern names.

    Anyway, here are some examples of pattern names from various programming-pattern books:

    Design Patterns by Gamma et al.
    - Adapter
    - Bridge
    - Composite
    - Decorator
    - Facade
    - Flyweight
    - Proxy

    Patterns of Enterprise Application Architecture by Fowler et al.
    - Class Table Inheritance
    - Data Transfer Object
    - Dependent Mapping
    - Gateway
    - Identity Map
    - Page Controller
    - Separated Interface
    - Single Table Inheritance
    - Special Case
    - Table Module
    - Transform View
    - Two Step View

    Too bad these names have been taken by the programming field - I could think of some interesting uses for them in CSS/XHTML.

    By Blogger Jonathan, at 4/14/2008 5:58 p.m.  

  • This kind of function definition is way more common in functional programming. In Haskell the convention (taken from mathematics in a roundabout way) is to call the second function "f prime" (where f is the original function name). Because ' is a valid character in a Haskell function name that is used (same as the mathematical notation):

    f x = f' x
    f' x = ...

    It's so common in fact that the working title for the next version of Haskell is Haskell' (that is, "Haskell Prime").

    Real life example from Data.List library:

    mergesort cmp = mergesort' cmp . map wrap

    mergesort' cmp [] = []
    mergesort' cmp [xs] = xs
    mergesort' cmp xss = mergesort' cmp (merge_pairs cmp xss)

    By Blogger Thomas David Baker, at 4/21/2008 4:58 a.m.  

  • Very neat - thanks Tom!

    By Blogger Jonathan, at 4/21/2008 8:27 p.m.  

  • Or if you're coding the thing up or with few calls/dependants you could rename "addAttachment" to "addAttachmentWrapper" and the "proper" could be named "addAttachment".
    The bigger problem I think is when you get *real* distributed computing yubnub-style - piped web APIs, but which are not in piped syntax, but some Java-like syntax - com.yubnub.test.APIs.TestFunction1.getArgmuent(1).getArgumentType().toString();

    Then, typing this thing out will be a pleasurable revision of the package hierarchy for experienced programmers, but people new to that particular API, it'll be hell.

    So you'll need awesome autocompletion in IDEs.
    Add a couple of JSP/XML/ multi-markup syntaxes and you get a delicious token-soup.

    Will we then resort to shortnames?

    #shortname cytaTF1gAGATtS();
    // for
    // com.yubnub.test.APIs.TestFunction1.getArgmuent(1).getArgumentType().toString();

    Compilers, parsers, interpreters can be modified easily to handle all this, but how do we make this simple to code up?

    This idea probably a year or so ahead of its time in scripting languages, but the tools for hacks for such problems are very much
    around - "include", "autoload" in PHP, for example.
    What do you say?

    #define macros in C/C++ can also be made to work similarly.

    (I hope this idea isn't patented yet. It's prior art if so.)

    By Anonymous Anonymous, at 10/12/2008 3:44 a.m.  

  • Agreed - PHP autoload is a great feature! Anything to reduce the amount of code is most welcome.

    By Blogger Jonathan, at 10/12/2008 10:30 a.m.  

Post a Comment

<< Home