<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Carl Vuorinen</title>
    <description>The latest articles on DEV Community by Carl Vuorinen (@cvuorinen).</description>
    <link>https://dev.to/cvuorinen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F18737%2F11a64f32-0616-49c9-b25c-9911a3a8e435.jpg</url>
      <title>DEV Community: Carl Vuorinen</title>
      <link>https://dev.to/cvuorinen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cvuorinen"/>
    <language>en</language>
    <item>
      <title>What is Clean Code and why should you care?</title>
      <dc:creator>Carl Vuorinen</dc:creator>
      <pubDate>Thu, 11 May 2017 16:10:52 +0000</pubDate>
      <link>https://dev.to/cvuorinen/what-is-clean-code-and-why-should-you-care</link>
      <guid>https://dev.to/cvuorinen/what-is-clean-code-and-why-should-you-care</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally posted on &lt;a href="http://cvuorinen.net/2014/04/what-is-clean-code-and-why-should-you-care/"&gt;my blog at cvuorinen.net&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Clean Code?
&lt;/h2&gt;

&lt;p&gt;Clean code is subjective and every developer has a personal take on it. There are some ideas that are considered best practice and what constitutes as clean code within the industry and community, but there is no definitive distinction. And I don't think there ever will be.&lt;/p&gt;

&lt;p&gt;After reading a few books on the topic, giving it some thought and delivering a couple of talks on the subject, if I had to summarize what clean code means in one sentence, I would say that for me:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Clean code is code that is easy to understand and easy to change.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, that sounds nice, but what does it really mean? Let's break that sentence apart and examine the individual points behind it.&lt;/p&gt;

&lt;p&gt;Easy to understand means the code is easy to read, whether that reader is the original author of the code or somebody else. It's meaning is clear so it minimizes the need for guesswork and possibility for misunderstandings. It is easy to understand on every level, specifically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is easy to understand the execution flow of the entire application&lt;/li&gt;
&lt;li&gt;It is easy to understand how the different objects collaborate with each other&lt;/li&gt;
&lt;li&gt;It is easy to understand the role and responsibility of each class&lt;/li&gt;
&lt;li&gt;It is easy to understand what each method does&lt;/li&gt;
&lt;li&gt;It is easy to understand what is the purpose of each expression and variable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Easy to change means the code is easy to extend and refactor, and it's easy to fix bugs in the codebase. This can be achieved if the person making the changes understands the code and also feels confident that the changes introduced in the code do not break any existing functionality. For the code to be easy to change:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Classes and methods are small and only have single responsibility&lt;/li&gt;
&lt;li&gt;Classes have clear and concise public APIs&lt;/li&gt;
&lt;li&gt;Classes and methods are predictable and work as expected&lt;/li&gt;
&lt;li&gt;The code is easily testable and has unit tests (or it is easy to write the tests)&lt;/li&gt;
&lt;li&gt;Tests are easy to understand and easy to change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I plan to write more posts later that cover these topics in more detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should you care about Clean Code?
&lt;/h2&gt;

&lt;p&gt;As Robert C. Martin stated in his book &lt;a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882"&gt;&lt;em&gt;Clean Code: A Handbook of Agile Software Craftsmanship&lt;/em&gt;&lt;/a&gt;, "Clean code is code that has been taken care of. Someone has taken the time to keep it simple and orderly. They have paid appropriate attention to details. They have cared." But why should you care? What's wrong with code that just works?&lt;/p&gt;

&lt;p&gt;You should care because code is (almost) never written just once and then forgotten. Most of the time you, or someone else, need to work on the code. And to be able to work on it efficiently you need to understand the code.&lt;/p&gt;

&lt;p&gt;And because people need to understand the code we write, we can say that the code we write is not intended only for the computer but also for humans.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Programming is the art of telling another human what one wants the computer to do.&lt;br&gt;
– Donald Knuth&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you write clean code, then you are helping your future self and your co-workers. You are reducing the cost of maintenance of the application you are writing. You are making it easier to estimate the time needed for new features. You are making it easier to fix bugs. You are making it more enjoyable to work on the code for many years to come. Essentially you are making the life easier for everyone involved in the project.&lt;/p&gt;

