Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think it depends a lot on context and choosing a meaningful metaphor. ActiveRecord for instance uses the class method "find" for finding records in the database, a more meaningful idea than "input a user_id, output a user". For example:</p> <pre><code>User.find(user_id) User.find_by_email(user_email) </code></pre> <p>For conversions, I usually like to write the conversion methods to make it easy to use in higher order functions. For example in ruby, conversions are often done with <code>to_*</code> instance methods, for example to convert a <code>Foo</code> to a <code>Bar</code> it would make sense to have a <code>to_bar</code> method for all foos, so you could write:</p> <pre><code>foo = Foo.new(...) # make a new Foo bar = foo.to_bar # convert it to a Bar </code></pre> <p>And then to convert a bunch of foos, you could simply:</p> <pre><code>bars = foos.map(&amp;:to_bar) </code></pre> <p>Ruby also tends to have <code>Foo.parse(str)</code> for converting a string to the object.</p> <hr> <p>For javascript, I like having class methods (which I got from standard ml), for example:</p> <pre><code>Foo.toBar = function(foo) { return new Bar(...); }; </code></pre> <p>And then you can map over it as well (using underscore in this example):</p> <pre><code>var bars = _.map(foos, Foo.toBar); </code></pre> <hr> <p>the Standard ML convention is structure (class) methods. Example fn types:</p> <pre><code>Foo.toBar : foo -&gt; bar Foo.fromBar : bar -&gt; foo </code></pre> <p>And you'd use it like:</p> <pre><code>val bar = Foo.toBar foo; val bars = map Foo.toBar foos; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload