<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://javieracero.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://javieracero.com/" rel="alternate" type="text/html" /><updated>2021-07-13T07:44:44+00:00</updated><id>https://javieracero.com/feed.xml</id><title type="html">Javier Acero</title><subtitle>About Javier Acero</subtitle><entry xml:lang="en"><title type="html">Consistent Design</title><link href="https://javieracero.com/blog/design-consistency/" rel="alternate" type="text/html" title="Consistent Design" /><published>2021-02-11T00:00:00+00:00</published><updated>2021-02-11T00:00:00+00:00</updated><id>https://javieracero.com/blog/design-consistency</id><content type="html" xml:base="https://javieracero.com/blog/design-consistency/">&lt;p&gt;Consistency is usually a quality desired in the design of any system. Unfortunately it means different things depending on who you talk to. It is frequently interpreted as using a small set of reusable building blocks.&lt;/p&gt;

&lt;p&gt;The promise &lt;em&gt;“Design By Building Blocks”&lt;/em&gt; makes is that one will come up with a design that suits all features of your application. This design will be the &lt;a href=&quot;https://en.wikipedia.org/wiki/Silver_bullet&quot;&gt;silver bullet&lt;/a&gt; one can refer to when adding any functionality to the application. It will also become the standard by which you judge the architecture of any part of your product.&lt;/p&gt;

&lt;p&gt;In most cases this “recipe” comes in the form of a prescribed set of layers, with a side of patterns and restrictions on how these layers communicate with each other. Base abstractions are also a frequent ingredient.&lt;/p&gt;

&lt;h2 id=&quot;tackling-the-median-complexity&quot;&gt;Tackling the median complexity&lt;/h2&gt;

&lt;p&gt;Any modern application today will provide dozens of features to its users. Even an MVP in a private beta will include a handful of them. Each one will have its level of complexity. In a well-designed system, the software’s complexity will reflect the complexity of the functionality it provides.&lt;/p&gt;

&lt;p&gt;At any given time a software product will be composed of a few simple features, several average-complexity features, and a small number of very complex ones. The distribution of the complexity will likely resemble a normal one:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://s3.amazonaws.com/javieracero.com/consistent-design-feature-count-vs-comlexity-1.png&quot; alt=&quot;Complexity-vs-Feature-Count-Distribution&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To find the target design, one focuses on the features that are close to the median-complexity. That guarantees that we are considering the most significant chunk of the system possible. We look into the commonalities these pieces have and infer the patterns and restrictions from them.&lt;/p&gt;

&lt;h2 id=&quot;advantages&quot;&gt;Advantages&lt;/h2&gt;

&lt;p&gt;In theory, your entire system follows the same architecture. This comes with a few perks:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The structure of new features should be known, even before working on them.&lt;/li&gt;
  &lt;li&gt;It should be relatively simple to introduce engineers to the codebase.&lt;/li&gt;
  &lt;li&gt;Engineers can walk away from the codebase, and come back knowing what to expect.&lt;/li&gt;
  &lt;li&gt;Maintenance costs should be minimized by the patterns and restrictions.&lt;/li&gt;
  &lt;li&gt;The costs of introducing new functionalities should be close to linear.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;problems&quot;&gt;Problems&lt;/h2&gt;

&lt;p&gt;While the promise of a sliver bullet is tempting, one should be aware of the dangers ahead.&lt;/p&gt;

&lt;h3 id=&quot;over-engineering&quot;&gt;Over-engineering&lt;/h3&gt;

&lt;p&gt;The features on the left tail of the distribution suffer from over-engineering. The simpler the logic, the bigger the problem is. Boilerplate code and excessive indirection are some of the expected symptoms. These usually complicate the maintainability, making the application harder to understand and evolve.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://s3.amazonaws.com/javieracero.com/consistent-design-feature-count-vs-comlexity-2.png&quot; alt=&quot;Complexity-vs-Feature-Count-Distribution-Left-Tail&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;under-engineering&quot;&gt;Under-engineering&lt;/h3&gt;

&lt;p&gt;The features on the right tail of the distribution tend to be the most problematic of all. Frequently the complexity of the problem to tackle outgrows the prescribed patterns. Bloated abstractions are a frequent indicator that tends to be the source of bugs and burden maintainability.&lt;/p&gt;

&lt;p&gt;It is also very frequent that these components end up being where the consistency breaks. The bloated abstractions are overflown by complexity and engineers refactor it away, ignoring the predefined set of building blocks. Usually, the organization overlooks this situation and believes that consistency is still in place.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://s3.amazonaws.com/javieracero.com/consistent-design-feature-count-vs-comlexity-3.png&quot; alt=&quot;Complexity-vs-Feature-Count-Distribution-Right-Tail&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;the-price-of-consistency&quot;&gt;The price of consistency&lt;/h3&gt;

&lt;p&gt;Keeping the patterns and restrictions consistent across a codebase is, by far, the biggest problem any company that attempts to come up with a building block-based consistent design will face.&lt;/p&gt;

&lt;p&gt;As the feature count grows, it is bound to happen that the initially defined conventions are no longer a good fit for a more significant subset of the system. Maybe even some of the initial building blocks are pin-pointed as the cause of issues.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://s3.amazonaws.com/javieracero.com/consistent-design-feature-count-vs-comlexity-4.png&quot; alt=&quot;Complexity-vs-Feature-Count-Distribution-Evolution&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The time to evolve the design has come.&lt;/p&gt;

&lt;p&gt;The colossal problem is that keeping consistency requires a tremendous effort from the organization. The parts of the system that are suffering issues have to be updated. On top of that, every other feature in the application needs to evolve as the building blocks are refined. It doesn’t matter if it has been working flawlessly and hasn’t required any maintenance in years.&lt;/p&gt;

&lt;p&gt;Facing the level of effort this enterprise will require, most companies will surrender to the original recipe. Design problems will be worked around, or even worse, overlooked. Bugs will start to pile up. Time to market and development costs of new features will grow. Engineers will burn out dealing with repetitive issues, not able to tackle the root problems without breaking the consistency of the design.&lt;/p&gt;

&lt;h2 id=&quot;a-better-approach-to-consistency&quot;&gt;A better approach to consistency&lt;/h2&gt;

&lt;p&gt;Rather than focusing on the pieces of the puzzle, focus on what you are trying to achieve with them. Decide what qualities you want your design to have, define them and make sure that those stay during the evolution of the system.&lt;/p&gt;

&lt;p&gt;Some examples:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Modifiability: The system is easy to understand, modify and evolve.&lt;/li&gt;
  &lt;li&gt;Testability: The system design allows for thorough and easy automated testing.&lt;/li&gt;
  &lt;li&gt;Scalability: The system design allows for the service to be scaled.&lt;/li&gt;
  &lt;li&gt;Resiliency and fault tolerance: The system takes errors into consideration and handles them appropriately.&lt;/li&gt;
  &lt;li&gt;Observability and auditability: The system provides information about what happens inside it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To make sure the architecture exhibits these qualities as it evolves you can define &lt;em&gt;Fitness Functions&lt;/em&gt; to monitor them as described in &lt;a href=&quot;https://www.amazon.es/Building-Evolutionary-Architectures-Support-Constant/dp/1491986360&quot;&gt;Building Evolutionary Architectures&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is imperative to allow for incremental changes to happen, adapting to increasing complexity. These may or may not cross-pollinate between subsets of the system when needed, without explicitly enforcing it.&lt;/p&gt;

&lt;p&gt;A small set of loosely defined building blocks, patterns and restrictions are OK, to begin with, but always take them with a grain of salt. Be flexible and allow for rules to be broken. That’s were innovation starts.&lt;/p&gt;</content><author><name></name></author><summary type="html">Features in any product vary in complexity. Designing for the median complexity and reusing the design is not a cost-effective practice.</summary></entry><entry xml:lang="en"><title type="html">Interesting content (January 2018)</title><link href="https://javieracero.com/blog/interesting-content-january-2018/" rel="alternate" type="text/html" title="Interesting content (January 2018)" /><published>2018-02-16T00:00:00+00:00</published><updated>2018-02-16T00:00:00+00:00</updated><id>https://javieracero.com/blog/interesting-content-january-2018</id><content type="html" xml:base="https://javieracero.com/blog/interesting-content-january-2018/">&lt;p&gt;Running a little late this month, but here we go:&lt;/p&gt;

&lt;h2 id=&quot;talks&quot;&gt;Talks&lt;/h2&gt;

&lt;h3 id=&quot;the-art-of-destroying-software-by-greg-young&quot;&gt;The art of destroying software by Greg Young&lt;/h3&gt;

