<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Ahmed Omran</title>
    <description>I am a software engineer and passionate educator. I have been building for the web for more than two decades.</description>
    <link>http://www.aomran.com/</link>
    <atom:link href="http://www.aomran.com/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Fri, 10 Jan 2025 15:46:34 +0000</pubDate>
    <lastBuildDate>Fri, 10 Jan 2025 15:46:34 +0000</lastBuildDate>
    <generator>Jekyll v4.3.2</generator>
    
      <item>
        <title>Compose Software Like Nature Would - RubyConf 2024</title>
        <description>&lt;p&gt;Many years ago I gave a talk that combined biology and software design to a local JavaScript meetup. It was an interesting concept so I proposed it as a talk for RubyConf 2024. Surprisingly it was accepted! &lt;a href=&quot;https://speakerdeck.com/this_ahmed/compose-software-like-nature-would-rubyconf-2024&quot; target=&quot;blank&quot;&gt;The slides can be seen here&lt;/a&gt;. The video can be seen &lt;a href=&quot;https://youtu.be/bVBAvCm2mCs?si=uafxsqlRWTwTgd7n&quot; target=&quot;blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;talk-summary&quot;&gt;Talk Summary&lt;/h3&gt;

&lt;p&gt;The only constant in software development is change. When our software cannot adapt, it turns into a big ball of mud. It becomes hard to reason about and hard to change. On the other hand, living organisms have the incredible ability to adapt to their environment. Over eons, they evolved innumerable adaptations. Let’s explore together how we can create adaptable, maintainable, and resilient software with some inspiration from nature.&lt;/p&gt;

&lt;p&gt;The conclusions of the talk are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Build small components (modules, classes)&lt;/li&gt;
  &lt;li&gt;Compose them together with messages (methods, events)&lt;/li&gt;
  &lt;li&gt;Have strong boundaries (limit public interfaces)&lt;/li&gt;
  &lt;li&gt;Use names, domain language and folder structure&lt;/li&gt;
  &lt;li&gt;Push side effects to the edges&lt;/li&gt;
  &lt;li&gt;Iterate and evolve your design over time&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Gary Bernhardt - &lt;a href=&quot;https://www.destroyallsoftware.com/talks/boundaries&quot; target=&quot;blank&quot;&gt;Boundaries&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Dan North - &lt;a href=&quot;https://www.youtube.com/watch?v=4Y0tOi7QWqM&quot; target=&quot;blank&quot;&gt;Software that Fits in Your Head&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Rich Hickey - &lt;a href=&quot;https://www.infoq.com/presentations/Simple-Made-Easy&quot; target=&quot;blank&quot;&gt;Simple Made Easy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Jessica Kerr - &lt;a href=&quot;https://www.youtube.com/watch?v=tq5SQ4W3gRI&quot; target=&quot;blank&quot;&gt;Functional Principles for OO Development&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 20 Nov 2024 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/compose-software-like-nature-would-rubyconf2024/</link>
        <guid isPermaLink="true">http://www.aomran.com/compose-software-like-nature-would-rubyconf2024/</guid>
        
        
      </item>
    
      <item>
        <title>People first</title>
        <description>&lt;p&gt;I have not written anything on this website in many years. I have had the pleasure and great privilege of being completely lost in my work on impactful projects over the past six years. The work has been challenging, and the people have been great. I write this as a reminder to myself that what makes a great team and organization is people.&lt;/p&gt;

&lt;p&gt;Manage with empathy and put your people first. As an individual contributor, make yourself indispensable to your team. Teams can do extraordinary things when they are empowered and supported.&lt;/p&gt;

&lt;h3 id=&quot;manage-with-people-first-in-mind&quot;&gt;Manage with people first in mind&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;People want “deep work” –  meaningful problems and meaningful decisions.&lt;/li&gt;
  &lt;li&gt;People want to be treated with respect –  flexible schedules and a lot of autonomy.&lt;/li&gt;
  &lt;li&gt;People want their accomplishments acknowledged – fair compensation and a clear career path.&lt;/li&gt;
  &lt;li&gt;People want to grow –  managers and mentors invested in their future.&lt;/li&gt;
  &lt;li&gt;People want time for themselves and their families –  sensible hours and long-term planning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Leaders have a big responsibility to focus on the needs of others before their own. The trust that you build with a people-first approach will buy you a lot of goodwill, and you can then, in turn, get more from your team.&lt;/p&gt;

&lt;h3 id=&quot;making-yourself-indispensable-to-your-team&quot;&gt;Making yourself indispensable to your team&lt;/h3&gt;

&lt;p&gt;As an individual contributor (non-manager role), there are skills that software engineers need, no matter the context. These skills can make you indispensable to your team and help you grow into a leadership role.&lt;/p&gt;

&lt;h4 id=&quot;presence&quot;&gt;Presence&lt;/h4&gt;

&lt;p&gt;Presence isn’t just clocking in. It means being there for others and benefiting from your colleagues being there for you. An indispensable team member:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;can be easily reached during regular business hours for their time zone unless the time is blocked off on their calendar.&lt;/li&gt;
  &lt;li&gt;shows up to all meetings on time and ready to engage.&lt;/li&gt;
  &lt;li&gt;responds to all code review requests within 24 business hours.
    &lt;ul&gt;
      &lt;li&gt;asks questions when they don’t understand the code changes.&lt;/li&gt;
      &lt;li&gt;offers advice, suggestions and encouragement in code reviews.&lt;/li&gt;
      &lt;li&gt;When someone reviews their code, engages with the reviewer and addresses their concerns.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;when they get stuck on a problem, they ask questions and get support from their colleagues.&lt;/li&gt;
  &lt;li&gt;actively uses the company’s communication channels (e.g. Slack) to help their colleagues, give their opinions, ask questions, and answer questions.&lt;/li&gt;
  &lt;li&gt;monitors production environment logs and alerts for bugs and performance issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;ownership&quot;&gt;Ownership&lt;/h4&gt;