&lt;p&gt;Now, I'm not saying you should get obsessed about clean code. Your code needs to provide value, so you can't spend countless hours making it perfect. Clean code usually doesn't happen on first try anyway, so you need to adopt a mindset that you will always strive to improve the code you are working on. You then need to decide when it is good enough and move on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post I have tried to explain what clean code means to me and also hopefully convinced you that you should also care about clean code (in case you didn't previously).&lt;/p&gt;

&lt;p&gt;To close things up, I would like to share a few more of my favourite quotes and tweets I have come across lately.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want your code to be easy to write, make it easy to read&lt;br&gt;
– Robert C. Martin&lt;/p&gt;

&lt;p&gt;Clean code always looks like it was written by someone who cares. There is nothing obvious you can do to make it better.&lt;br&gt;
– Michael Feathers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote class="twitter-tweet"&gt;Beautiful code, like beautiful prose, is the result of many small decisions. The right method length here, the right object name there.

– DHH (@dhh) &lt;a href="https://twitter.com/dhh/statuses/447042824622850048"&gt;March 21, 2014&lt;/a&gt;
&lt;/blockquote&gt;

&lt;blockquote class="twitter-tweet"&gt;"Code is like humor. When you *have* to explain it, it’s bad" - &lt;a href="https://twitter.com/housecor"&gt;@housecor&lt;/a&gt;

– About Programming (@abt_programming) &lt;a href="https://twitter.com/abt_programming/statuses/448101448564629504"&gt;March 24, 2014&lt;/a&gt;
&lt;/blockquote&gt;

&lt;blockquote class="twitter-tweet"&gt;“The cheapest time to refactor code is right now.” Great advices by &lt;a href="https://twitter.com/bugroll"&gt;@bugroll&lt;/a&gt; &lt;a href="http://t.co/GGEQ8Bc3e1"&gt;http://t.co/GGEQ8Bc3e1&lt;/a&gt; &lt;a href="https://twitter.com/search?q=%23refactoring&amp;amp;src=hash"&gt;#refactoring&lt;/a&gt; &lt;a href="https://twitter.com/search?q=%23cleancode&amp;amp;src=hash"&gt;#cleancode&lt;/a&gt;

– Luca Guidi (@jodosha) &lt;a href="https://twitter.com/jodosha/statuses/446683743907237888"&gt;March 20, 2014&lt;/a&gt;
&lt;/blockquote&gt;

&lt;blockquote class="twitter-tweet"&gt;The cost of ownership for a program includes the time humans spend to understand it. Humans are costly, so optimize for understandability.

– Mathias Verraes (@mathiasverraes) &lt;a href="https://twitter.com/mathiasverraes/statuses/457239755785506816"&gt;April 18, 2014&lt;/a&gt;
&lt;/blockquote&gt;

&lt;blockquote class="twitter-tweet"&gt;Dev time = 60% reading and 40% writing. Code refactor can reduce reading by 20%, improving 8 hours/week, or 416 hours/year. &lt;a href="https://twitter.com/search?q=%23refactor&amp;amp;src=hash"&gt;#refactor&lt;/a&gt;

– Refactoring 101 (@refactoring101) &lt;a href="https://twitter.com/refactoring101/statuses/453282385027534848"&gt;April 7, 2014&lt;/a&gt;
&lt;/blockquote&gt;

</description>
      <category>cleancode</category>
    </item>
    <item>
      <title>Running Angular tests in headless Chrome</title>
      <dc:creator>Carl Vuorinen</dc:creator>
      <pubDate>Fri, 05 May 2017 00:00:00 +0000</pubDate>
      <link>https://dev.to/cvuorinen/running-angular-tests-in-headless-chrome-9h9</link>
      <guid>https://dev.to/cvuorinen/running-angular-tests-in-headless-chrome-9h9</guid>
      <description>&lt;p&gt;Angular has some great tooling for running tests, namely Karma and Protractor. By default (at least when using Angular CLI) they run using Chrome. So when you execute the tests from command-line, it will pop open a browser window where the tests execute. This works well enough, but sometimes you either don’t want to see that browser window pop open or you are running the tests in an environment where there is no graphical environment (on a CI server or a Docker container for example).&lt;/p&gt;

