<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Adrian Lee</title>
    <description>The latest articles on Forem by Adrian Lee (@os6sense).</description>
    <link>https://forem.com/os6sense</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F302878%2F34951cab-9bda-41b7-9469-addc25732ef3.jpeg</url>
      <title>Forem: Adrian Lee</title>
      <link>https://forem.com/os6sense</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/os6sense"/>
    <language>en</language>
    <item>
      <title>Ruby: Call me (in which an old dog learns new tricks)</title>
      <dc:creator>Adrian Lee</dc:creator>
      <pubDate>Mon, 03 Oct 2022 20:46:03 +0000</pubDate>
      <link>https://forem.com/os6sense/ruby-call-me-in-which-an-old-dog-learns-new-tricks-5c7h</link>
      <guid>https://forem.com/os6sense/ruby-call-me-in-which-an-old-dog-learns-new-tricks-5c7h</guid>
      <description>&lt;p&gt;A few years ago I worked somewhere where the lead dev wanted us to adhere to SRP and expose the class and instance via a single interface named &lt;code&gt;call&lt;/code&gt; e.g:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def Foo
  def self.call = new.call
  def call = "hello"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This actually ended up making a pretty clean codebase in certain respects (certainly helped ease the problem of naming things) although ignoring more obvious conventions such as &lt;code&gt;to_h&lt;/code&gt; when returning a hash, &lt;code&gt;to_s&lt;/code&gt; for strings etc eventually left me feeling it was too rigorously applied.&lt;/p&gt;

&lt;p&gt;That aside, one of the missed opportunities was in generating a broader understanding of &lt;em&gt;why&lt;/em&gt; a class/instance like this is useful and I recently had an opportunity to revisit the question.&lt;/p&gt;

&lt;p&gt;In case you aren't aware, there are couple of ruby objects which already respond to &lt;code&gt;call&lt;/code&gt; namely &lt;code&gt;lambda&lt;/code&gt; and &lt;code&gt;proc&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;l = lambda { |x| x * x }
puts l.call(2) # 4

p = proc { |x| x + x }
puts p.call(3) # 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also covert a method into a proc using &lt;code&gt;method&lt;/code&gt; or (preferably) &lt;code&gt;public_method&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def foo(x) = x - x
m = method(:foo)
puts m.call(10) # 0 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this in mind you can pass around a lambda, a proc, a method, a class, or an instance of a class, all executed with the same interface. This is very useful for callbacks and differed execution.&lt;/p&gt;