&lt;p&gt;Great teams are full of leaders – people who know what they’re working on and why they’re working on it and can get projects to the finish line. They&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;understand the real-world problem they are trying to solve. If they don’t know why they are doing something, they actively engage with their manager, and colleagues to understand better.&lt;/li&gt;
  &lt;li&gt;break down the project they are working on into smaller milestones and communicate their progress.&lt;/li&gt;
  &lt;li&gt;communicate risks and help reduce the scope of a project if the timeline is unrealistic.&lt;/li&gt;
  &lt;li&gt;remove roadblocks, get unstuck, help people make decisions, and push projects to the finish line.&lt;/li&gt;
  &lt;li&gt;think about the state of their codebase and actively work to improve it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;software-design&quot;&gt;Software Design&lt;/h4&gt;

&lt;p&gt;The first step is to find a solution; often, the first solution is suboptimal. Iteratively, the solution is then refined. A talented and experienced engineer knows when to stop. They know that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;not all code is created equal. I often ask myself: How do I make my code easier to read? How do I make it easier to change in the future?&lt;/li&gt;
  &lt;li&gt;conversely, there is no perfect code. They have the wisdom to ship the working code and improve it later if necessary.&lt;/li&gt;
  &lt;li&gt;They write automated tests and ask others to do the same.&lt;/li&gt;
  &lt;li&gt;They evaluate trade-offs (refactor or not? their code or a third-party solution?) and understand why they made a decision.&lt;/li&gt;
  &lt;li&gt;They evaluate the software design of the entire codebase and help their colleagues make good decisions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;delivering-solutions&quot;&gt;Delivering solutions&lt;/h4&gt;

&lt;p&gt;Beautiful code is only worthwhile if it ships or solves a real problem.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Sometimes, the solution involves writing no code.&lt;/li&gt;
  &lt;li&gt;Sometimes, the solution does not need to solve all use cases (80/20 rule).&lt;/li&gt;
  &lt;li&gt;Solutions should be broken down into small changes that are easy to understand, easy to review and less risky to merge.&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 30 Oct 2023 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/people-first/</link>
        <guid isPermaLink="true">http://www.aomran.com/people-first/</guid>
        
        
      </item>
    
      <item>
        <title>Rails View Components</title>
        <description>&lt;p&gt;Ruby on Rails is a framework that delivers a tremendous amount of developer productivity and happiness. Unsurprisingly, Rails application also go through growing pains as they mature. Models and controllers expand until small objects are extracted to keep them under control. The same happens with Rails views; they start out powerful and easy to use and slowly grow out of control. The views become hard to reason about and maintain. Views are also inherently hard to test, so they become the riskiest part of a Rails application.&lt;/p&gt;

&lt;h3 id=&quot;decorator&quot;&gt;Decorator&lt;/h3&gt;

&lt;p&gt;Decorators are added to contain the situation. Gems exist to help with this, but we don’t need them. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SimpleDelegator&lt;/code&gt; is built into Ruby so we can make use of it like so:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommentDecorator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SimpleDelegator&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gravatar&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;timestamp&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;author_name&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;CommentDecorator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Decorators are great, and they help for a while. The problem is that they are tightly coupled to models. Models tend to describe big ideas in the system that are displayed in many different ways. Decorators, therefore, get bloated as they start describing numerous UIs.&lt;/p&gt;

&lt;h3 id=&quot;view-component&quot;&gt;View Component&lt;/h3&gt;

&lt;p&gt;Applying &lt;a href=&quot;/object-oriented-programming-is-not-about-objects&quot;&gt;basic OOP principles&lt;/a&gt; we can break a decorator into smaller objects. Instead of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommentDecorator&lt;/code&gt; we can build several components: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommentForm&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommentBox&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommentThread&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReactionButtons&lt;/code&gt;, etc. We don’t need a fancy gem to do this. A plain old ruby object will do:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ViewComponent&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveModel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Model&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:context&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;ss&quot;&gt;partial: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;components/&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;template_path&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;ss&quot;&gt;locals: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;component: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;kp&quot;&gt;private&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;template_path&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;underscore&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The view component objects can be put under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/view_components&lt;/code&gt; and their templates can be placed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/views/components/&lt;/code&gt;. The template is just a rails partial that gets a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;component&lt;/code&gt; local variable.&lt;/p&gt;

&lt;p&gt;The view components will each inherit from the main &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ViewComponent&lt;/code&gt; class:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# object at app/view_components/comment_box.rb&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# template at app/views/components/_comment_box.html.erb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommentBox&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ViewComponent&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# object at app/view_components/comment_thread.rb&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# template at app/views/components/_comment_thread.html.erb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommentThread&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ViewComponent&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# object at app/view_components/comment_form.rb&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# template at app/views/components/_comment_form.html.erb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommentForm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ViewComponent&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We’ll add a helper to facilitate the rendering of a view component:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;render_component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;context: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;render&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now anywhere in our views we can render a component with:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erb&quot; data-lang=&quot;erb&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;render_component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;CommentForm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;some: &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;property&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;View components are ideally minimal as they have a single and focused responsibility. This makes them easy to test and reason about. We can use a real rendering context if we wanted to in tests or mock it and do isolation testing.&lt;/p&gt;

&lt;h3 id=&quot;interactivity&quot;&gt;Interactivity&lt;/h3&gt;

&lt;p&gt;View components implemented in Ruby are a good solution until interactivity is required. If for example, our user interfaces need to respond to mouse events then the Ruby classes become a dead-end. Vue.js or Backbone.js are excellent JavaScript libraries for adding interactivity on-top of server-rendered views. To pass properties from the ruby side to the JavaScript side, we just have to make sure they exist in the HTML data attribute:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erb&quot; data-lang=&quot;erb&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;id: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;element-id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;data: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;name: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Ahmed&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_json&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Component template here!&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;element-id&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;props&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;component&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;el&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;#element-id&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;props&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This is, in my opinion, a great solution for applications that just need a sprinkle of interactivity. Of course a component that is spread over four languages (Ruby, JavaScript, HTML, and CSS) becomes harder to test and contribute to. For new projects I recommend switching entirely to front-end components. Backbone.js, React, or Vue.js can be used to build both the template and behavior of complex UI components. This is a topic I want to explore more in the future on this blog.&lt;/p&gt;
</description>
        <pubDate>Sun, 15 Oct 2017 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/rails-view-components/</link>
        <guid isPermaLink="true">http://www.aomran.com/rails-view-components/</guid>
        
        
      </item>
    
      <item>
        <title>Compose Software Like Nature Would</title>
        <description>&lt;p&gt;Inspired by biology background, I combine some ideas from functional and OO programming into a presentation about software design.  I gave this talk at the OttawaJS meet-up group in September. &lt;a href=&quot;https://speakerdeck.com/this_ahmed/compose-software-like-nature-would&quot; target=&quot;blank&quot;&gt;The slides can be seen here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;talk-summary&quot;&gt;Talk Summary&lt;/h3&gt;