&lt;p&gt;There is nothing new in running Karma tests without a browser window, you have been able to do it with PhantomJS by installing the &lt;code&gt;karma-phantomjs-launcher&lt;/code&gt;. PhantomJS has been good enough solution for this, but you might encounter some issues every now and then and need to add some additional polyfills etc. But Chrome now has the ability to run in &lt;a href="https://www.chromestatus.com/feature/5678767817097216"&gt;headless mode since version 59&lt;/a&gt;, so you can use it to run tests without needing to install any additional packages and with a more standard environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conf your Karma
&lt;/h2&gt;

&lt;p&gt;Whether you have a Karma config generated with Angular CLI or one that you have created manually, you can use a config option called &lt;strong&gt;customLaunchers&lt;/strong&gt; to create a new launcher based on an existing one by defining additional flags for it. This works equally with older AngularJS v1.x projects as well as newer Angular v2.x-4.x projects. To use Chrome in headless mode, you need to add the following section to your &lt;code&gt;karma.conf.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customLaunchers: {
  ChromeHeadless: {
    base: 'Chrome',
    flags: [
      '--headless',
      '--disable-gpu',
      // Without a remote debugging port, Google Chrome exits immediately.
      '--remote-debugging-port=9222',
    ],
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note, depending on your Chrome version, the &lt;code&gt;--disable-gpu&lt;/code&gt; flag might not be needed.&lt;/p&gt;

&lt;p&gt;Then you can replace whatever you had in the &lt;code&gt;browsers&lt;/code&gt; section (either ‘Chrome’ or ‘PhantomJS’ etc.) with &lt;code&gt;ChromeHeadless&lt;/code&gt;. That’s it, after that you can enjoy running your tests without any browser window popping up.&lt;/p&gt;

&lt;h2&gt;
  
  
  E2E tests with Protractor
&lt;/h2&gt;

&lt;p&gt;Running E2E tests in headless mode has been a bit more difficult, since it has not worked very nicely with PhantomJS. Basically your only option has been to run Chrome in Xvfb (that’s X virtual framebuffer in case you were wondering). But now it’s as simple as &lt;a href="https://github.com/angular/protractor/blob/master/docs/browser-setup.md#using-headless-chrome"&gt;adding a few lines to your &lt;code&gt;protractor.conf.js&lt;/code&gt;&lt;/a&gt; to also run your E2E tests in headless mode. You need to add the following options under the &lt;code&gt;capabilities&lt;/code&gt; key (where you should already have &lt;code&gt;browserName: 'chrome'&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chromeOptions: {
  args: ["--headless", "--disable-gpu", "--window-size=800x600"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See &lt;a href="https://gist.github.com/cvuorinen/543c6f72f8ec917ebfd596802d387aa3"&gt;this Gist&lt;/a&gt; for full examples of both &lt;code&gt;karma.conf.js&lt;/code&gt; and &lt;code&gt;protractor.conf.js&lt;/code&gt; (both files generated by Angular CLI with only the changes described above).&lt;/p&gt;

&lt;h2&gt;
  
  
  Go forth and test all the things
&lt;/h2&gt;

&lt;p&gt;Now you can just sit back and enjoy running all your tests without any distracting browser windows, or more importantly you can run them in your Continuous Integration server like Travis CI or Jenkins etc.&lt;/p&gt;

&lt;p&gt;For more information, see &lt;a href="https://developers.google.com/web/updates/2017/04/headless-chrome"&gt;Getting Started with Headless Chrome&lt;/a&gt; on Google Developers.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