&lt;p&gt;As for tricks ... there are several ways to actually call &lt;code&gt;call&lt;/code&gt;. &lt;code&gt;l.call(2)&lt;/code&gt; is obvious but you can also do &lt;code&gt;l.(2)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;For procs, lambdas, and methods, there is also &lt;code&gt;l[2]&lt;/code&gt; and &lt;code&gt;l.yield(2)&lt;/code&gt; (I didn't know this!) but in the case of a class or instance, you'll have to add support (e.g. &lt;code&gt;alias_method :[], :call&lt;/code&gt;) which starts to make the class a little messy, so I tend to stick with &lt;code&gt;.()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But there's more. if you're a fan of Elixirs pipes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;l = lambda { |x| x * x }
p = proc { |x| x + x }
output = proc { |x| puts "*** #{x} ***" }

(l &amp;gt;&amp;gt; p &amp;gt;&amp;gt; output).call(2) # *** 8 ***
(output &amp;lt;&amp;lt; p &amp;lt;&amp;lt; l).call(2) # *** 8 ***
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I don't know that I'd approve code like this in a PR unless there was a really good reason but I had no idea this was possible. New tricks!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Ruby: When to use protected</title>
      <dc:creator>Adrian Lee</dc:creator>
      <pubDate>Sun, 02 Oct 2022 12:12:12 +0000</pubDate>
      <link>https://forem.com/os6sense/ruby-when-to-use-protected-3773</link>
      <guid>https://forem.com/os6sense/ruby-when-to-use-protected-3773</guid>
      <description>&lt;p&gt;One of my pet peeves (I have a few) is finding the &lt;code&gt;protected&lt;/code&gt; keyword in the wild because 99% of the time (seriously) the developer does not understand its use.&lt;/p&gt;

&lt;p&gt;The use case is simple. You want an instance of a class to be able to access what would normally be private within another instance of the same class. For example, you are trying to apply an operation (e.g. multiply) on some item of state within the two classes; it has to be possible for one of the classes to reach into the other class and get a value for the operation to be applied. &lt;/p&gt;

&lt;p&gt;Using multiply as an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class A
  def initialize(value) = @value = value

  def *(other) = value * other.value

  protected

  attr_reader :value
end

a1 = A.new(3)
a2 = A.new(4)

puts a1 * a2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should be no surprise that this prints &lt;code&gt;12&lt;/code&gt; when run.&lt;/p&gt;

&lt;p&gt;If the value has been &lt;code&gt;private&lt;/code&gt; rather than &lt;code&gt;protected&lt;/code&gt; the output would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected.rb:5:in `*': private method `value' called for #&amp;lt;A:0x000000010520e1c0 @value=4&amp;gt; (NoMethodError)

  def *(other) = value * other.value
                              ^^^^^^
        from protected.rb:15:in `&amp;lt;main&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So protected has nothing to do with inheritance (as many people seem to think), everything to do with constraining access to state only to instances of the same class. &lt;/p&gt;

&lt;p&gt;I give the slightly more lengthy explanation there because the question does arise of why not to just use &lt;code&gt;public&lt;/code&gt;? The answer, to quote myself, encapsulation is vital in complex software systems. Everything that is public increases the surface area of a class and hence the cognitive overhead when working with the code. A classes public surface area should be as small as possible and &lt;code&gt;protected&lt;/code&gt; allows you to achieve this.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Ruby: Why to use private_constant (pt 1)</title>
      <dc:creator>Adrian Lee</dc:creator>
      <pubDate>Sat, 01 Oct 2022 22:29:16 +0000</pubDate>
      <link>https://forem.com/os6sense/ruby-why-to-use-privateconstant-pt-1-5a8j</link>
      <guid>https://forem.com/os6sense/ruby-why-to-use-privateconstant-pt-1-5a8j</guid>
      <description>&lt;p&gt;I don't want to muddy the waters by going over all the reasons to use private_constant (e.g. for inner classes, that's pt 2) since I want to focus on what we most commonly think of as constants in ruby i.e. &lt;code&gt;FOO = 1&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I'm too used to seeing classes that define constants at the top of the class (great!) but then leave me wondering .. is that constant being used from outside the class? In a small code base that isn't a problem; in a large code base it's &lt;strong&gt;essential&lt;/strong&gt; that a classes public interface is public for a reason, not by chance.&lt;/p&gt;

&lt;p&gt;Sometimes people do consider this and we end up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Foo
   def blahblah = FOO
...
  private
  FOO = 1
end
puts Foo:FOO
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;:shocked_pikachu_face: when people discover that the above quite happily prints &lt;code&gt;1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The proper way to declare a private constant is to use ... &lt;code&gt;private_constant&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Foo
  FOO = 1
  private_constant :FOO

  def blahblah = FOO
end
puts Foo::FOO
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have a private constant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ruby private_constants.rb
private_constants.rb:7:in `&amp;lt;main&amp;gt;': private constant Foo::FOO referenced (NameError)

puts Foo::FOO
        ^^^^^
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Encapsulation is &lt;strong&gt;vital&lt;/strong&gt; in complex software systems. Please watch your privates! &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Ruby: Why to use private attr_reader</title>
      <dc:creator>Adrian Lee</dc:creator>
      <pubDate>Sat, 01 Oct 2022 22:10:13 +0000</pubDate>
      <link>https://forem.com/os6sense/ruby-why-to-use-private-attrreader-4c0a</link>
      <guid>https://forem.com/os6sense/ruby-why-to-use-private-attrreader-4c0a</guid>
      <description>&lt;p&gt;I was just reminded of why I use &lt;code&gt;private att_reader&lt;/code&gt; in my code. &lt;/p&gt;

&lt;p&gt;I know the advice is to always use an interface but a point of discussion between myself and a colleague has been why not to use an instance variable, with the argument that it is much more readable. &lt;/p&gt;

&lt;p&gt;I have to agree with him in that with &lt;code&gt;@foo&lt;/code&gt; it's obvious that it's an instance variable being referred to. I agree it's better than plain old &lt;code&gt;foo&lt;/code&gt; in terms of readability. &lt;/p&gt;

&lt;p&gt;That is about where the benefit ends however and there is a &lt;strong&gt;huge&lt;/strong&gt; downside. Consider the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Foo
  def initialize(some_variable) = @some_variable = some_variable
  def some_op = @some_varible
end

class BetterFoo
  def initialize(some_variable) = @some_variable = some_variable

  def some_op = some_varible

  private attr_reader :some_variable
end

puts "----"
puts Foo.new("BAR").some_op
puts "----"
puts BetterFoo.new("BAR").some_op
puts "----"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The typo in the above is probably glaringly obvious since it's a small piece of code ... did you spot it? If not compare the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ruby some_variable.rb
----

----
some_variable.rb:10:in `some_op': undefined local variable or method `some_varible' for #&amp;lt;BetterFoo:0x00007fdba9d26090 @some_variable="BAR"&amp;gt; (NameError)

  def some_op = some_varible
                ^^^^^^^^^^^^
Did you mean?  some_variable
               @some_variable
    from some_variable.rb:18:in `&amp;lt;main&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you spot it now?&lt;/p&gt;

&lt;p&gt;IMO the use of a &lt;code&gt;private attr_reader&lt;/code&gt; so as to ensure the variable you are using has been initialised to a value other than nil overrides &lt;strong&gt;any&lt;/strong&gt; readability argument; let's not mention that it more easily allows the interface to be changed internally in the future should the need arise. The fact that it's private makes it an implementation detail but an implementation detail that will save you a lot of time.&lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
  </channel>
</rss>