&lt;p&gt;The only constant in software development is change. When our software cannot adapt, it turns into a big ball of mud. It becomes hard to hard to reason about and hard to change.&lt;/p&gt;

&lt;p&gt;Living organisms, on the other hand, have the incredible ability to adapt to their environment. Over 3.5 billion years they evolved many different adaptations. For example, there are 400k species of beetles, and they live in diverse habitats. Some beetles feed on decayed matter while others prefer locally sourced, organic vegetation.  Perhaps nature could be our inspiration for better software.&lt;/p&gt;

&lt;p&gt;The conclusions of the talk are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;/object-oriented-programming-is-not-about-objects&quot;&gt;Build small components &amp;amp; compose them with messages.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/pure-functional-programming&quot;&gt;Push side-effects to boundaries and have a functional core.&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Gary Bernhardt - &lt;a href=&quot;https://www.destroyallsoftware.com/talks/boundaries&quot; target=&quot;blank&quot;&gt;Boundaries&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Dan North - &lt;a href=&quot;https://www.youtube.com/watch?v=4Y0tOi7QWqM&quot; target=&quot;blank&quot;&gt;Software that Fits in Your Head&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Rich Hickey - &lt;a href=&quot;https://www.infoq.com/presentations/Simple-Made-Easy&quot; target=&quot;blank&quot;&gt;Simple Made Easy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Jessica Kerr - &lt;a href=&quot;https://www.youtube.com/watch?v=tq5SQ4W3gRI&quot; target=&quot;blank&quot;&gt;Functional Principles for OO Development&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 04 Oct 2017 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/compose-software-like-nature-would/</link>
        <guid isPermaLink="true">http://www.aomran.com/compose-software-like-nature-would/</guid>
        
        
      </item>
    
      <item>
        <title>Pure Functional Programming</title>
        <description>&lt;p&gt;Functional programming has a long history, with the first programming languages that use functions made in the 1950s. There has been a recent resurgence of the functional programming paradigm, so I think it’s important to be familiar with it.&lt;/p&gt;

&lt;p&gt;There are a lot of concepts in functional programming – for example, first class functions, immutable types, lambda calculus, recursion, closure, currying, and monads – so it can be confusing. But at its core the functional paradigm is about one thing:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Pure Functional Programming is simply about avoiding side-effects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;side-effects&quot;&gt;Side-effects&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;When a program modifies state:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// starting state&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;incrementor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Above is a function that causes a “side-effect” by changing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; variable outside of its scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When a program mutates data:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;sorter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The sort method mutates the array, so our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,6,4,2]&lt;/code&gt; state is gone forever. The “side-effect” here is that we’ve broken any other program that relied on the order in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When a program depends on anything other than its arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The examples above have an additional problem. The functions depend on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;array&lt;/code&gt; which are not passed in as arguments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When a program interacts with I/O:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myLogger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Log:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;A program that raises an error, writes data to disk, or prints something on the screen is causing a “side-effect.”&lt;/p&gt;

&lt;h3 id=&quot;problems-with-side-effects&quot;&gt;Problems with side-effects&lt;/h3&gt;

&lt;p&gt;A program that has side-effects is hard to reason about because it gives us no guarantees about what is going to happen. When a program depends on things other than its arguments it is relying on hidden inputs that might not be available at all. We can’t safely predict what these programs will do or even reuse them elsewhere because they are tightly coupled to their context.&lt;/p&gt;

&lt;p&gt;These programs are also hard to test in isolation without excessive mocking and stubbing.&lt;/p&gt;

&lt;h3 id=&quot;avoiding-side-effects-pure-functions&quot;&gt;Avoiding side-effects: pure functions&lt;/h3&gt;

&lt;p&gt;Pure functions depend only on their arguments and return a value without causing side-effects. These functions will always return the same output given a specific input. For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;incrementor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myLogger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Log: &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This second function is not interacting with I/O (console) anymore. This pure function transforms data into a form that we can later send to the console. When testing this code, we don’t have to mock or assert against &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;console.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Pure functions are easy to understand, test, change, compose, and reuse.&lt;/p&gt;

&lt;h3 id=&quot;avoiding-side-effects-immutable-data&quot;&gt;Avoiding side-effects: immutable data&lt;/h3&gt;

&lt;p&gt;We’re safe inside of pure functions because we know our inputs and outputs. But what if the inputs are mutable (i.e., something outside of the function can change them)?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getFirst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;getFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 3&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// somewhere else, something mutated the array&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;getFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Our function is working fine, but it’s giving us an unexpected output. JavaScript arrays and objects are mutable, so we have to be very careful. Languages like Clojure and Elixir have immutable data structures, so this problem doesn’t happen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Freeze Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For primitive types like strings and numbers, we can rely on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; because the values are immutable. Reassigning the variable name will raise an error.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Ahmed&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;myName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Paula&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// TypeError: Assignment to constant variable.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For arrays and objects we can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.freeze()&lt;/code&gt;. Going back to the example above:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;freeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getFirst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;getFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 3&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// TypeError: Cannot assign to read only property &apos;1&apos; ...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;getFirst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This technique doesn’t work for nested data types; for those we have to freeze each level recursively. &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze&quot; target=&quot;blank&quot;&gt;MDN has an example of doing this&lt;/a&gt; or you can use the &lt;a href=&quot;https://www.npmjs.com/package/deep-freeze&quot; target=&quot;blank&quot;&gt;deep-freeze&lt;/a&gt; npm package which does the same thing.&lt;/p&gt;