&lt;p&gt;This one comes, again, from &lt;a href=&quot;https://twitter.com/eferro&quot;&gt;@eferro&lt;/a&gt; and his post about &lt;a href=&quot;http://www.eferro.net/2017/10/simplicidad-para-desarrolladores.html&quot;&gt;&lt;em&gt;Simplicity for developers&lt;/em&gt;&lt;/a&gt;. I’ve been watching most of those talks in the past few weeks.&lt;/p&gt;

&lt;p&gt;I loved this talk, even if it’s a guy talking to the camera for 40 minutes without slides. I usually enjoy all Greg Young’s talks. This one wasn’t an exception.&lt;/p&gt;

&lt;p&gt;The main idea behind it is to focus on building systems from pieces (objects, actors, micro-services, components…) that you can easily delete and re-write. This has a lot of implications, specially when talking about technical debt.&lt;/p&gt;

&lt;p&gt;The other main takeaway from the talk is that we are doing nothing new. This is something that you will hear from other reputed professionals: Uncle Bob shares this very same idea in Clean Architecture several times. &lt;a href=&quot;https://destroyallsoftware.com&quot;&gt;Gary Bernhart&lt;/a&gt; has said the same thing in numerous occasions.&lt;/p&gt;

&lt;h2 id=&quot;books&quot;&gt;Books&lt;/h2&gt;

&lt;p&gt;January was a busy month, including a DNSimple meetup in Florida. I continued reading &lt;a href=&quot;https://www.sandimetz.com/99bottles/&quot;&gt;99 bottles of OOP&lt;/a&gt;. I have just finished reading it but the review will have to wait until next month.&lt;/p&gt;

&lt;p&gt;In the meantime you can &lt;a href=&quot;https://www.instagram.com/p/Bd-tZ4-HNvQ/&quot;&gt;watch this hypnotic video&lt;/a&gt; that &lt;a href=&quot;https://twitter.com/sbastn&quot;&gt;@sbastn&lt;/a&gt; and I recorded on a windy day at Melbourne Beach.&lt;/p&gt;

&lt;h2 id=&quot;posts&quot;&gt;Posts&lt;/h2&gt;

&lt;h3 id=&quot;auto-squashing-git-commits-by-thought-bot&quot;&gt;Auto-squashing git commits by Thought Bot&lt;/h3&gt;

&lt;p&gt;This one &lt;a href=&quot;https://twitter.com/amuino/status/943130121975881729&quot;&gt;came into my twitter timeline thanks to @amuino&lt;/a&gt; back in December but I didn’t have the chance to read it until January…&lt;/p&gt;

&lt;p&gt;It introduces a couple features that you can use when rebasing that I wasn’t familiar with. It may encourage me to rebase more often than I usually do.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://robots.thoughtbot.com/autosquashing-git-commits&quot;&gt;&lt;strong&gt;Read it here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;stop-abusing-notimplementederror&quot;&gt;Stop abusing NotImplementedError&lt;/h3&gt;

&lt;p&gt;I found this post in the comments of &lt;a href=&quot;https://www.youtube.com/watch?v=rK8yHl0cHoc&quot;&gt;a talk on Youtube&lt;/a&gt;. Funny enough the author seems to have deleted it.&lt;/p&gt;

&lt;p&gt;It explains why we should not use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NotImplementedError&lt;/code&gt; when trying to mimic interfaces or abstract classes in Ruby. This is, to my knowledge, a widely used practice. Finding out about the details of this error was very enlightening to me.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://chrisstump.online/2016/03/23/stop-abusing-notimplementederror/&quot;&gt;&lt;strong&gt;Read it here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;the-modern-dev-team&quot;&gt;The modern dev team&lt;/h3&gt;

&lt;p&gt;This one was dropped in the &lt;a href=&quot;https://dnsimple.com&quot;&gt;DNSimple&lt;/a&gt; Slack channel.&lt;/p&gt;

&lt;p&gt;There are 2 things that made me enjoy this post a lot:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Reflects on the fact that we keep resolving the same problems with different tools. Also says that Erlang solved this problems a long time ago and got most of the solutions right (which is a thought that I keep having more and more often).&lt;/li&gt;
  &lt;li&gt;Shares the view of a developer that just turned 50. We will all get there (🤞), so reading about the fears that will come may make it easier to grow older as a software engineer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href=&quot;https://rob.conery.io/2018/01/22/the-modern-dev-team/&quot;&gt;&lt;strong&gt;Read it here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;dont-cross-the-beams-by-kent-beck&quot;&gt;Don’t cross the beams by Kent Beck&lt;/h3&gt;

&lt;p&gt;This is one was referenced on &lt;a href=&quot;https://www.sandimetz.com/99bottles/&quot;&gt;99 bottles of OOP&lt;/a&gt;. Like pretty much everything that Kent Beck writes, this is a must read.&lt;/p&gt;

&lt;p&gt;The post explains the difference between vertical and horizontal refactorings and why you should perform them in order rather than mix them.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.facebook.com/notes/kent-beck/dont-cross-the-beams-avoiding-interference-between-horizontal-and-vertical-refac/260531380646400/&quot;&gt;&lt;strong&gt;Read it here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;</content><author><name></name></author><summary type="html">Interesting books, talks and blog posts I have viewed or read during January 2018 and that I consider worth sharing for one reason or another.</summary></entry><entry xml:lang="en"><title type="html">Interesting content (December 2017)</title><link href="https://javieracero.com/blog/interesting-content-december-2017/" rel="alternate" type="text/html" title="Interesting content (December 2017)" /><published>2018-01-07T00:00:00+00:00</published><updated>2018-01-07T00:00:00+00:00</updated><id>https://javieracero.com/blog/interesting-content-december-2017</id><content type="html" xml:base="https://javieracero.com/blog/interesting-content-december-2017/">&lt;p&gt;Happy New Year everyone! I’m starting 2018 trying to go back to writing here from time to time.&lt;/p&gt;

&lt;h2 id=&quot;talks&quot;&gt;Talks&lt;/h2&gt;

&lt;h3 id=&quot;the-right-way-to-ship-software&quot;&gt;The “right” way to ship software&lt;/h3&gt;

&lt;p&gt;By &lt;a href=&quot;https://www.linkedin.com/in/jgoldfein/&quot;&gt;Jocelyn Goldfein&lt;/a&gt; former Engineer Director at Facebook (2010-2014) and  VP of Engineering at VMWARE (2003-2010). The talk took place as part of &lt;a href=&quot;https://hacksummit.org/&quot;&gt;hack.summit() 2016&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I &lt;a href=&quot;http://www.eferro.net/2017/12/it-depends-jocelyn-goldfin-model-for.html&quot;&gt;found this one&lt;/a&gt; thanks to &lt;a href=&quot;https://twitter.com/eferro&quot;&gt;@eferro&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It’s a really thoughtful talk on how the way software is released is totally dependent on the context, and how it shapes companies, practices and the way we build software in general. &lt;strong&gt;Must watch&lt;/strong&gt;. One of those talks that put into words things that seem obvious, but aren’t.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=VmYvJ3kDIWE&quot;&gt;&lt;strong&gt;Watch it here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;evolutionary-architectures&quot;&gt;Evolutionary architectures&lt;/h3&gt;

