Note that there are some explanatory texts on larger screens.

plurals
  1. POHow far should I take referential transparency?
    text
    copied!<p>I am building a website using erlang, mnesia, and webmachine. Most of the documentation I have read praises the virtues of having referentially transparent functions.</p> <p>The problem is, all database access is external state. This means that any method that hits the database is no longer referentially transparent.</p> <p>Lets say I have a user object in a database and some functions that deal with authentication.</p> <p>Referentially opaque functions might look like:</p> <pre><code>handle_web_request(http_info) -&gt; is_authorized_user(http_info.userid), ... %referentially opaque is_authorized_user(userid) -&gt; User = get_user_from_db(userid), User.is_authorized. %referentially opaque lots_of_other_functions(that_are_similar) -&gt; db_access(), foo. </code></pre> <p>Referentially transparency requires that I minimize the amount of referentially opaque code, so the caller must get the object from the database and pass that in as an argument to a function:</p> <pre><code>handle_web_request(http_info) -&gt; User = get_user(http_info.userid), is_authorized_user(User), ... %referentially opaque get_user(userid) -&gt; get_user_from_db(userid). %referentially transparent is_authorized(userobj) -&gt; userobj.is_authorized. %referentially transparent lots_of_other_functions(that_are_similar) -&gt; foo. </code></pre> <p>The code above is obviously not production code - it is made up purely for illustrative purposes.</p> <p>I don't want to get sucked into dogma. Do the benefits of referentially transparent code (like provable unit testing) justify the less friendly interface? Just how far should I go in the pursuit of referentially transparancy?</p>
 

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