&lt;p&gt;We can’t change immutable data types, so we have to make a copy. Copying does come with a performance penalty, especially if we’re transforming a lot of data, so we have to be cognizant of that.&lt;/p&gt;

&lt;p&gt;In ES2015 we can copy arrays with the spread operator, like so: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;newArr = [ ...prevArr ]&lt;/code&gt; and in ES2009 we can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;newArr = prevArr.slice()&lt;/code&gt;. For objects we can use the spread operator in a similar way &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextObj = { ...prevObj }&lt;/code&gt; or with ES2009 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextObj = Object.assign({}, prevObj)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that the above examples only do a shallow copy. Another &lt;a href=&quot;https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Deep_Clone&quot; target=&quot;blank&quot;&gt;example from MDN&lt;/a&gt; gives us a solution for deep clone that works for both objects and arrays: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextObj = JSON.parse(JSON.stringify(prevObj))&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Languages like Clojure and Elixir that have immutable data structures take care of this for you and do it in a very efficient manner. The Wikipedia page about &lt;a href=&quot;https://en.wikipedia.org/wiki/Persistent_data_structure&quot; target=&quot;blank&quot;&gt;persistent data structures&lt;/a&gt; is a good read.&lt;/p&gt;

&lt;h3 id=&quot;dealing-with-side-effects&quot;&gt;Dealing with side-effects&lt;/h3&gt;

&lt;p&gt;Functional programming is ideal for computations. The pure functions always apply the same algorithm to inputs and will always give a predictable output. The goal is to push side-effects (persisting state, writing to disk, printing on a screen, making HTTP requests, raising errors, etc.) to the edges of our program.&lt;/p&gt;

&lt;p&gt;One potential way of dealing with side-effects is to treat them as streams. The “outside world” is a stream you can read from or write to. Pure functions are composed and then finally at the edges of our program we connect to the outside “streams.”&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;Input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function_3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Output&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Everything inside the parentheses is pure functional programming. &lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&amp;gt;&lt;/code&gt; is a pipe operator that unfortunately JavaScript doesn’t have. But there’s a &lt;a href=&quot;https://github.com/tc39/proposal-pipeline-operator&quot; target=&quot;blank&quot;&gt;proposal&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gist.github.com/staltz/868e7e9bc2a7b8c1f754&quot; target=&quot;blank&quot;&gt;Reactive programming&lt;/a&gt; might be useful for dealing with asynchronous data streams. In the reactive paradigm, user interactions with a UI are an event stream.&lt;/p&gt;

&lt;p&gt;A pragmatic approach might be to “eject” from the pure functional paradigm and go into Object Oriented Programming at the edge of the program. OOP is complementary to the functional paradigm because it can be used to rationally model the side-effects of our programs and hide their implementation details.&lt;/p&gt;
</description>
        <pubDate>Fri, 18 Aug 2017 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/pure-functional-programming/</link>
        <guid isPermaLink="true">http://www.aomran.com/pure-functional-programming/</guid>
        
        
      </item>
    
      <item>
        <title>Object Oriented Programming Is Not About Objects</title>
        <description>&lt;p&gt;When I was introduced to Object-Oriented Programming (OOP), the focus was entirely on modeling our world using classes and hierarchies. Inheritance was taught immediately as a main concept. The problem with this is that Alan Kay coined the term OOP, and what we’re doing and teaching has nothing to do with what he envisioned.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages… OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. – &lt;a href=&quot;http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en&quot; target=&quot;blank&quot;&gt;Alan Kay&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;OOP is not about classes, inheritance or even objects per se. The important part is messages.&lt;/p&gt;

&lt;h3 id=&quot;the-metaphor&quot;&gt;The metaphor&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Biological cells&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Living organisms are extremely complex yet built from tiny, relatively simple, building blocks. Trillions of cells make up a human being. Even smaller components make up these cells.&lt;/p&gt;

&lt;p&gt;Cells have a half-life, meaning that they will eventually decay and die. Blood cells will live for about 100 days and then die. Other blood cells are created to take their place.&lt;/p&gt;

&lt;p&gt;Cells have walls that isolate them from their environment; they communicate with each other through tiny channels.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Computer networks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like a living organism, the internet is a collection of billions of computers. These computers will die off or disconnect, and the internet doesn’t even notice. Computers are isolated from each other and can only communicate with each other through a narrow protocol. TCP/IP, the protocol that connects all of these computers together, is ~10k lines of code yet lives forever and scales well.&lt;/p&gt;

&lt;h3 id=&quot;takeaway-1-build-small-components&quot;&gt;Takeaway 1: Build Small Components&lt;/h3&gt;

&lt;p&gt;If we’re trying to model a problem space then making a &lt;strong&gt;person&lt;/strong&gt; class makes no sense. A &lt;strong&gt;person&lt;/strong&gt; is a collection of many &lt;strong&gt;components&lt;/strong&gt; and they have many different &lt;strong&gt;roles&lt;/strong&gt;. Classes will get you in trouble. Adding inheritance and hierarchies on top of classes is just trying to fit the world into a model you invented. Chances are you will get the classification wrong and will have to redo it or desperately patch it.&lt;/p&gt;

&lt;p&gt;Taking our cue from biology and networks, we should build small components. The components can be &lt;em&gt;objects&lt;/em&gt;, but they can also be &lt;em&gt;processes&lt;/em&gt; (e.g. Elixir) or &lt;em&gt;modules&lt;/em&gt; and &lt;em&gt;functions&lt;/em&gt;. Individual components should do only one thing.&lt;/p&gt;

&lt;h3 id=&quot;takeaway-2-compose-components-using-messages&quot;&gt;Takeaway 2: Compose Components Using Messages&lt;/h3&gt;

&lt;p&gt;Components should be isolated from each other. They shouldn’t know anything about the internals of others. Components should expose a small public interface to the world to say “here’s what I can do for you.” For example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;construct_graph&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;DataFetch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;LineGraph&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;highlight language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;DataFetch&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;highlight language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;LineGraph&lt;/span&gt;&lt;/code&gt; respond to just one message, and we don’t know anything else about them. They each do one thing.&lt;/p&gt;

