Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd actually expect TO to look more like this:</p> <pre><code>class User def increment_points!(points, increment_credits) @points+=points if increment_credits @points-=points unless increment_credits end end class Activity def increment_user_points! @user.increment_points!(points, true) end end </code></pre> <p>Creating a module to <code>include</code> would seem to create <em>more</em> complexity. And the whole point of the Law of Demeter (I like to think of it more as a guideline..) is that you ought to be able to do whatever you like to <code>User</code>'s internals without having to rewrite much code outside the class. Your <code>UserDelegator</code> module doesn't help much -- you still get to re-write that code when you fiddle with <code>User</code>'s internals.</p> <p>But if it were me, I don't think I'd even bother with this, unless you're finding yourself rewriting a <em>lot</em> of code to make simple changes to <code>User</code>. Maybe that's just because I'm used to the Linux kernel coding style, which breaks the law of Demeter on a regular basis:</p> <pre><code>static inline int need_reval_dot(struct dentry *dentry) { if (likely(!(dentry-&gt;d_flags &amp; DCACHE_OP_REVALIDATE))) return 0; if (likely(!(dentry-&gt;d_sb-&gt;s_type-&gt;fs_flags &amp; FS_REVAL_DOT))) return 0; return 1; } </code></pre> <p>Three objects away :) and I'm not sure the code would be more legible if written:</p> <pre><code>need_reval_dot(dentry) { if(likely(!dentry_need_reval_dot(dentry)) return 0; } dentry_need_reval_dot(dentry) { return superblock_need_reval_dot(dentry-&gt;d_sb); } superblock_need_reval_dot(sb) { return fs_type_need_reval_dot(sb-&gt;s_type); } fs_type_need_reval_dot(s_type) { return fs_flags_need_reval_dot(s_type-&gt;fs_flags); } fs_flags_need_reval_dot(fs_flags) { return fs_flags &amp; FS_REVAL_DOT; } </code></pre> <p>So I'm all in favor of following guidelines in moderation -- ask yourself if your modifications actually lead to cleaner, more maintainable code, or if it is just following a rule for the sake of following rules.</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