&lt;p&gt;Saw a few talks on the same topic:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;One by Rebecca Parsons (&lt;a href=&quot;https://twitter.com/rebeccaparsons&quot;&gt;@rebeccaparsons&lt;/a&gt;) CTO at ThoughtWorks. &lt;a href=&quot;https://www.youtube.com/watch?v=OU_NKGEVsy8&quot;&gt;&lt;strong&gt;Watch it here&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;One by Neal Ford (&lt;a href=&quot;https://twitter.com/neal4d&quot;&gt;@neal4d&lt;/a&gt;) from ThoughtWorks.  &lt;a href=&quot;https://www.youtube.com/watch?v=CglSFhwbI3s&quot;&gt;&lt;strong&gt;Watch it here&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;One by Pat Kua (&lt;a href=&quot;https://twitter.com/patkua&quot;&gt;@patkua&lt;/a&gt;) CTO at N26, former ThoughtWorks.  &lt;a href=&quot;https://www.youtube.com/watch?v=7e6Ww8b2hzQ&quot;&gt;&lt;strong&gt;Watch it here&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I got these from &lt;a href=&quot;https://twitter.com/GermanDZ/status/939258688048648193&quot;&gt;this Twitter thread&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Talks present a, somewhat different, view of software architecture explaining how it can, and should, be the vehicle to allow guided, incremental, change.&lt;/p&gt;

&lt;p&gt;Love the underlying concept and will certainly investigate more about the topic (in fact &lt;a href=&quot;http://shop.oreilly.com/product/0636920080237&quot;&gt;the book they wrote&lt;/a&gt; is already in my iPad and I’m looking forward to start it).&lt;/p&gt;

&lt;p&gt;Neal’s and Pat’s are really the same talk delivered by two separate speakers. I liked Neal’s better. Rebecca’s is slightly different and addresses other interesting ideas. I re-watched Rebecca’s after watching Neal’s (and Pat’s) and I think you’ll get more value if you watch them in that order. Don’t expect technical details as the talks (and the topic) are more high level.&lt;/p&gt;

&lt;h2 id=&quot;books&quot;&gt;Books&lt;/h2&gt;

&lt;h3 id=&quot;clean-architecture-uncle-bob&quot;&gt;Clean Architecture (Uncle Bob)&lt;/h3&gt;

&lt;p&gt;It’s been almost 6 years since &lt;a href=&quot;https://www.youtube.com/watch?v=WpkDN78P884&quot;&gt;&lt;em&gt;Architecture: The Lost Years&lt;/em&gt;&lt;/a&gt; was published. That talk shook my world and caused quite some noise in the Ruby community. It pointed out everything that is wrong with &lt;em&gt;The Rails Way&lt;/em&gt; (aka &lt;em&gt;The Framework Way&lt;/em&gt;). The main complian about the talk was that it didn’t include any examples.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The database is a detail.
The web is a detail.
Frameworks are not your application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately the book doesn’t go much further than all the previous content that the author had already shared in blog posts, &lt;a href=&quot;https://cleancoders.com/&quot;&gt;Clean Coders&lt;/a&gt; videos or talks. In that sense, it’s a &lt;em&gt;“greatest hits compilation”&lt;/em&gt;. Most people will expect an in-depth example and won’t find it in the book either.&lt;/p&gt;

&lt;p&gt;There are two things that I really enjoyed though:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Part II&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;In this section the author discusses the 3 main programming paradigms (Procedural, Object Oriented and Functional). It goes over the main traits of each one and explains how each one of them imposes restrictions on the programmer, which is a point of view that I had never heard of before.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;The epilogue.&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;I’d buy the book only to read it.&lt;/p&gt;

    &lt;p&gt;In it the author goes over most of his career explaining what companies did he work for and what he did there. He explains the challenges he faced and what they did about them at the time. All of it sauced with pictures of the old computers and different devices that they were using at the time.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;the-nature-of-software-development-ron-jeffries&quot;&gt;The Nature of Software Development (Ron Jeffries)&lt;/h3&gt;

&lt;p&gt;I was expecting a more technical book. I had read good reviews by some peers and went for it without really knowing what the book was about. Got very little out of it because I was familiar with most of the ideas discussed in it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Don’t worry—we’re not going to drop into programming—but we do want all our readers to be aware of what the business needs to expect of developers, and what it needs to support.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s short and easy to read. Goes to the point and justifies everything clearly. It even points out something that most books about software processes ignore: to be able to build software iteratively (aka Agile Software Development) you need skilled developers.&lt;/p&gt;

&lt;p&gt;Even though I didn’t get much out of it, I must say that, if I have ever have to recommend one book to anyone as an introduction to iterative software development, it will be this one. It is &lt;em&gt;that&lt;/em&gt; good.&lt;/p&gt;

&lt;h2 id=&quot;posts&quot;&gt;Posts&lt;/h2&gt;

&lt;h3 id=&quot;managing-your-personal-tech-portfolio&quot;&gt;Managing your personal tech portfolio&lt;/h3&gt;

&lt;p&gt;This post got to my twitter feed (was retweeted by Neal Ford). Went through it quickly as it’s an easy read. It contains good advice to structure your learning. Most tips you probably have heard before, but I found two ideas really interesting:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Treating it as an investment portfolio was really appealing to me.&lt;/li&gt;
  &lt;li&gt;Having your own &lt;a href=&quot;https://www.thoughtworks.com/radar&quot;&gt;technology radar a la ThoughtWorks&lt;/a&gt; to guide learning.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href=&quot;https://crswlls.wordpress.com/2017/12/11/managing-your-personal-tech-portfolio/&quot;&gt;&lt;strong&gt;Read it here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;excuses-by-uncle-bob&quot;&gt;Excuses by Uncle bob.&lt;/h3&gt;

&lt;p&gt;I follow Uncle Bob, but it ended up in my timeline retweeted by several people I follow.&lt;/p&gt;

&lt;p&gt;What can I say, I love pro-TDD rants and this post is one of those. I have tried to stay away from rants against or in favor of TDD for the last 4 years. I can’t believe our industry is still debating about this…&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.cleancoder.com/uncle-bob/2017/12/18/Excuses.html&quot;&gt;&lt;strong&gt;Read it here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;elixir-code-quality-tools-by-matt-gauger&quot;&gt;Elixir code quality tools by Matt Gauger&lt;/h3&gt;

&lt;p&gt;This one was part of &lt;a href=&quot;http://plataformatec.com.br/elixir-radar/&quot;&gt;Elixir Radar&lt;/a&gt; issue 123.&lt;/p&gt;

&lt;p&gt;Although I’m not a big fan of these type of tools (specially Rubocop), there are some useful ones in this post.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.mattgauger.com/2017/11/21/elixir-code-quality-tools&quot;&gt;&lt;strong&gt;Read it here&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;</content><author><name></name></author><summary type="html">Interesting books, talks and blog posts I have viewed or read during December 2017 and that I consider worth sharing for one reason or another.</summary></entry><entry xml:lang="en"><title type="html">The smell of that potion</title><link href="https://javieracero.com/blog/the-smell-of-that-potion/" rel="alternate" type="text/html" title="The smell of that potion" /><published>2016-04-04T00:00:00+00:00</published><updated>2016-04-04T00:00:00+00:00</updated><id>https://javieracero.com/blog/the-smell-of-that-potion</id><content type="html" xml:base="https://javieracero.com/blog/the-smell-of-that-potion/">&lt;p&gt;I’ve been using &lt;a href=&quot;https://www.ruby-lang.org&quot;&gt;Ruby&lt;/a&gt; as my main programming language since 2010. As of today I still enjoy its syntax and simplicity. Its community is very friendly and productive and many companies and products have sprung from it.&lt;/p&gt;

&lt;p&gt;However all these years have not been a path of roses. I have seen how the Ruby community has been deeply influenced by one big project: &lt;a href=&quot;http://rubyonrails.org/&quot;&gt;Ruby on Rails&lt;/a&gt;. The standards and conventions of that project were taken as the right way of using the language. There have been &lt;a href=&quot;http://blog.steveklabnik.com/posts/2011-12-30-active-record-considered-harmful&quot;&gt;plenty&lt;/a&gt; of &lt;a href=&quot;http://confreaks.tv/videos/goruco2012-hexagonal-rails&quot;&gt;voices&lt;/a&gt; &lt;a href=&quot;http://solnic.eu/2015/06/06/cutting-corners-or-why-rails-may-kill-ruby.html&quot;&gt;complaining&lt;/a&gt; about it yet the mainstream mentality hasn’t changed one bit.&lt;/p&gt;

&lt;p&gt;The other problem that any Ruby developer will face sooner rather than later is performance. If there were people in the community warning about the Rails way of doing things the complains about the performance have been an outcry.&lt;/p&gt;

&lt;p&gt;It’s been three years since this thought crossed my mind for the first time and I still think that &lt;strong&gt;Rails and the &lt;a href=&quot;https://en.wikipedia.org/wiki/Global_interpreter_lock&quot;&gt;GIL&lt;/a&gt; will kill&lt;sup id=&quot;fnref:C1_1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; Ruby&lt;/strong&gt;. There are countless examples of how bad the maintainability of Rails applications gets as the code base grows. You will find even more cases of performance issues. There is a lesson to be learnt lurking in there. A lesson that I won’t easily forget when I choose my next go-to programming language.&lt;/p&gt;

&lt;p&gt;Speaking of which: I have been playing with &lt;a href=&quot;http://elixir-lang.org&quot;&gt;Elixir&lt;/a&gt; lately. It was a young language back in 2013 when I first tried it out. It’s more mature now. Several libraries and &lt;a href=&quot;http://www.catonmat.net/blog/frameworks-dont-make-sense/&quot;&gt;frameworks&lt;/a&gt; are making the language ecosystem more interesting. One of these projects is &lt;a href=&quot;http://www.phoenixframework.org/&quot;&gt;Phoenix&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Phoenix is a mayor player in the language now. Big enough to make creating a new Phoenix project a task in the language build tool, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mix&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I have to admit that I haven’t given the framework a serious try yet. But all the demos and guides I have seen reminded me so much of Rails that I wanted to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;brew uninstall elixir&lt;/code&gt; immediately. While performance won’t be an issue, maintainability and bad practices may turn Elixir into the new Ruby.&lt;/p&gt;

&lt;p&gt;It was a relief to find &lt;a href=&quot;http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/&quot;&gt;this article&lt;/a&gt; by José Valim (the creator of Elixir) while researching about testing and mocking tools. That blog post is a summary of the mocking and testing practices that I have been following and advocating for several years. I was super pleased not only to see that José advocates them too but also that the language has support for them right out of the box.&lt;/p&gt;

&lt;p&gt;I am looking forward to give Phoenix a try. Hope all that Rails-iness is only a coat intended to create familiarity. Hope Elixir and its community embraces modularity and good design practices like Ruby never did. My fingers are crossed.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:C1_1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;And by killing I mean significantly impacting the usage of the language. &lt;a href=&quot;#fnref:C1_1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">Some thoughts on Ruby, Rails, Elixir and Phoenix and how libraries, frameworks and communities influence the practices and the sustainability of languages.</summary></entry><entry xml:lang="en"><title type="html">Starting with EventMachine IV</title><link href="https://javieracero.com/blog/starting-with-eventmachine-iv/" rel="alternate" type="text/html" title="Starting with EventMachine IV" /><published>2013-06-15T00:00:00+00:00</published><updated>2013-06-15T00:00:00+00:00</updated><id>https://javieracero.com/blog/starting-with-eventmachine-iv</id><content type="html" xml:base="https://javieracero.com/blog/starting-with-eventmachine-iv/">&lt;p&gt;This blog post is an extract of an ebook about EventMachine that I started
writing some time ago. Somehow it ended up half written in some dark corner
of my hard drive. I have decided to publish the material I have as series of
posts. &lt;strong&gt;Please let me know if you find the material useful.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can find all posts here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-i&quot;&gt;Starting with EventMachine I&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-ii&quot;&gt;Starting with EventMachine II&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iii&quot;&gt;Starting with EventMachine III&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iv&quot;&gt;Starting with EventMachine IV&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;doing-it-the-wrong-way&quot;&gt;Doing it the wrong way&lt;/h2&gt;
&lt;p&gt;For a long time Ruby’s threading issues have been a problem for application scalability and performance. EventMachine is a great tool to tackle that problem. But it’s no silver bullet. It has its drawbacks and you need to be aware of them in order to unleash all it’s potential.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Asynchronous I/O for your Ruby programs&lt;/em&gt;. The title of the book&lt;sup id=&quot;fnref:C1_10&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_10&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; already points to the kind of Ruby application that may take advantage of EventMachine’s capabilities. You won’t make a CPU-heavy program faster by throwing this library in (or any other reactor based library for that matter). The reactor is the heart and soul of EventMachine and its strengths and weaknesses are shared.&lt;/p&gt;

&lt;p&gt;That’s why you should know this pattern well. Learning the frequent mistakes that people make using it and how to avoid them is something any developer using EventMachine should read about.&lt;/p&gt;

&lt;h3 id=&quot;blocking-the-reactor&quot;&gt;Blocking the reactor&lt;/h3&gt;
&lt;p&gt;Web applications are I/O heavy. Receiving requests and returning responses is reading-from and writing-into sockets. That’s pure input and output operations. We may think that web applications are a good fit for reactor based libraries and frameworks, right? The answer, like in almost any software problem, is &lt;em&gt;it depends&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It is true that asynchronous operations will make the reactor shine. The reactor thread doesn’t block waiting for requests to come. The operating system will notify when there is information available. In the meantime the application can be serving other requests. The same thing will happen handling responses: the write operation will be executed asynchronously without blocking the thread. It’s concurrency paradise!&lt;/p&gt;

&lt;p&gt;While it is true that receiving requests and sending responses will greatly benefit of the use of EventMachine we also need to consider what happens in between. If the responses are generated really fast, we truly are in concurrency paradise. We are in front the kind of software that truly fits the reactor pattern. But what happens if the application needs to perform some long running task before generating the response?&lt;/p&gt;

&lt;p&gt;In the previous section we discussed how event demultiplexing works in EventMachine’s reactor. Let’s apply that knowledge to the processing of an HTTP request. When a socket has received the HTTP request information and it’s ready to be read, the reactor will be notified by the operating system and it will dispatch the corresponding callback to process it. The callback will be invoked immediately. And, what’s more important, &lt;strong&gt;synchronously&lt;/strong&gt;. It takes control of the reactor’s thread and &lt;strong&gt;the event loop will be blocked until the callback execution is over&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You may not see the implications blocking the reactor right now. Let’s put an example so you can experiment them:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EM.run do
  EM.add_periodic_timer(1) { puts &apos;sec&apos; }
  EM.add_timer(1.01) { puts &apos;1 second passed by&apos; }
  EM.add_timer(2.01) { puts &apos;2 seconds passed by&apos;; }
  EM.add_timer(5.01) do
    puts &apos;5 seconds passed by: stopping...&apos;
    EM.stop
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We are introducing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.add_periodic_timer&lt;/code&gt;, a new timer flavour that instead of running just once runs indefinitely. Along the periodic timer we are setting up other timers&lt;sup id=&quot;fnref:C1_08&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_08&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; that should produce the following output:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sec
1 second passed by
sec
2 seconds passed by
sec
sec
5 seconds passed by: stopping...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Every second we will see &lt;em&gt;sec&lt;/em&gt; printed out. Along those messages, we will see the specific messages when one and two seconds pass by. Finally, after five seconds, we will see the terminating message and the process will exit.&lt;/p&gt;

&lt;p&gt;Now let’s change one of the callbacks. We are going to block the reactor thread when handling the event &lt;em&gt;one second passing by&lt;/em&gt; making the process sleep for ten seconds:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EM.run do
  EM.add_periodic_timer(1) { puts &apos;sec&apos; }
  EM.add_timer(1.01) do
    puts &apos;1 second passed by&apos;
    sleep 10
  end
  EM.add_timer(2.01) { puts &apos;2 seconds passed by&apos;; }
  EM.add_timer(5.01) do
    puts &apos;5 seconds passed by: stopping...&apos;
    EM.stop
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Lets check what is printed out to standard output:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sec
1 second passed by
sec
2 seconds passed by
5 seconds passed by: stopping...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wait a second! What’s going on? This process has been running for more than ten seconds. Where are all the missing &lt;em&gt;sec&lt;/em&gt; messages? Why didn’t the other output get printed when it was supposed to? EventMachine is broken!&lt;/p&gt;

&lt;p&gt;Actually it is not. Let’s review the example to understand what’s going on: The first event that will be triggered will be &lt;em&gt;one second passing by&lt;/em&gt; corresponding to the periodic timer. A few reactor ticks later, once ten milliseconds have passed by, the next event will be dispatched. It’s handler will be run and it will execute the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep 10&lt;/code&gt; sentence. That will block the event loop thread. EventMachine won’t be able to check whether more events have been triggered. For ten seconds the reactor won’t &lt;em&gt;tick&lt;/em&gt; at all.&lt;/p&gt;

&lt;p&gt;When the process is done sleeping the event loop returns to its usual workflow finishing the previous iteration. In the next one it will start checking if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stop&lt;/code&gt; has been called. Since the method hasn’t been called yet it will verify if timers have expired. Given that more than ten seconds have gone by since the last check, three callbacks need to be executed: the one corresponding to the periodic timer, the one of the 2 seconds timer and, last, the one of the 5 seconds timer.&lt;/p&gt;

&lt;p&gt;Once the timer events have been handled, the reactor will find the call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stop&lt;/code&gt; and terminate.&lt;/p&gt;

&lt;p&gt;In our case blocking the reactor has made us lost three periodic timer events plus delaying another three. There is no I/O involved in this example, but the results would be pretty much the same: no reads or writes would be performed while the event loop is blocked. Sockets ready to be read or written will queue and the throughput of the application would be very poor.&lt;/p&gt;

&lt;p&gt;The reactor pattern excels at handling events with callbacks that are dispatched quickly. While a callback is being executed it can’t deal with other events and that’s bad for the performance of any EventMachine powered program.&lt;/p&gt;

&lt;p&gt;So, keep it in mind at all times: &lt;strong&gt;do NOT block the event loop&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;using-synchronous-io&quot;&gt;Using synchronous I/O&lt;/h3&gt;
&lt;p&gt;In the previous example you can tell pretty easily what’s blocking the reactor and why. There are other ways of causing the same issues that are way more subtle. One of the most common ways of doing it and not noticing is using gems that use blocking I/O.&lt;/p&gt;

&lt;p&gt;If you use a gem that relies on libraries that are not reactor-aware the I/O operations that you perform won’t be asynchronous. EventMachine won’t handle those sockets or file descriptors and the read and writes from or to them will be blocking. You will be losing all the benefits the reactor pattern provides.&lt;/p&gt;

&lt;p&gt;Lets change our previous example a little bit. We are going to remove the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep&lt;/code&gt; and do some blocking I/O operation instead: we are going to get Twitter’s landing page using Ruby standard library’s HTTP capabilities (remember to require &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;net/http&lt;/code&gt;). We will do it ten times in order to have the reactor blocked long enough so we can see the effects. We are also going to make our example last a little shorter: three seconds instead of five.&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;URL = &apos;http://twitter.com&apos;

EM.run do
  EM.add_periodic_timer(1) { puts &apos;sec&apos; }
  EM.add_timer(1.01) do
    puts &apos;one second passed by&apos;
    10.times do
      response = Net::HTTP.get_response(URI.parse(URL))
      puts response.code
    end
  end
  EM.add_timer(2.01) { puts &apos;two seconds passed by&apos; }
  EM.add_timer(3.01) do
    puts &apos;three seconds passed by: stopping...&apos;
    EM.stop
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The results will resemble to the ones we got blocking the loop with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sec
one second passed by
200
200
200
200
200
200
200
200
200
200
sec
two seconds passed by
three seconds passed by: stopping...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While the requests are done no other event is handled by the reactor. We don’t see the expected &lt;em&gt;sec&lt;/em&gt; message when one second passes by, nor is EventMachine stopped after three seconds. We are losing events and making timers malfunction.&lt;/p&gt;

&lt;p&gt;Now, let’s swap our blocking HTTP library for an asynchronous one: &lt;a href=&quot;https://github.com/igrigorik/em-http-request&quot;&gt;em-http-request&lt;/a&gt;. You will need to install the gem by running:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem install em-http-request
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then require it in your example code:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;require &apos;em-http&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Look carefully how the &lt;em&gt;one second passing by&lt;/em&gt; event handler changes. We are now using non-blocking I/O and that means setting up our response status code output message inside a callback.&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EM.run do
  EM.add_periodic_timer(1) { puts &apos;sec&apos; }
  EM.add_timer(1.01) do
    puts &apos;one second passed by&apos;
    10.times do
      http = EM::HttpRequest.new(URL).get
      http.callback { puts http.response_header.status }
    end
  end
  EM.add_timer(2.01) { puts &apos;two seconds passed by&apos; }
  EM.add_timer(3.01) do
    puts &apos;three seconds passed by: stopping...&apos;
    EM.stop
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let’s run this code and analyse the output:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sec
one second passed by
200
200
200
sec
two seconds passed by
200
200
200
200
200
200
sec
three seconds passed by: stopping...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We get the expected three &lt;em&gt;sec&lt;/em&gt;s. Responses are handled without blocking the reactor and that means that &lt;em&gt;200&lt;/em&gt; messages are placed among the timer-triggered ones&lt;sup id=&quot;fnref:C1_09&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_09&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;. Besides that you have experienced how fast asynchronous I/O can be.&lt;/p&gt;

&lt;p&gt;You may be wondering how can the performance improvement be so dramatic.&lt;/p&gt;

&lt;p&gt;Every time we are calling a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get&lt;/code&gt; on an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM::HttpRequest&lt;/code&gt; instance we are adding a socket to the resources handled by the reactor. Once done the event loop keeps ticking. Those connections will be handled within it. On every tick the reactor will poll those resources and trigger the corresponding callback if responses came back.&lt;/p&gt;

&lt;p&gt;On the other hand, when we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;net/http&lt;/code&gt;, every call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_response&lt;/code&gt; blocks the thread waiting for the server to respond. It repeats the same process for each one of the ten requests that we are making. The reactor doesn’t have a chance to go through it’s usual event demultiplexing loop during that time. Hence the event loosing and timer events delaying.&lt;/p&gt;

&lt;p&gt;That’s why you always have be sure that you are using reactor aware libraries for I/O. Remember: &lt;strong&gt;do NOT block the event loop&lt;/strong&gt;.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:C1_10&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Remember: this is an extract of an unfinished e-book. &lt;a href=&quot;#fnref:C1_10&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:C1_08&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;We are adding a 0.1 to the non-periodic timers so we make sure that &lt;em&gt;sec&lt;/em&gt; is printed out prior to the other messages that run almost simultaneously. We think the output is easier to follow that way. Otherwise we cant be sure of the order in which messages would be printed out. &lt;a href=&quot;#fnref:C1_08&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:C1_09&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;If your network connection is slow three seconds may not be enough for the 10 requests to be responded. In that case you may not get the expected ten &lt;em&gt;200&lt;/em&gt;. You can either increase the timer that will make EventMachine stop. There are better ways to ensure that you get the ten responses before stopping the reactor but we haven’t seen them yet. &lt;a href=&quot;#fnref:C1_09&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">Using EventMachine is not easy and people make well known mistakes. Let&apos;s review the most common ones and find and explain the problems behind them.</summary></entry><entry xml:lang="en"><title type="html">A useful shell function</title><link href="https://javieracero.com/blog/a-useful-shell-function/" rel="alternate" type="text/html" title="A useful shell function" /><published>2013-05-31T00:00:00+00:00</published><updated>2013-05-31T00:00:00+00:00</updated><id>https://javieracero.com/blog/a-useful-shell-function</id><content type="html" xml:base="https://javieracero.com/blog/a-useful-shell-function/">&lt;p&gt;In last week’s
&lt;a href=&quot;http://rubyweekly.com&quot;&gt;Ruby Weekly&lt;/a&gt;
one of the
&lt;a href=&quot;http://daniel.fone.net.nz/blog/2013/05/20/a-better-way-to-manage-the-rails-secret-token/&quot;&gt;recommended posts&lt;/a&gt;
was about a gem called
&lt;a href=&quot;https://github.com/bkeepers/dotenv&quot;&gt;Dotenv&lt;/a&gt;.
This gem allows you to setup the environment variables your rails
application needs in order to work on your development environment.&lt;/p&gt;

&lt;p&gt;The gem will load the environment variables from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ENV&lt;/code&gt;
files in the application folder. To do so, you will need to call the
gem from your application.&lt;/p&gt;

&lt;p&gt;The problem is that the gem is conceived to be used within ruby but
environment variables are an OS thing and you may need to solve the
same problem when using other platforms or languages.&lt;/p&gt;

&lt;p&gt;Probably they have similar libraries to do it. But, &lt;strong&gt;you
already have all the tools you need: your shell&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;exporting-variables&quot;&gt;Exporting variables&lt;/h2&gt;
&lt;p&gt;I really like ZSH. And ZSH provides
&lt;a href=&quot;http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions&quot;&gt;hooks to certain events&lt;/a&gt;
you can plug functions into.&lt;/p&gt;

&lt;p&gt;One of those hooks is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chpwd&lt;/code&gt;. It will run a function every time you
change your working directory. We can make use of it, and write a
simple function that checks for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; files in the destination folder
and sources it.&lt;/p&gt;

&lt;p&gt;To do that we only need to write a little function and bind it to the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chpwd&lt;/code&gt; hook. That code belongs in my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.zshrc&lt;/code&gt; file and looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;shell&quot;&gt;function set_environment() {
  env_file=&quot;$PWD/.env&quot;
  if [ -e $env_file ]; then source $env_file; fi
}
function chpwd() { set_environment; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; files but the name doesn’t really matter.&lt;/p&gt;

&lt;p&gt;Then I create the file and export the environment variables:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export TWITTER_KEY=&apos;&apos;
export TWITTER_SECRET=&apos;&apos;
export S3_KEY=&apos;&apos;
export S3_SECRET=&apos;&apos;
export S3_ENDPOINT=&apos;s3-eu-west-1.amazonaws.com&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And, as soon as you enter the directory where the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; is placed
the variables will be automatically exported and available for your
application to read.&lt;/p&gt;

&lt;h2 id=&quot;there-are-other-uses&quot;&gt;There are other uses&lt;/h2&gt;
&lt;p&gt;But you can do way more than exporting environment variables: you can
tailor your shell to the project you are working with.&lt;/p&gt;

&lt;p&gt;Recently I started working on a Python project. It’s my first python
project and one of my coworkers introduced me to
&lt;a href=&quot;https://pypi.python.org/pypi/virtualenv&quot;&gt;virtualenv&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But, unlike RVM or rbenv, there is no such thing (as far as we know)
like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ruby-version&lt;/code&gt; files. So, I used a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.env&lt;/code&gt; file to activate
the python virtual environment every time I enter the project’s
folder:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;source project-virtualenv/bin/activate
alias activate=&quot;source project-virtualenv/bin/activate&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I also used it to create an alias that allows me to have the opposite
command to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deactivate&lt;/code&gt; one provided by virtualenv (just in
case I deactivate the environment by hand).&lt;/p&gt;

&lt;p&gt;As you see the possibilities are enormous. There are a lot of
things you can do.  Hope you find it as useful as I do!&lt;/p&gt;</content><author><name></name></author><summary type="html">Nowadays it&apos;s very common to put some of your configuration values, like API secrets, in environment variables. But dealing with those variables can be painful.</summary></entry><entry xml:lang="en"><title type="html">Starting with EventMachine III</title><link href="https://javieracero.com/blog/starting-with-eventmachine-iii/" rel="alternate" type="text/html" title="Starting with EventMachine III" /><published>2013-04-07T00:00:00+00:00</published><updated>2013-04-07T00:00:00+00:00</updated><id>https://javieracero.com/blog/starting-with-eventmachine-iii</id><content type="html" xml:base="https://javieracero.com/blog/starting-with-eventmachine-iii/">&lt;p&gt;This blog post is an extract of an ebook about EventMachine that I started
writing some time ago. Somehow it ended up half written in some dark corner
of my hard drive. I have decided to publish the material I have as series of
posts. &lt;strong&gt;Please let me know if you find the material useful.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can find all posts here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-i&quot;&gt;Starting with EventMachine I&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-ii&quot;&gt;Starting with EventMachine II&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iii&quot;&gt;Starting with EventMachine III&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iv&quot;&gt;Starting with EventMachine IV&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;just-enough-about-patterns&quot;&gt;Just enough about patterns&lt;/h2&gt;

&lt;p&gt;In the &lt;a href=&quot;/blog/starting-with-eventmachine-i&quot;&gt;&lt;em&gt;hello world&lt;/em&gt; example&lt;/a&gt; we talked about something called the &lt;em&gt;event loop&lt;/em&gt;. The &lt;em&gt;event loop&lt;/em&gt; is one of the two patterns EventMachine is powered by. Although you could use the library without really knowing anything about this patterns, understanding how they work will make a huge difference. There are certain features and bad practices that wouldn’t make sense otherwise.&lt;/p&gt;

&lt;p&gt;We are going to overview both patterns but it’s going to be a really brief introduction. Later in the book we will develop our own reactor and event loop to get a better grasp of their internals.&lt;/p&gt;

&lt;h3 id=&quot;the-reactor&quot;&gt;The reactor&lt;/h3&gt;
&lt;p&gt;The reactor is an event handling pattern designed to manage service requests that are delivered concurrently to an application. It was conceived with the &lt;a href=&quot;http://www.kegel.com/c10k.html&quot;&gt;C10K problem&lt;/a&gt; in mind.&lt;/p&gt;

&lt;p&gt;The reactor has to be aware of the different service requests or events it will be watching for and their corresponding handlers or callbacks. Those events will come through a set of resources or &lt;em&gt;handles&lt;/em&gt;. In EventMachine the &lt;em&gt;handles&lt;/em&gt; will be I/O resources like sockets or file descriptors.&lt;/p&gt;

&lt;p&gt;Once initialized, the reactor will enter a phase known as &lt;em&gt;event demultiplexing phase&lt;/em&gt;. During this stage it will poll the resources for a very sort time span waiting for events to happen. Once an event is triggered in one of the &lt;em&gt;handles&lt;/em&gt;, the registered callback for that event, in that particular resource, will be dispatched.&lt;/p&gt;

&lt;p&gt;With this newly acquired knowledge we can rephrase what the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.run&lt;/code&gt; method does: it boots EventMachine’s reactor taking a block that will be yielded right before it enters the &lt;em&gt;event demultiplexing phase&lt;/em&gt;. That is, prior to starting the event loop.&lt;/p&gt;

&lt;h3 id=&quot;the-event-loop&quot;&gt;The event loop&lt;/h3&gt;
&lt;p&gt;The &lt;em&gt;event loop&lt;/em&gt; is EventMachine’s reactor’s event demultiplexer.&lt;/p&gt;

&lt;p&gt;When the reactor enters this phase it gets trapped in a loop&lt;sup id=&quot;fnref:C1_06&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_06&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. Each of the iterations through this loop is known as a &lt;em&gt;reactor tick&lt;/em&gt; or just &lt;em&gt;tick&lt;/em&gt;. During each of them the reactor performs the same three actions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Check whether enough time has passed by for a timer event to be triggered. It will handle those events by calling the set up callbacks.&lt;/li&gt;
  &lt;li&gt;Check if any of the handled resources is ready to be read or written and trigger the corresponding event. EventMachine uses the &lt;a href=&quot;http://en.wikipedia.org/wiki/Asynchronous_I/O#Select.28.2Fpoll.29_loops&quot;&gt;select&lt;/a&gt;&lt;sup id=&quot;fnref:C1_05&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_05&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; system call to poll the &lt;em&gt;handles&lt;/em&gt; which is widely available on UNIX and Windows platforms.&lt;/li&gt;
  &lt;li&gt;Check for dead connections. If an inactivity timeout was set and enough time has passed by without getting any information, the connection will be closed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prior to each these three actions EventMachine’s reactor checks whether the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stop&lt;/code&gt; method has been called. If it wasn’t called the event loop continues otherwise the loop is broken&lt;sup id=&quot;fnref:C1_07&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_07&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; and the reactor exits the event demultiplexing phase.&lt;/p&gt;

&lt;p&gt;Putting all this together the loop’s pseudocode would look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;loop do
  break if stop_was_called?
  trigger_time_based_events

  break if stop_was_called?
  select_handles

  break if stop_was_called?
  close_dead_connections
end&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;revisiting-the-hello-world-example&quot;&gt;Revisiting the &lt;em&gt;hello world&lt;/em&gt; example&lt;/h3&gt;
&lt;p&gt;That’s all you need to know about the reactor and the event loop right now. Obviously there are a lot of subtleties on how EventMachine works but the basics are covered. This brief explanation will make a huge difference when you try to understand how the library primitives work out.&lt;/p&gt;

&lt;p&gt;In fact, it will already pay of. With our current level of understanding of how EventMachine works we can explain the behaviour of the second version of our &lt;em&gt;hello world&lt;/em&gt; application. Let me refresh your memory:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EM.run do
  puts &quot;starting EventMachine at \#{format_as_hhmmss(Time.now)}&quot;
  EM.add_timer(0) { puts &quot;Hello world&quot; }
  EM.stop
  puts &quot;stopping EventMachine at \#{format_as_hhmmss(Time.now)}&quot;
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We got both messages as output even though we were calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.stop&lt;/code&gt; before printing the second message. We also didn’t get the &lt;em&gt;hello world&lt;/em&gt; message despite it should be printed in response to 0 seconds passing by:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;starting EventMachine at 17:17:37
stopping EventMachine at 17:17:37
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lets think carefully on what’s going on. All the code we have is inside the block we are passing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.run&lt;/code&gt;. That means it will be executed before the reactor enters the &lt;em&gt;event demultiplexing phase&lt;/em&gt;. Before the event loop is started both messages will be printed, an event handler for the event &lt;em&gt;0 seconds passing by&lt;/em&gt; is set up and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.stop&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;When the loop starts, the first thing the reactor will do is checking if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stop&lt;/code&gt; has been called, even before checking if there are timer events to be triggered. It will find that the stop has been scheduled and break the loop immediately. The reactor will check the timers not even once.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:C1_06&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Quite literally because it uses Ruby’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt; construct &lt;a href=&quot;#fnref:C1_06&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:C1_05&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Latest versions of EventMachine can also use a better mechanism where available called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;epoll&lt;/code&gt; &lt;a href=&quot;#fnref:C1_05&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:C1_07&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Again, literally calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;break&lt;/code&gt; &lt;a href=&quot;#fnref:C1_07&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">The event loop and reactor pattern are the two patterns EventMachine is powered by . Understanding them is key to write successful EventMachine applications.</summary></entry><entry xml:lang="en"><title type="html">Starting with EventMachine II</title><link href="https://javieracero.com/blog/starting-with-eventmachine-ii/" rel="alternate" type="text/html" title="Starting with EventMachine II" /><published>2013-03-25T00:00:00+00:00</published><updated>2013-03-25T00:00:00+00:00</updated><id>https://javieracero.com/blog/starting-with-eventmachine-ii</id><content type="html" xml:base="https://javieracero.com/blog/starting-with-eventmachine-ii/">&lt;p&gt;This blog post is an extract of an ebook about EventMachine that I started
writing some time ago. Somehow it ended up half written in some dark corner
of my hard drive. I have decided to publish the material I have as series of
posts. &lt;strong&gt;Please let me know if you find the material useful.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can find all posts here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-i&quot;&gt;Starting with EventMachine I&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-ii&quot;&gt;Starting with EventMachine II&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iii&quot;&gt;Starting with EventMachine III&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iv&quot;&gt;Starting with EventMachine IV&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Despite being a pretty simple six line example, in
&lt;a href=&quot;/blog/starting-with-eventmachine-i&quot;&gt;the last post&lt;/a&gt; you could get the
feeling of how building an EventMachine application will be. Evented code
has certain characteristics that make it different from the plain old
sequential code that we are used to write.&lt;/p&gt;

&lt;h2 id=&quot;getting-the-feeling-of-evented-code&quot;&gt;Getting the feeling of evented code&lt;/h2&gt;
&lt;h3 id=&quot;the-mind-shift&quot;&gt;The mind shift&lt;/h3&gt;
&lt;p&gt;In the procedural world all code is executed in the order it appears in our files.&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;response1 = Net::HTTP.get_response(uri1)
puts response1.code
puts response1.body

response2 = Net::HTTP.get_response(uri2)
puts response2.code
puts response2.body

response3 = Net::HTTP.get_response(uri3)
puts response3.code
puts response3.body&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code will first perform the first HTTP request. Once the response comes back it will print out the status code and, once that has been printed, it will output the response body. It will repeat this for requests 2 and 3. When this code is run it will follow the exact order the code does.&lt;/p&gt;

&lt;p&gt;However, in the land of events that no longer applies. Our code runs in response to things happening.&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;request1 = EM::MadeUpHTTP.get(uri1)
request1.callback do |response1|
  puts response1.code
  puts response1.body
end

request2 = EM::MadeUpHTTP.get(uri2)
request2.callback do |response2|
  puts response2.code
  puts response2.body
end

request3 = EM::MadeUpHTTP.get(uri3)
request3.callback do |response|
  puts response3.code
  puts response3.body
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The output of this code can’t be predicted. You won’t know which response will arrive first so you can’t know the order in which callbacks will be run either. The responses can arrive in any order. It can be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;response1&lt;/code&gt; the one that comes first but it could also be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;response2&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;response3&lt;/code&gt;. It has nothing to do with the flow of the code nor the order in which the requests were made. The callbacks will be executed asynchronously. &lt;strong&gt;The only thing that remains synchronous is the execution of the code inside the callbacks&lt;/strong&gt;. That’s the only place where the rules of the procedural world still apply.&lt;/p&gt;

&lt;p&gt;It will require a mind shift but you will need to start structuring your code around responding to events instead of around performing a sequence of actions. &lt;strong&gt;The flow of the program depends on the flow of events not on the flow of the code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You will also need to understand the ecosystem where your code is running: check out this alternate version of the hello world example. It uses the same constructs as the previous one with minor changes: the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.stop&lt;/code&gt; call has been taken out of the timer callback and we are now handing the event &lt;em&gt;zero seconds passing by&lt;/em&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EM.run do
  puts &quot;starting EventMachine at \#{format_as_hhmmss(Time.now)}&quot;
  EM.add_timer(0) { puts &quot;Hello world&quot; }
  EM.stop
  puts &quot;stopping EventMachine at \#{format_as_hhmmss(Time.now)}&quot;
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Even if the call to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.add_timer&lt;/code&gt; method appears before the one to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.stop&lt;/code&gt;, and even if we are waiting for 0 seconds to pass by, the code in the callback will never be executed. You will also see the second message even if we are stopping EventMachine in the previous line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;starting EventMachine at 17:17:37
stopping EventMachine at 17:17:37
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case we are not letting the event loop start handling events: we are stopping it right in the setup phase. This is the reason why we were calling  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.stop &lt;/code&gt; inside the callback in the first example: to make sure that EventMachine waits for the event to be triggered and handled.&lt;/p&gt;

&lt;h3 id=&quot;block-nightmare&quot;&gt;Block nightmare&lt;/h3&gt;
&lt;p&gt;EventMachine relies heavily on Ruby blocks for callback setting. Most of the times callbacks trigger new events that will also need to be handled.  That means that we can end up with a lot of block nesting and that can make the code really hard to understand.&lt;/p&gt;

&lt;p&gt;Let’s take what a web application usually does as an example: it will receive a request through a certain route, get some information out of the database and render some HTML that will be returned in the response.&lt;/p&gt;

&lt;p&gt;Let’s shape that into a made-up EventMachine-based web framework:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EM.run do
  # other routes...
  on_request(&apos;/user/:id&apos;) do |params|
    query = prepare_query(params)
    query_db(query) do |query_results|
      user = process(query_results)
      render(&apos;profile.haml&apos;, locals: { user: user }) do |html|
        respond_with status: 200, body: html
      end
    end
  end
  # even more routes...
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I know what you are thinking: that code smells pretty badly!&lt;/p&gt;

&lt;p&gt;If you have read Robert C. Martin’s Clean Code&lt;sup id=&quot;fnref:C1_1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; you already know that having more than one indentation level should be avoided. We have four indentation levels in nine lines of code, shaping our program into that horrible pyramid.&lt;/p&gt;

&lt;p&gt;Besides that you can easily tell that this code breaks several
OO principles. And, to tell you the truth, I don’t think it is object oriented at all.&lt;/p&gt;

&lt;p&gt;Of course, evented code does not have to be like this. There are several patterns you can apply to design object oriented EventMachine applications. You can also take advantage of the Proc building capabilities Ruby has to make it look even more like so. And the Ruby fibers that were introduced in 1.9 can also be helpful getting your program’s API to look more synchronous.&lt;/p&gt;

&lt;h3 id=&quot;testing&quot;&gt;Testing&lt;/h3&gt;
&lt;p&gt;We should test all the code we write. In fact we shouldn’t write any production code without a having failing test&lt;sup id=&quot;fnref:C1_02&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_02&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; first. If this statement is true in the procedural world it is even more important in the land of events. However, testing event oriented code can be really hard. Could you imagine the test setup that would be required to test our web application example? The amount of stubbing it would require to be run?&lt;/p&gt;

&lt;p&gt;But you will find other problems testing EventMachine applications even with code as simple as our &lt;em&gt;hello world&lt;/em&gt; example. Let’s give it a try (removing the noise of the start and stop messages):&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;def evented_hello_world
  EM.run do
    EM.add_timer(2) do
      puts &quot;Hello world&quot;
      EM.stop
    end
  end
end

describe &apos;evented hello world&apos; do
  it &apos;prints out hello world after two seconds&apos; do
    start_time = Time.now
    STDOUT.should_receive(:puts).with(&apos;Hello world&apos;)
    evented_hello_world
    finish_time = Time.now
    (finish_time - start_time).should be_between(1.95, 2.15)
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Lets run the test:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;evented hello world
  prints out hello world after two seconds

Finished in 2.1 seconds
  1 example, 0 failures
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This single spec takes more than two seconds to pass. Can you imagine how long it would take to run the test suite of a whole app?&lt;sup id=&quot;fnref:C1_03&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:C1_03&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; Imagine an application with a few &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.add_timer(60)&lt;/code&gt; and everyone will agree that this kind of testing is a no go… We can do better.&lt;/p&gt;

&lt;p&gt;You may be wondering if there are specific tools to test evented applications. There are several gems designed to test EventMachine applications specifically. The thing is that they are not needed. At the end of the day you are writing Ruby code so you should be able to test evented code with the tools you are already familiar with.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:C1_1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin) &lt;a href=&quot;#fnref:C1_1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:C1_02&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;One of the three laws of Test-Driven Development &lt;a href=&quot;#fnref:C1_02&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:C1_03&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Yes, more or less the same it takes a Rails test suite to run. &lt;a href=&quot;#fnref:C1_03&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">Writing evented code has implications that may not be explicit at first look. There are 3 aspects that will be affected: flow, program structure and testing.</summary></entry><entry xml:lang="en"><title type="html">Starting with EventMachine I</title><link href="https://javieracero.com/blog/starting-with-eventmachine-i/" rel="alternate" type="text/html" title="Starting with EventMachine I" /><published>2013-03-13T00:00:00+00:00</published><updated>2013-03-13T00:00:00+00:00</updated><id>https://javieracero.com/blog/starting-with-eventmachine-i</id><content type="html" xml:base="https://javieracero.com/blog/starting-with-eventmachine-i/">&lt;p&gt;This blog post is an extract of an ebook about EventMachine that I started
writing some time ago. Somehow it ended up half written in some dark corner
of my hard drive. I have decided to publish the material I have as series of
posts. &lt;strong&gt;Please let me know if you find the material useful.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can all the posts here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-i&quot;&gt;Starting with EventMachine I&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-ii&quot;&gt;Starting with EventMachine II&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iii&quot;&gt;Starting with EventMachine III&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/blog/starting-with-eventmachine-iv&quot;&gt;Starting with EventMachine IV&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;whats-eventmachine&quot;&gt;What’s EventMachine&lt;/h2&gt;
&lt;p&gt;What’s EventMachine you may be wondering? EventMachine is an event-driven I/O and lightweight concurrency library for Ruby. It provides an API to handle many different protocols and the ones that are not distributed with it can be found as gems.&lt;/p&gt;

&lt;h3 id=&quot;hello-evented-world&quot;&gt;Hello evented world&lt;/h3&gt;
&lt;p&gt;Many blog posts and talks about EventMachine use an &lt;a href=&quot;http://en.wikipedia.org/wiki/Echo_Protocol&quot;&gt;echo server and client&lt;/a&gt; to show how the library works. Is kind of the &lt;em&gt;hello world&lt;/em&gt; version of libraries that deal with concurrency. Even though it’s a great example of how easy it is to create servers and connect to them with EventMachine I don’t think it’s a good way of getting started.&lt;/p&gt;

&lt;p&gt;We are going to start simple. Our first program will be the closest it can get to the usual &lt;em&gt;hello world&lt;/em&gt; but with an evented flavour. We will:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Start EventMachine.&lt;/li&gt;
  &lt;li&gt;Create a time based event that will trigger after 2 seconds.&lt;/li&gt;
  &lt;li&gt;Create a callback that handles that event which will print the message &lt;em&gt;“Hello world”&lt;/em&gt; and stop EventMachine.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first thing you need to do to get started is installing EventMachine. This one will be easy. It’s distributed as a gem so you can install it by running:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem install eventmachine
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You will need to require it in order to use it in your program:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;require &apos;eventmachine&apos;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once you have done that you are set to write your first EventMachine powered application: an event powered &lt;em&gt;hello world!&lt;/em&gt; (the time formatting has been omitted for simplicity):&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EventMachine.run do
  puts &quot;starting EventMachine at \#{format_as_hhmmss(Time.now)}&quot;
  EM.add_timer(2) do
    puts &quot;Hello world&quot;
    puts &quot;stopping EventMachine at \#{format_as_hhmmss(Time.now)}&quot;
    EM.stop
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You may have noticed that I have used both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EventMachine&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM&lt;/code&gt;. In the library codebase you can find this line:&lt;/p&gt;

&lt;pre&gt;&lt;code data-language=&quot;ruby&quot;&gt;EM = EventMachine&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Both modules are the same thing. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM&lt;/code&gt; is just an alias created for convenience so we don’t need to type the whole name every single time.&lt;/p&gt;

&lt;p&gt;When you run this code the first thing you will see will be the time EventMachine was started at. Two seconds later the &lt;em&gt;Hello world&lt;/em&gt; will be printed out and, almost simultaneously, the stopping message:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;starting EventMachine at 10:35:56
Hello world
stopping EventMachine at 10:35:58
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;what-does-emrun-do&quot;&gt;What does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.run&lt;/code&gt; do?&lt;/h3&gt;
&lt;p&gt;The method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run&lt;/code&gt; starts EventMachine’s event loop. Don’t worry if you have no idea of what an &lt;em&gt;event loop&lt;/em&gt; is: you you will by the end of this chapter. Once started it traps the thread it runs in, so the code won’t continue running until it’s stopped. For now you can think of it being a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while(true)&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.run&lt;/code&gt; takes a block. The block will be yielded &lt;strong&gt;right before&lt;/strong&gt; the event loop is booted. That means this is the place to setup everything your application may need.&lt;/p&gt;

&lt;h3 id=&quot;what-does-emstop-do&quot;&gt;What does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.stop&lt;/code&gt; do?&lt;/h3&gt;
&lt;p&gt;As you may have figured out, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stop&lt;/code&gt; method halts the event loop and returns the control to the thread that started it.&lt;/p&gt;

&lt;p&gt;Calling this method will stop EventMachine almost immediately (we will learn what &lt;em&gt;almost immediately&lt;/em&gt; means when we dig deeper into how the event loop works). It will wait for no event to be triggered and no callback to be run. That means we need to be careful with when and how we stop EventMachine.&lt;/p&gt;

&lt;h3 id=&quot;what-does-emadd_timer-do&quot;&gt;What does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.add_timer&lt;/code&gt; do?&lt;/h3&gt;
&lt;p&gt;By calling this method you are setting up your first event handler or callback. You are telling EventMachine to watch for an event: &lt;em&gt;two seconds passing by&lt;/em&gt;. Once that event is triggered (once two seconds pass by), EventMachine will run the block you passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EM.add_timer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is where you can see the &lt;a href=&quot;http://en.wikipedia.org/wiki/Hollywood_principle&quot;&gt;&lt;em&gt;Hollywood Principle&lt;/em&gt;&lt;/a&gt; applied. Usually you call other application’s code. Whether they are gems, frameworks or APIs it is your code who calls them. In this case it will be the other way around: the library will be the one calling the code you wrote. It’s a very common pattern to deal with asynchrony.&lt;/p&gt;</content><author><name></name></author><summary type="html">Earlier this year I wrote a couple ruby programs using EventMachine. I found it hard to start with it and the documentation or tutorials scarce. Let&apos;s fix that.</summary></entry><entry xml:lang="en"><title type="html">Internal vs. External DSL</title><link href="https://javieracero.com/blog/internal-vs-external-dsl/" rel="alternate" type="text/html" title="Internal vs. External DSL" /><published>2012-01-23T00:00:00+00:00</published><updated>2012-01-23T00:00:00+00:00</updated><id>https://javieracero.com/blog/internal-vs-external-dsl</id><content type="html" xml:base="https://javieracero.com/blog/internal-vs-external-dsl/">&lt;p&gt;For those of you who have never heard about it, DSL stands for &lt;em&gt;Domain Specific Language&lt;/em&gt;. Basically it’s &lt;em&gt;a language which is built for a specific domain&lt;/em&gt; (hence the name). They are pretty common in the computer world, and you may be using a few without even knowing it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Domain-specific language: a computer programming language of limited expressiveness focused on a particular domain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href=&quot;http://rspec.info/&quot;&gt;RSpec&lt;/a&gt; or &lt;a href=&quot;http://rake.rubyforge.org/&quot;&gt;Rake&lt;/a&gt; Rake are clear examples of DSLs. They use all the power of the Ruby language to make you feel like using a language that was designed with a particular problem in mind.&lt;/p&gt;

&lt;p&gt;This kind of DSL that bends and twists a host language to make it feel like a different one is known as &lt;em&gt;Internal DSL&lt;/em&gt; and is the only kind of DSL I was aware of.&lt;/p&gt;

&lt;p&gt;My current apprentice task involves building a DSL that is 100% in Spanish. Ruby is so flexible that you can do such a thing. But I also have very strong syntax constraints. That’s why I have been struggling for a long time trying to build it an &lt;em&gt;Internal DSL&lt;/em&gt; to solve this particular problem.&lt;/p&gt;

&lt;p&gt;Today (after my mentor asked me to write this post) I have learned that &lt;em&gt;there is another type of Domain-specific Languages: the external ones&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It turns out that Cucumber is an external DSL. But also CSS, Sass and many other technologies that I would have never considered a Domain Specific Language, are such a thing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;External DSLs&lt;/em&gt; have their own syntax instead of being built on top of a language. All you need to make it work is a parser that interprets the language or that translates it to another one.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://martinfowler.com/bliki/DomainSpecificLanguage.html&quot;&gt;This post&lt;/a&gt; by Martin Fowler has been really helpful to understand the whole thing. Now I know how to complete my task.&lt;/p&gt;</content><author><name></name></author><summary type="html">Lately I&apos;ve been struggling with one of my apprentice tasks because I only knew about Internal DSLs. It turns out that there is another kind: external DSLs.</summary></entry></feed>