&lt;p&gt;If we have to clean the data, then we can pass it through &lt;code class=&quot;highlight language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;DataClean&lt;/span&gt;&lt;/code&gt; first.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;construct_graph&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;DataFetch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;DataClean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;LineGraph&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;em&gt;Note: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&amp;gt;&lt;/code&gt; is a pipe operator – we’re just moving data to the right.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s say that pie graphs would show this data better – then we can switch one component with &lt;code class=&quot;highlight language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;PieGraph&lt;/span&gt;&lt;/code&gt;. If we need to render this information in another way, then we can change that last part as long as it responds to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;draw&lt;/code&gt; message.&lt;/p&gt;

&lt;h3 id=&quot;dependency-injection&quot;&gt;Dependency Injection&lt;/h3&gt;

&lt;p&gt;One mistake that we made above is using explicit dependencies. In the example above, our code relies on knowing &lt;code class=&quot;highlight language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;DataFetch&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;highlight language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;DataClean&lt;/span&gt;&lt;/code&gt;, and &lt;code class=&quot;highlight language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;no&quot;&gt;LineGraph&lt;/span&gt;&lt;/code&gt; to do its job. We can instead “inject” these dependencies:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;construct_graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_fetcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_cleaner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grapher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data_fetcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_cleaner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grapher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now this component composes three parts without explicitly knowing about them. We can reuse this to build several different types of graphs or representations.&lt;/p&gt;

&lt;h3 id=&quot;pub-sub&quot;&gt;Pub-Sub&lt;/h3&gt;

&lt;p&gt;For more complex composition pub-sub might be useful. For example, we are selling books online, and after payment goes through we need to do several things. We need to send an order confirmation email, start preparing the book file, sign up the user to the book’s forums, etc.&lt;/p&gt;

&lt;p&gt;All of these operations can subscribe to the order component. After payment goes through, the order component will publish/notify all subscribers. All of the subscribers will respond appropriately and do their unique operation.&lt;/p&gt;

&lt;p&gt;Avdi Grimm has an example of using the &lt;a href=&quot;https://www.rubytapas.com/2017/04/11/skinny-controller-domain-model-events/&quot; target=&quot;blank&quot;&gt;observer pattern in a Ruby on Rails application&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;half-life-and-testing&quot;&gt;Half-life and Testing&lt;/h3&gt;

&lt;p&gt;Like biological cells, we can replace these small components of code without breaking the system. We can more easily reason about them, and so we can refactor with confidence.&lt;/p&gt;

&lt;p&gt;Components that have few dependencies and a small interface are easy to test. We can easily decouple them from slow operations like the database or HTTP and write fast tests that focus on their behavior.&lt;/p&gt;

&lt;p&gt;Bear in mind that while the isolation tests check that each component works, we need to do end-to-end testing to prove that it all works together.&lt;/p&gt;

&lt;h3 id=&quot;inheritance-is-not-evil&quot;&gt;Inheritance is not evil&lt;/h3&gt;

&lt;p&gt;Inheritance is sometimes useful, but we have to be careful how we use it. Prefer composition unless you’re sure you have a good use case for hierarchies. When you use inheritance remember to keep it shallow (don’t subclass a subclass). Subclasses should inherit everything from their parent.&lt;/p&gt;

&lt;p&gt;Object-Oriented Programming is essentially one simple idea: &lt;strong&gt;build small things and compose them with messages&lt;/strong&gt;. We can do this in any programming language. There is no reason to complicate matters.&lt;/p&gt;
</description>
        <pubDate>Mon, 07 Aug 2017 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/object-oriented-programming-is-not-about-objects/</link>
        <guid isPermaLink="true">http://www.aomran.com/object-oriented-programming-is-not-about-objects/</guid>
        
        
      </item>
    
      <item>
        <title>React.js: The Hard Parts</title>
        <description>&lt;p&gt;Getting started with React is easy. You don’t need anything fancy – just use &lt;a href=&quot;http://codepen.io/gaearon/pen/ZpvBNJ?editors=0010&quot; target=&quot;blank&quot;&gt;codepen.io&lt;/a&gt; and off you go. React doesn’t need to be part of a single page app, you can progressively add it to existing apps. The API for React is mostly just JavaScript concepts. The documentation is excellent, and there are a lot of helpful free resources available. JSX can be awkward at first, but it’s just HTML for the most part, so it’s easy to pick up.&lt;/p&gt;

&lt;p&gt;Most of the concepts in React are manageable. The hardest part, in my opinion, is state management.&lt;/p&gt;

&lt;h3 id=&quot;what-is-state&quot;&gt;What is “state”?&lt;/h3&gt;

&lt;p&gt;Program state has a long history in computer science, but essentially it’s just the “condition” that our program is in. If we’re rendering a tweet, then its state will be things like contents, the number of likes, the number of retweets, whether it’s expanded or collapsed, etc.&lt;/p&gt;

&lt;p&gt;In traditional server-rendered apps we don’t have to worry about state changes. If a user interacts with your application or refreshes the page, then we’ll look up state at that moment and rebuild the page.&lt;/p&gt;

&lt;p&gt;In dynamic apps, like ones built using React, state change has to be dealt with immediately. If a user clicks “retweet” then the rendered tweet needs to change instantaneously.&lt;/p&gt;

&lt;p&gt;If your app is very dynamic (hence React) then you’ll be juggling a lot of states. To complicate things even further, what if someone from a different computer clicks “retweet” – how do we update state for everyone looking at that tweet? Moreover, some states are user-specific and possibly not saved at all, for example, whether navigation on the page is fully open or collapsed.&lt;/p&gt;

&lt;p&gt;Facebook suggested a pattern called “flux” that can be very useful for managing a UI’s state. There are three main points:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;single source of truth&lt;/li&gt;
  &lt;li&gt;top-down data flow&lt;/li&gt;
  &lt;li&gt;state is changed through actions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There’s a library called &lt;a href=&quot;http://redux.js.org/&quot; target=&quot;blank&quot;&gt;Redux&lt;/a&gt; that implements this pattern. You &lt;a href=&quot;https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367&quot; target=&quot;blank&quot;&gt;might not need Redux&lt;/a&gt; though, especially for smaller applications. The following describes one way to manage state without using Redux.&lt;/p&gt;

&lt;h3 id=&quot;single-source-of-truth&quot;&gt;Single source of truth&lt;/h3&gt;

&lt;p&gt;Managing state can get complicated if each component has some state. The users will start to experience inconsistencies. For example, in an email client, the header shows that you have two unread messages while the inbox itself is empty. One solution to this problem is to have a single place where we store state. This “store” is the single “source of truth” in the application.&lt;/p&gt;

&lt;p&gt;Keeping state centralized is a concept that we have embraced on the server-side by using one database so doing the same for the client-side seems reasonable. As a compromise, especially in the prototyping stage, we can use “container components” instead of a single store.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/aomran/3ce614a1a7e812bcd4ad32ef836e24d8.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Container components handle state for many child components.&lt;/p&gt;

&lt;h3 id=&quot;top-down-data-flow&quot;&gt;Top-down data flow&lt;/h3&gt;

&lt;p&gt;The child components are stateless functions and get all of their data as properties from the container (parent) component. These are simply visual components.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/aomran/9dbac71f8d076962ce66c36358c1a3ed.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;The above is called a stateless functional component. These functions don’t have state or life-cycle methods. React encourages their usage because they’re easy to test, compose and reuse.&lt;/p&gt;

&lt;h3 id=&quot;state-is-changed-through-actions&quot;&gt;State is changed through actions&lt;/h3&gt;

&lt;p&gt;Functions are passed down to the child components to handle actions. Interaction with the components, like a mouse click, trigger these functions to update state. For example to implement retweets:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/aomran/1092cd01518308a117377bd04c669606.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;When the state is updated, the props will change too and render the child components again.&lt;/p&gt;

&lt;h3 id=&quot;in-summary&quot;&gt;In summary&lt;/h3&gt;

&lt;p&gt;Your application might be composed of multiple container components. As the app gets more complex, with this approach, it will be easier to graduate to a solution like Redux.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;properties flow down; actions flow up&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/single-source-of-truth.png&quot; alt=&quot;&apos;React: single source of truth&apos;&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 22 Jul 2017 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/react-the-hard-parts/</link>
        <guid isPermaLink="true">http://www.aomran.com/react-the-hard-parts/</guid>
        
        
      </item>
    
      <item>
        <title>Front-end Web Development in 2017</title>
        <description>&lt;p&gt;Web development appears to have a furious rate of change so keeping up can be overwhelming. In reality, though, stable changes don’t happen that often. To put things into perspective: the hottest JavaScript framework right now, React.js, was created four years ago. jQuery is ten years old and is still on 70% of the top 10k websites. One sane approach is to avoid the hype train and stay on firm ground. Sometimes it’s a good idea to watch the framework jostling from a distance. That said, front-end web development has reached an unprecedented level of maturity, and there’s a lot of warranted excitement.&lt;/p&gt;

&lt;h3 id=&quot;mobile-first&quot;&gt;Mobile First&lt;/h3&gt;

&lt;p&gt;jQuery was created in 2006 to address cross-browser incompatibilities. For example, Internet Explorer has been a huge issue for a long time because of its lack of support for many critical JavaScript and CSS features. Microsoft has &lt;a href=&quot;https://www.microsoft.com/en-us/WindowsForBusiness/End-of-IE-support&quot; target=&quot;blank&quot;&gt;dropped support for I.E. 8, 9 and 10&lt;/a&gt;. Today these old browsers, combined, have less than &lt;a href=&quot;http://caniuse.com/usage-table&quot; target=&quot;blank&quot;&gt;1% of the browser market share&lt;/a&gt;. All of the top JavaScript frameworks (React, Angular, Ember) still support I.E. 9. 1% of two billion active Facebook users is a lot. For the rest of us, it’s safe to drop support and not look back.&lt;/p&gt;

&lt;p&gt;More interestingly, &lt;a href=&quot;http://gs.statcounter.com/platform-market-share/desktop-mobile-tablet&quot; target=&quot;blank&quot;&gt;mobile has overtaken desktops&lt;/a&gt; in browser share. Google is doing &lt;a href=&quot;https://www.forbes.com/sites/jaysondemers/2017/01/26/what-googles-mobile-first-index-means-for-your-seo-strategy/#5c8378e54f24&quot; target=&quot;blank&quot;&gt;mobile first indexing&lt;/a&gt;. Ethan Marcotte’s article about &lt;a href=&quot;https://alistapart.com/article/responsive-web-design&quot; target=&quot;blank&quot;&gt;responsive web design&lt;/a&gt; from 2010 is still very relevant.&lt;/p&gt;

&lt;p&gt;The internet is fragmented. We are always going to have to build our websites and apps for different platforms and browsers. Integrating manual and automated &lt;a href=&quot;https://saucelabs.com/open-source&quot; target=&quot;blank&quot;&gt;browser testing&lt;/a&gt; into our process is an excellent idea. It’s also prudent to use &lt;a href=&quot;http://caniuse.com/&quot; target=&quot;blank&quot;&gt;caniuse.com&lt;/a&gt; to find out what we can safely use across browsers.&lt;/p&gt;

&lt;h3 id=&quot;javascript-like-its-2015&quot;&gt;JavaScript Like It’s 2015&lt;/h3&gt;

&lt;p&gt;JavaScript is the &lt;a href=&quot;https://insights.stackoverflow.com/survey/2017#technology&quot; target=&quot;blank&quot;&gt;most commonly used programming language&lt;/a&gt;. I firmly believe that learning it is the best thing a web developer can do for their career. I highly recommend the book &lt;a href=&quot;https://github.com/getify/You-Dont-Know-JS&quot; target=&quot;blank&quot;&gt;You Don’t Know JS&lt;/a&gt; for learning the language. The language itself is an implementation of a standardized specification called ECMAScript (ES). Nearly all of web browsers (~98%) have full support for 2009’s ECMAScript specification (ES5).&lt;/p&gt;

&lt;p&gt;ECMAScript 2015 has a lot of great features, but the browser support is patchy. Not to worry though because you can write in ES2015 and then use &lt;a href=&quot;https://babeljs.io/&quot; target=&quot;blank&quot;&gt;Babel.js&lt;/a&gt; to compile the code to ES5. Learning to use these new features is easy thanks to the &lt;a href=&quot;https://babeljs.io/learn-es2015/&quot; target=&quot;blank&quot;&gt;Babel.js guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;JavaScript runs outside of the browser using &lt;a href=&quot;https://nodejs.org&quot; target=&quot;blank&quot;&gt;Node.js&lt;/a&gt;. Node is extremely popular and its package ecosystem, &lt;a href=&quot;https://www.npmjs.com/&quot; target=&quot;blank&quot;&gt;npm&lt;/a&gt;, claims to be the “largest ecosystem of open source libraries in the world.” There are many versions of Node, and they have partial support for features from ES2015, ES2016, ES2017, etc.&lt;/p&gt;

&lt;p&gt;Writing ES5 is acceptable, but ideally, we write in ES2015 and let Babel.js take care of the rest. Airbnb has an excellent JavaScript &lt;a href=&quot;https://github.com/airbnb/javascript&quot; target=&quot;blank&quot;&gt;style guide&lt;/a&gt; that can help you write good code. I recommend running your JS code through a linter like &lt;a href=&quot;http://eslint.org/&quot; target=&quot;blank&quot;&gt;ESLint&lt;/a&gt; to catch common errors. &lt;a href=&quot;http://flowtype.org/&quot; target=&quot;blank&quot;&gt;Flow&lt;/a&gt; is another useful tool; it’ll type-check your JS code to find bugs early on.&lt;/p&gt;

&lt;h3 id=&quot;components&quot;&gt;Components&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages. – &lt;a href=&quot;http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en&quot; target=&quot;blank&quot;&gt;Alan Kay&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Writing software is relatively easy, maintaining it is hard. That is why most programmers have converged on the idea of writing small components (objects, functions, etc.) and carefully composing them. The complexity of the software decreases because each component “can fit in your head,” and you can reason about it. These loosely coupled components can be replaced more quickly, with more confidence, and with fewer side-effects.&lt;/p&gt;

&lt;p&gt;The front-end JavaScript frameworks have all embraced the same, decades-old, idea. You write components for UI elements and then loosely combine them together to build an application.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. – &lt;a href=&quot;https://facebook.github.io/react/docs/components-and-props.html&quot; target=&quot;blank&quot;&gt;React.js&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;everybody-loves-react&quot;&gt;Everybody loves React&lt;/h3&gt;

&lt;p&gt;Today, Angular is the JavaScript framework that has the biggest adoption, although React.js has the greatest momentum. &lt;a href=&quot;http://stateofjs.com&quot; target=&quot;blank&quot;&gt;Developer surveys&lt;/a&gt; in 2017 show that React.js is the highest rated library as developers seem to enjoy using it. If you are interested in learning a library/framework this year, then I highly recommend React, especially because the &lt;a href=&quot;http://www.ryan-williams.net/hacker-news-hiring-trends/2017/june.html?compare1=AngularJS&amp;amp;compare2=React&amp;amp;compare3=Ember&amp;amp;compare4=&quot; target=&quot;blank&quot;&gt;hiring trends&lt;/a&gt; are strongly in its favor.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://facebook.github.io/react/&quot; target=&quot;blank&quot;&gt;React.js&lt;/a&gt; leverages ES2015, components and powerful build-tools to help you develop dynamic web applications. It is conceptually simple. You create components, and then when there are state changes, React will redraw the component. This is in contrast to the more bug-prone manual changing of specific elements on the page that we had to do with jQuery.&lt;/p&gt;

&lt;p&gt;If you want to learn React then I highly recommend doing the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Learn JavaScript well.&lt;/li&gt;
  &lt;li&gt;Learn ES2015, especially arrow functions, classes, and modules.&lt;/li&gt;
  &lt;li&gt;Build simple components with React.&lt;/li&gt;
  &lt;li&gt;Make a full app with React.&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Mon, 10 Jul 2017 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/front-end-web-development-in-2017/</link>
        <guid isPermaLink="true">http://www.aomran.com/front-end-web-development-in-2017/</guid>
        
        
      </item>
    
      <item>
        <title>Long Hiatus</title>
        <description>&lt;p&gt;I haven’t posted on this website in a few years. The difficulties I have experienced in getting going again have made me appreciate even more the people who work full-time and still manage to contribute to the software community through blog posts, videos, and open source code. I want to write more, and actually publish. To get things started off, I would like to share some of the interesting experiences from the last few years as a full-stack software developer.&lt;/p&gt;

&lt;h4 id=&quot;open-source&quot;&gt;Open Source&lt;/h4&gt;

&lt;p&gt;My main open source contribution over the past few years has been an &lt;a href=&quot;https://www.npmjs.com/package/ember-cli-chart&quot;&gt;Ember.js component&lt;/a&gt;. It’s a small project but has been a great learning experience. Many people download this addon every day, so it keeps me motivated to keep it updated. The component has had 14 contributors so far which is encouraging; it’s nice when people get involved. Someone even made a screencast explaining how to use the component, so that was a thrill to watch. It feels gratifying to know that something you’ve done is helping others, even if it’s in a minor capacity.&lt;/p&gt;

&lt;h4 id=&quot;recruiting&quot;&gt;Recruiting&lt;/h4&gt;

&lt;p&gt;Last time I was looking for a job I spent months researching companies, applying, and interviewing. The pool of employers (especially remote ones) seemed very small. I had the impression that most companies had their pick of dozens of qualified people. After being involved in the hiring process at my current workplace, it’s clear that that’s not true. Hiring a software developer who is a good fit for the company is very hard. The amount of risk involved in the hiring process is huge because it takes months to hire and onboard a new person. Even senior developers have a tough time making meaningful contributions right away so you could potentially spend many months before you find out that they are not a good fit. I certainly have a lot of empathy with companies now and respect the hiring process more.&lt;/p&gt;

&lt;h4 id=&quot;focus&quot;&gt;Focus&lt;/h4&gt;

&lt;p&gt;Building software isn’t too difficult. Building great software is in my opinion quite hard. A lot of the difficulty comes from having the discipline to focus on one small problem space. Everyone loves new features, so it’s easy to keep adding more and more. Removing features takes a lot of courage, so it rarely happens. Over time you end up building software that does everything but badly. This problem is especially acute when there is no unifying vision, and many viewpoints push and pull the project in different directions.&lt;/p&gt;

&lt;p&gt;Ideally, a project has a strong vision and real actionable data that helps steer the vision. The data about how people are using the app can help the team experiment, iterate and perhaps even remove features to simplify the design.&lt;/p&gt;

&lt;h4 id=&quot;project-management&quot;&gt;Project Management&lt;/h4&gt;

&lt;p&gt;Over the past two years, our teams experimented with many different project management techniques. Usually, this is prompted by perceived low productivity, as if adopting a new process will fix profound organizational issues. My opinion about project management is probably controversial, but it’s pretty simple: any process will do, and the simpler, the better. Adopting agile isn’t going to make your project more attractive, nor is it going to make your codebase easier to work with.&lt;/p&gt;

&lt;p&gt;The agile manifesto misinterpreted:&lt;/p&gt;

&lt;p&gt;“Individuals and interactions over processes and tools” somehow translates as “let’s use a process with a cool sounding acronym (SCRUM!), have daily meetings, estimate everything, and install complicated tools for managing this whole process”. The work ends up being chopped up into tiny little pieces and fed into the developer assembly line. Productivity goes up in the short-term at the cost of quality. Over the long-term productivity goes down as developers have less and less control over their work. Conveniently for managers, now every developer has a number over their head – the one they were forced to estimate.&lt;/p&gt;

&lt;p&gt;“Working software over comprehensive documentation” sounds great if you follow the remaining two statements from the agile manifesto: “Customer collaboration over contract negotiation” and “Responding to change over following a plan.” If the organization as a whole remains invested in contracts, and scales back production after the software is “delivered,” you are, in short, delivering crappy software.&lt;/p&gt;

&lt;p&gt;Ideally, the right people are hired, and they’re trusted to get the job done. The team needs to make meaningful decisions and have a lot of input over the direction of the project. A simple process with continuous delivery is all you need.&lt;/p&gt;

&lt;h4 id=&quot;estimation-vs-bargaining&quot;&gt;Estimation vs. bargaining&lt;/h4&gt;

&lt;p&gt;Estimation starts off as a “rough guess” because people need to know how much the software will cost. The healthy response to a high estimate would be to reduce scope, but that doesn’t always happen. Some people, usually out of desperation, end up bargaining with the developers. “Can you do this in less time?” is always asked and undisciplined developers, eager to please, will say “I’ll see what I can do”. Of course, the result is that software gets delivered faster by skimping on the quality. First, tests go out the window; next, refactors and quality; then UI’s go missing, and so on. What developers are doing is writing technical debt. This debt has to be paid one day – with compound interest.&lt;/p&gt;

&lt;h4 id=&quot;craftsmanship&quot;&gt;Craftsmanship&lt;/h4&gt;

&lt;p&gt;Solving problems as a software developer isn’t that hard. We can deliver a “working solution” relatively quickly. If you’re working with developers, you have to ask yourself whether you want the “working solution” or a finely crafted solution that will pass the test of time. Do you want your users to grit their teeth and suffer through it or do you want them to have a smooth and memorable experience?&lt;/p&gt;

&lt;p&gt;To produce a great product you need talented people working under reasonable expectations. The company needs to have a culture of craftsmanship.&lt;/p&gt;

&lt;p&gt;Ideally, there’s a lack of emphasis on “process” and developers have the freedom to work on whatever inspires them. One day it might be refactoring a major piece of the code that is causing trouble. Another day it might be introducing a useful new feature or tool. The company can deliver a lot of value if it learns how to direct the passion and inspiration of the developers.&lt;/p&gt;

&lt;p&gt;Companies need to encourage developers and reward them for standing their ground about high-quality software. Testing should be a requirement, not a “nice to have.” User experience should be paramount, not an afterthought. Code reviews should have an extremely high bar without comments like “I have to ship this today so we’ll fix it later.”&lt;/p&gt;
</description>
        <pubDate>Sun, 02 Jul 2017 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/long-hiatus/</link>
        <guid isPermaLink="true">http://www.aomran.com/long-hiatus/</guid>
        
        
      </item>
    
      <item>
        <title>Designing &amp; Building RESTful JSON APIs</title>
        <description>&lt;p&gt;Yesterday I gave a talk at Ottawa’s Ruby Meetup. The talk was about designing and building Web APIs. The slides and a recording are below, enjoy!&lt;/p&gt;

&lt;h4 id=&quot;video-recording&quot;&gt;Video recording&lt;/h4&gt;

&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;//www.youtube.com/embed/36M2BSA2LYk&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;

&lt;h4 id=&quot;slides&quot;&gt;Slides&lt;/h4&gt;

&lt;script async=&quot;&quot; class=&quot;speakerdeck-embed&quot; data-id=&quot;8f882520103b01324ddf7ae407783e32&quot; data-ratio=&quot;1.77777777777778&quot; src=&quot;//speakerdeck.com/assets/embed.js&quot;&gt;&lt;/script&gt;

&lt;h4 id=&quot;code&quot;&gt;Code&lt;/h4&gt;

&lt;p&gt;The sample API that I built can be found on &lt;a href=&quot;https://github.com/aomra015/api-presentation&quot;&gt;Github here&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;questions&quot;&gt;Questions?&lt;/h4&gt;

&lt;p&gt;If you have any questions please don’t hesitate to reach out.&lt;/p&gt;
</description>
        <pubDate>Wed, 27 Aug 2014 00:00:00 +0000</pubDate>
        <link>http://www.aomran.com/designing-building-restful-json-apis/</link>
        <guid isPermaLink="true">http://www.aomran.com/designing-building-restful-json-apis/</guid>
        
        
      </item>
    
  </channel>
</rss>
