<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Rafal Spacjer blog]]></title>
  <link href="http://www.spacjer.com//atom.xml" rel="self"/>
  <link href="http://www.spacjer.com//"/>
  <updated>2015-10-01T21:27:01+02:00</updated>
  <id>http://www.spacjer.com//</id>
  <author>
    <name><![CDATA[Rafal Spacjer]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[NDepend 6]]></title>
    <link href="http://www.spacjer.com//blog/2015/10/01/ndepend-6/"/>
    <updated>2015-10-01T21:13:07+02:00</updated>
    <id>http://www.spacjer.com//blog/2015/10/01/ndepend-6</id>
    <content type="html"><![CDATA[<p>Again I get from Patrick Smacchia an opportunity to test new version of <a href="http://www.ndepend.com/"><em>NDepend</em></a> &ndash; this time version 6. If you don&rsquo;t know, NDepend is a tool that measure and present information about code metrics in your <code>.Net</code> code. It can run as standalone application, but also as Visual Studio plugin.</p>

<p>In this article I will only focus on elements that are new in comparison to the previous versions. You can read my earlier description <a href="http://www.spacjer.com/blog/2013/10/31/ndepend-5/">here</a> and <a href="http://www.spacjer.com/blog/2013/08/11/ndepend/">here</a>.</p>

<h2>Performance</h2>

<p>I usually work with NDepend in Visual Studio. The tool integrates with VS flawless, it feels as part of the IDE. There was only one problem, because of the complex nature of calculating metrics, the tool wasn&rsquo;t very fast. Occasionally it could also hang. Fortunately this is changed in the new release. Now the whole process of analyzing solution is much faster and I have feeling that the plugin is more stable. After installing version 6 I&rsquo;ve never experienced a crash.</p>

<h2>Better treemap</h2>

<p>One of the &ldquo;killer feature&rdquo; of NDepend is treemap of the source code. In the previous versions it shows graphic representation based on one of the selected metrics. This is very nice, because you can easily spot problematic places &ndash; usually big rectangles on the map. Sometimes this can be misleading. Imagine situation when you look at treemap of lines of codes per method. You see a big figure and you start wondering what wrong is with this method. How big is your surprise when you examine it and discover that the code in this method only add some constants to the dictionary. Of course such method isn&rsquo;t big deal and can safely be in your code. Thankfully with the new feature of NDepend, such situations can easily be eliminated. Now you can add another metrics to the view &ndash; every rectangle will be filled with additional color that indicate another metric. To solve a problem description above, we can add code complexity metric. This will point us to the code that we should refactor.</p>

<p><a href='http://www.spacjer.com//images/posts/2015/10/ndepend3.jpg' rel='lightbox[ndepend6]'><img class='center' src='http://www.spacjer.com//images/posts/2015/10/ndepend3_small.jpg' alt='Treemap'></a></p>

<h2>Documentation</h2>

<p>In my former overview of NDepend I haven&rsquo;t write much about its documentation. When you start working with NDepend all those queries, matrices, graphs and maps can be overwhelming &ndash; especially when you don&rsquo;t know (and understand) all the metric and how they describe your code. Thankfully almost every element is well documented. Without trouble you will find info about metrics, how they are calculated and what they mean (i.e. what value means bad code). This info is available as context pop-ups in the GUI, but also on the HTML pages. A big plus for that!</p>

<p>Version 6 extend this with detailed description of rules (i.e. &ldquo;Types too big &ndash; critical&rdquo;) and how you can fix it. If you want you can extend those description by yourself.</p>

<p><a href='http://www.spacjer.com//images/posts/2015/10/ndepend2.jpg' rel='lightbox[ndepend6]'><img class='center' src='http://www.spacjer.com//images/posts/2015/10/ndepend2_small.jpg' alt='rules'></a></p>

<h2>Themes</h2>

<p>Previous version of NDepend introduced huge changes in the look and feel of the GUI. In current version the changes aren&rsquo;t so big. There are some small tweaks here and there. As a bigger improvement we can treat possibility of setting different colored themes (light, dark, blue and others) &ndash; maybe nothing very important, but it nice to have such option.</p>

<p><a href='http://www.spacjer.com//images/posts/2015/10/ndepend1.jpg' rel='lightbox[ndepend6]'><img class='center' src='http://www.spacjer.com//images/posts/2015/10/ndepend1_small.jpg' alt='matrix'></a></p>

<h2>Conclusion</h2>

<p>Probably you would like to hear from me if it&rsquo;s worth buying new version of NDepend? As usual, it depends ;)</p>

<p>If you work with NDepend every day with many projects then I think you should. You will be happy with the speed and stability of the tool. New features will be additional plus.</p>

<p>If you use it occasionally and only some subset of all its options then probably you can think twice before spending money.</p>

<p>If you haven&rsquo;t use it before and you want to improve code quality of your .Net projects, then it&rsquo;s good time to buy it.</p>

<p><em>Note: On the screenshots you can see results of analyzing <a href="https://github.com/apache/lucenenet">Lucene.Net</a> source code.</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[DevDay 2015]]></title>
    <link href="http://www.spacjer.com//blog/2015/09/23/devday-2015/"/>
    <updated>2015-09-23T21:19:35+02:00</updated>
    <id>http://www.spacjer.com//blog/2015/09/23/devday-2015</id>
    <content type="html"><![CDATA[<p>September has come and as usual at this time of the year I went to Krakow (Poland), to attend DevDay. Since the last year, the conference changed a bit &ndash; it&rsquo;s not a free one anymore (but the cost is very reasonable) and it takes two days now. Expect that changes everything else is the same &ndash; <a href="http://www.devday.pl/sessions.html">great sessions</a> with superior service around them (lots of good food, free drinks, helpful crew).</p>

<p>During those two days, I&rsquo;ve attended 14 sessions about various subject. I will shortly write my feelings about each of them.</p>

<h3>Day one</h3>

<p><strong>&ldquo;The Passionate Programmer: 10 years later&rdquo;</strong> by <a href="http://chadfowler.com/">Chad Fowler</a></p>

<p>There is a good chance, that you&rsquo;ve heard about Chad Fowler. If not, then I will only say that he is the author of <a href="https://pragprog.com/titles/cfcar2/the-passionate-programmer">&ldquo;The Passionate Programmer&rdquo; book</a> (and many others) and currently works as CTO in a company (bought by Microsoft recently) that created <a href="https://www.wunderlist.com/">Wunderlist</a> app. This short introduction should be a good taste of the quality of the speech he gave. In 45 minutes talk he told a story about his career and what other can learn from it. After the talk I was motivated and full of the energy to do more as a developer. Definitely one of the best session I saw during this year DevDay.</p>

<p><strong>&ldquo;The T in TDD: Tests, Types, Tales&rdquo;</strong> by <a href="http://www.clear-lines.com/blog/">Mathias Brandewinder</a></p>

<p>I&rsquo;m not in the &ldquo;F#&rdquo; business, but still I enjoy hearing about it (still my choice for the functional language is Clojure as I have a love affair with the lisp syntax). Mathias showed how you can use rich type system of F# to simplify your code (in comparison to C#). This wasn&rsquo;t anything new for me, but if you looking for some introduction to F# then you probably enjoy it.</p>

<p><strong>&ldquo;Introduction to Aurelia&rdquo;</strong> by <a href="http://odetocode.com/">Scott Allen</a></p>

<p>I went to this presentation, because I wanted to see Scott Allen (a blog writer, podcaster, screencasts creator) in &ldquo;action&rdquo;. He talked about JavaScript framework called <a href="http://aurelia.io/">Aurelia</a>. As interesting this framework looks I&rsquo;m not a fan of it. I prefer simpler and more declarative solutions (like Backbone or React). If I ignore the subject and focus only on the way Scott presented it, I can definitely say he is a good speaker.</p>

<p><strong>&ldquo;The Wonderful World That Is Twitter Bootstrap&rdquo;</strong> by <a href="http://ironshay.com/">Shay Friedman</a></p>

<p>I went to this talk, because I thought that I learn something new (and tricky) about <a href="http://getbootstrap.com/">Twitter Bootstrap css framework</a>. Unfortunately this was beginner friendly talk, that describes every element of the framework. For me this wasn&rsquo;t anything new and after 30 minutes I went out of the room. If you haven&rsquo;t use it and want to hear overview of main features, then this talk is for you.</p>

<p><strong>&ldquo;Rethinking NoSQL: Four and a Half Reasons Why RethinkDB Is Worth a Look&rdquo;</strong> by <a href="http://rob.conery.io/">Rob Conery</a></p>

<p>I admire Rob Conery for his work as a programmer, speaker and screencast creator (IMHO TekPub was the best screencast site ever made ;)). When I heard that he will be on DevDay I knew that I want to be there. The subject he presented was about <a href="https://www.rethinkdb.com/">RethinkDB</a>, which isn&rsquo;t surprising if you read his blog. He made an overview of the main features of this NoSql database with its advantages and drawbacks. The way he presented this topic is amazing &ndash; very focused on subject, but with enthusiasm and fun in the voice, with good knowledge about things he talk. For sure one of the best speaker I&rsquo;ve ever saw.</p>

<p><strong>&ldquo;Roslyn and .NET Code Gems&rdquo;</strong> by <a href="http://odetocode.com/">Scott Allen</a></p>

<p>The subject of this talk can be a bit misguiding. At first I thought that this will be about Roslyn compiler and how you can use it to extend the C# language. In fact this was about the code optimizations used by people writing Roslyn compiler (you can look at the source code <a href="https://github.com/dotnet/roslyn">here</a>). Quite interesting things you can find in this code ;)</p>

<p><strong>&ldquo;Heretics, High Priests, and Hagiolatry&rdquo;</strong> by <a href="https://twitter.com/semanticwill">Will Evans</a></p>

<p>The title of this speech was very promising. I thought this could be another great, motivating, talk. Will Evans started very good, with some interesting quotes. Unfortunately the farther he was in his talk, the more blur the subject was. I couldn&rsquo;t find one, single, idea he wanted to convey. After the talk I&rsquo;ve spoken with other people who had similar feelings. Maybe it&rsquo;s because this talk was more about people management then about technical subjects.</p>

<h3>Day two</h3>

<p><strong>&ldquo;Quo vadis, JavaScript?&rdquo;</strong> by <a href="https://christianheilmann.com/">Chris Heilmann</a></p>

<p>I&rsquo;ve seen Chris Heilmann presentation (on similar, but not identical subject) before on <a href="http://www.spacjer.com/blog/2015/03/16/wrocsharp-2015/">WrocSharp 2015</a>, so I knew he is a good speaker. I wasn&rsquo;t disappointed. In his speech he summarized state of the JavaScript as it&rsquo;s defined in ECMAScript 6 and how we can use those features now. It was good start of the second day of the conference.</p>

<p><strong>&ldquo;The Twelve Factor App Methodology&rdquo;</strong> by <a href="https://yobriefca.se/">James Hughes</a></p>

<p>On a daily basis I don&rsquo;t work with <a href="https://en.wikipedia.org/wiki/Software_as_a_service">Software as a service</a>, so for me the <a href="http://12factor.net/">twelve factor app methodology</a> was something new. The idea is quite interesting and it&rsquo;s good to know about it. From my side I give a plus for a subject. The only objection I have is about the monotonous way of presenting it.</p>

<p><strong>&ldquo;Hacking F# on Systems, Not Windows&rdquo;</strong> by <a href="http://compositecode.com/">Adron Hall</a></p>

<p>A very brief presentation about using F# on operating systems other then Windows (which means OSX and Linux). Author showed how you can use <a href="https://code.visualstudio.com/">Visual Studio Code</a> or <a href="https://atom.io/">Atom</a> to work with F# on those system. It was quite interesting, but from my point of view it lacked two things. First there was almost no info, how you can install F# on OSX or Linux platform, second it would be nice to see usage of Vim or Emacs. Still it&rsquo;s worth watching.</p>

<p><strong>&ldquo;Walkthrough of a European Space Agency Data Science Project&rdquo;</strong> by <a href="https://garyshortblog.wordpress.com/">Gary Short</a></p>

<p>Image a task, where you need to figure out health of the crops base on the satellite images. Sounds fascinating? I think so too&hellip; Gery Short worked for <a href="http://www.esa.int/ESA">European Space Agency</a> to solve this problem. In this session he told us how he achieved this and what results he got. I hope that this few sentences convinced you to see this session, I highly recommend it!</p>

<p><strong>&ldquo;From Homogeneous Monolith to Radically Heterogeneous Microservices: The Wunderlist 3 Story&rdquo;</strong> by <a href="http://chadfowler.com/">Chad Fowler</a></p>

<p>Another great speech by Chad Fowler. This time more technical one. He told us a story about switching from a monolith Rails application to a set of loosely coupled services written in many, different, languages. Everything base on his big refactor of Wunderlist application. A must-see!</p>

<p><strong>&ldquo;Document Storage Techniques with PostgreSQL and JSONB&rdquo;</strong> by <a href="http://rob.conery.io/">Rob Conery</a></p>

<p>In short: another great talk by Rob Conery. This time about how you can have fully featured, ACID compliant database that allows you working with JSON documents (a NoSQL databases thing). The title should point you to the database name &ndash; &ldquo;it&rsquo;s PostgreSQL&rdquo; :D</p>

<p><strong>&ldquo;Metaphors We Code&rdquo;</strong> by <a href="http://www.natpryce.com/">Nat Pryce</a></p>

<p>This was the last, more soft, presentation. It treats about the role of the metaphor in communication between people. How good or bad metaphor can influence on code we write. This thing is important when you work with clients and you need to understand their requirements through their language. My only complain to this speech is about it&rsquo;s length, for me it was a bit too long, 25~30 minutes would be enough.</p>

<h2>Final words</h2>

<p>In some other blog posts about DevDay 2015 I&rsquo;ve read that people were disappointed by quality of the sessions. I can&rsquo;t say that. Maybe it&rsquo;s because I&rsquo;m going to developer conference not only to learn new stuff, but also to hear about what others are doing, how they do that and how they present this to others. I almost always get something from the presentation I see. Of course I would love to watch only superior speakers, but as we all know it isn&rsquo;t possible. Still I think that every developer should attend to at least one conference a year.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Starting with Emacs]]></title>
    <link href="http://www.spacjer.com//blog/2015/08/17/starting-with-emacs/"/>
    <updated>2015-08-17T21:14:14+02:00</updated>
    <id>http://www.spacjer.com//blog/2015/08/17/starting-with-emacs</id>
    <content type="html"><![CDATA[<p>It has been almost one year since I&rsquo;ve started using Emacs. I think this is a good moment to give you some tips about starting using it. This isn&rsquo;t typical tutorial, but rather the description of my approach to learning Emacs.</p>

<h2>Let&rsquo;s start</h2>

<p>Before I even download the binary files of Emacs, I read quite a lot about it. In many guides I&rsquo;ve read that instead of installing &ldquo;plain&rdquo; Emacs, I should try a preconfigured version of it. On Github you can find many Emacs distributions (or rather configurations) like:</p>

<ul>
<li><a href="https://github.com/bbatsov/prelude">Prelude</a></li>
<li><a href="https://github.com/overtone/emacs-lie">Live</a></li>
<li><a href="https://github.com/eschulte/emacs24-starter-kit">emacs24-starter-kit</a></li>
</ul>


<p>Because I read lots of good opinions about <code>Prelude</code>, I&rsquo;ve decided to use it.</p>

<h2>Failure</h2>

<p>Without any delays I&rsquo;ve installed Emacs, pulled all code from <code>Prelude</code> repository into <code>.emacs.d</code> folder and I&rsquo;ve started my journey. Telling the truth, this wasn&rsquo;t a very long one. After starting Emacs, I did a basic tutorial (this is suggested by almost everybody and I also encourage you to do it &ndash; you can press <code>C-h t</code> to show it) and I was <em>lost</em>.  There was no menu or toolbar that could help me. There was some active minor modes that I didn&rsquo;t understand. My inner curiosity forces me also to look into <code>.emacs.d</code> directory. I wanted to understand the code that was there. For me, a Lisp beginner, this wasn&rsquo;t an easy task &ndash; I couldn&rsquo;t manage so much code. The result was, that after one day I&rsquo;ve gave up and deleted Emacs from my hard drive.</p>

<p><em>(Please, don&rsquo;t get me wrong I think that <code>Prelude</code> and other configs are great. They provide a superior, fully customized, experience when working with Emacs. Also their code base is well written and structured. Simply this wasn&rsquo;t the right time for me to use them. If you are completely beginner then maybe it won&rsquo;t be good for you too. But if you know Emacs a bit at least then you should definitely look at those distros. Right now I often read their code, to get some inspiration and learn new stuff)</em></p>

<h2>Start it plain</h2>

<p>I&rsquo;ve return to Emacs again after one week of a break. This time I&rsquo;ve decided that I will start with plain Emacs &ndash; without any modifications, with GUI that will help me. I&rsquo;ve also used the default key bindings &ndash; even for &ldquo;copy&rdquo;, &ldquo;cut&rdquo; and &ldquo;paste&rdquo; which are awkward at the beginning, because they are different then the &ldquo;Windows&rdquo; one. I&rsquo;ve used it for any task that I would normally do in <a href="http://www.sublimetext.com/">Sublime text</a>. I did them with problems. I was looking for help everywhere &ndash; in the Emacs build-in help (a very good one), in the Internet or through out available menus in toolbar. The time I&rsquo;ve spent on a typical task was bigger then it would take me in Sublime, but I was stubborn. After some time I&rsquo;ve started to feel more comfortable. I&rsquo;ve known few key binding and commands. I found some places (like editing JavaScript or <a href="http://orgmode.org/">org-mode</a>) where Emacs shines. I&rsquo;ve also started to recognize elements of Emacs that I wanted to improve. This was a moment when I started to figure out how to customize Emacs.</p>

<h2>Let it change</h2>

<p>We as programmers love to code. This can be used to master Emacs skills. Emacs uses Lisp as a language for its extensibility and customization, so you can write &ldquo;real&rdquo; code to change it to your needs. I encourage you to experiment with the configs files. This will give you better understanding of Emacs and therefore better productivity. I also challenge you to change all Emacs config code in itself &ndash; this way you also level up your editing skills.</p>

<h2>Let&rsquo;s write some Lisp code</h2>

<p>In Emacs all the configuration is done in <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html">init.el</a> file (<em>there are also other options, but I skip them for simplicity</em>). The whole configuration is done through <a href="https://www.gnu.org/software/emacs/manual/eintr.html">Emacs lisp code</a>. When Emacs starts it evaluates the code in its config files. This code can change the look and feel of the editor (GUI) or define additional commands and functions.</p>

<p>The more advance Emacs configuration is usually splitted into many config files, that deals with different elements of Emacs (i.e. every file customizes different modes). When I started customizing Emacs, I wrote all my code in <code>init.el</code> file. I didn&rsquo;t even try to write it in some meaning order. My main goal was to see what I can change and how:</p>

<ul>
<li>how can I change the font or the color theme?</li>
<li>how can I use fuzzy search in buffer switching?</li>
<li>and many more&hellip;</li>
</ul>


<p>To answer those question I&rsquo;ve read lots of articles on <a href="http://www.emacswiki.org/"><em>emacswiki</em></a>, I&rsquo;ve looked at mentioned Emacs distributions code and of course I googled a lot. Before I&rsquo;ve used any function, I&rsquo;ve read its description. If I&rsquo;ve copied some code snipped, I&rsquo;ve spent time to understand it. I extend and extend my <code>init.el</code> file for a long time &ndash; to a moment when I was quite happy with the results. This gave me better confidence in working with and customizing Emacs.</p>

<h2>Let&rsquo;s improve</h2>

<p>Of course, as almost every programmer I felt bad about my messy code in <code>init.el</code> file. After some time I&rsquo;ve started introducing changes and splitting my confing into smaller, more readable and manageable files. My current configuration is heavily inspired by <code>Prelude Emacs</code> code &ndash; <a href="http://batsov.com/">Bozhidar Batsov</a> is great and very inspiring developer. You should definitely look at his work.</p>

<p>At present I don&rsquo;t modify Emacs so much &ndash; I do it when something irritating me during my work.</p>

<p>If you still aren&rsquo;t convenient to use Emacs, I need to say that every time I use Emacs I feel joy &ndash; this is so great text editor! I hope that you will find this joy too!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Emacs]]></title>
    <link href="http://www.spacjer.com//blog/2015/04/23/emacs/"/>
    <updated>2015-04-23T20:34:30+02:00</updated>
    <id>http://www.spacjer.com//blog/2015/04/23/emacs</id>
    <content type="html"><![CDATA[<p>For more then 10 years I&rsquo;m looking for the ideal text (and code) editor. I&rsquo;ve started my journey with small, fast and probably forgotten text editor called <a href="http://en.wikipedia.org/wiki/ConTEXT">Context</a>. I was quite happy with it, but its development was stopped, which forced my to look for something else. At that moment, I&rsquo;ve started to use <a href="http://notepad-plus-plus.org/">Notepad++</a> which I still like and treat as useful, general purpose editor. Around the 2008 year <a href="http://www.sublimetext.com/">Sublime Text</a> appeared and I&rsquo;ve soon started to use it. I&rsquo;ve used version 1 to 3 and I think it&rsquo;s very good text editor (I&rsquo;m using it from time to time even now). During those years, I&rsquo;ve also tried to use <a href="http://www.vim.org/">vim</a> &ndash; for sure it&rsquo;s an excellent text editor, but I have problem with using it. Maybe it&rsquo;s because two modes and necessity of switching between them using command, maybe it&rsquo;s because the way you configure it. I don&rsquo;t really know, but I couldn&rsquo;t switch to vim (still using it in rare cases in Linux OS).</p>

<p>Around a year ago, I&rsquo;ve started reading about <a href="http://clojure.org/">Clojure</a> and in general about <a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a>. Among the developers using Lisp and it&rsquo;s dialects <a href="https://www.gnu.org/software/emacs/">Emacs</a> is the most popular text editor. The reason is simple, Emacs has be built in the Lisp interpreter (strictly speaking <a href="http://en.wikipedia.org/wiki/Emacs_Lisp]">Emacs lisp</a>) which is used to configure and extend it&rsquo;s functionality. This convinced me to give it a try. First I was skeptical (I&rsquo;ve used Emacs in one of my course, during studies), but after some time of using it (and understanding its philosophy), I think it&rsquo;s the best text editor (or maybe an <a href="http://en.wikipedia.org/wiki/Editor_war">operating system</a> ;) I&rsquo;ve ever use.</p>

<p>In the next post of my Emacs series, I will describe how I&rsquo;ve switched to Emacs. In this text I only focus on the reasons why Emacs shines in my opinion.</p>

<h3>Modes</h3>

<p>To understand Emacs, you need to realize that it is a multi modes editor. This means that for currently edited document you can have one major mode (like markdown mode, JavaScript mode and so one) and many minor modes (like autocomplete, snippets, spell checker and similar). All those modes define functions that can be either invoked by name (using <code>M-x</code> key) or using key shortcut (in Emacs named key binding). Those functions can be very different, from simple one, like move a cursor forward, to the advance one like convert markdown to HTML.</p>

<p>By default Emacs comes with wide range of preinstalled modes.</p>

<h3>Customization and extensibility</h3>

<p>As I mentioned before, all configuration in Emacs is done using <a href="https://www.gnu.org/software/emacs/manual/elisp.html">elisp</a>. This means that you have the power of real programming language at your disposal. You can configure basic options &ndash; that every text editor has (like: font, color schema, usage of tabs and so on), but you can change almost everything. You can define your own functions, that can be invoked or binded to keys combination. You can load minor modes depending on major mode (using <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Hooks.html">hooks</a>). You can show line numbers depending on mode. And many, many, more. This freedom is so big, that people write very elaborated addons (like git, email or irc clients) &ndash; called here packages. Starting from Emacs 24 those packages can be installed directly from Emacs using a simple package manager (a bit similar to <code>JS npm</code> or <code>.Net NuGet</code>).</p>

<p><a href='http://www.spacjer.com//images/posts/2015/04/elisp.jpg' rel='lightbox[Emacs]'><img class='center' src='http://www.spacjer.com//images/posts/2015/04/elisp_small.jpg' alt='Emacs lisp'></a></p>

<p>The general philosophy of Emacs is to allow you to do as much as possible from editor. You can write code, read twitter, speak on IRC, use terminal and play &lsquo;tetris&rsquo; without leaving Emacs. This is the reason why people call it an &ldquo;operating system&rdquo;. For me this is one of the greatest features of it. You don&rsquo;t need to switch contexts (and windows) to do many typical task for the programmer.</p>

<p>For people, who can&rsquo;t live without <code>Vim</code> way of working with text (navigate and write text in different modes) there are good emulators for Emacs.</p>

<p>All of this makes that you can fully customize the behavior and look of Emacs. If you can&rsquo;t live without tabs, you can find package for it or maybe you want to have file navigator on the left side, no problem there is a package for it. I think  you see the pattern right now.</p>

<h3>Org mode</h3>

<p>The <a href="http://orgmode.org/">org mode</a> is the second reason why I fall in love with Emacs. What is it? This sentence: &ldquo;Org mode is for keeping notes, maintaining TODO lists, planning projects, and authoring documents with a fast and effective plain-text system&rdquo; from the <a href="http://orgmode.org/">main page</a> summarize it the best.</p>

<p>I used several pages to track my time, <a href="https://evernote.com/">Evernote</a> and <a href="https://hackpad.com/">hackpad</a> to store notes, <a href="https://www.wunderlist.com/">Wunderlist</a> to maintain my TODO items and sever other services. The end result was that I still had problems with finding what I need. From the moment when I started using <code>org-mode</code> all of this is gone. Now I store everything in a few <code>org</code> files that are synchronized with all my computers by Dropbox. Those files are simple, text files, so I don&rsquo;t need to worry about loosing my work. This mode allows me (using agenda) to search or summarize all the items through out all those files.</p>

<p>Thanks to the power of the org-mode every item is properly highlighted and justified. You can mark item with tags, add status to it or even schedule to specific date. You can also add note to any point, link to external page or local file. I know only the basic features of this mode (still learning and discovering new stuff) but still I&rsquo;m amazed.</p>

<p>If you install Emacs on your computer, you definitely should try <code>org-mode</code>!</p>

<p><a href='http://www.spacjer.com//images/posts/2015/04/org-mode.jpg' rel='lightbox[Emacs]'><img class='center' src='http://www.spacjer.com//images/posts/2015/04/org-mode_small.jpg' alt='Org mode'></a></p>

<h3>Editing code</h3>

<p>I&rsquo;ve described many Emacs possibility, but in the first place it&rsquo;s of course superior code editor. On a daily basics I use it to edit JavaScript, Clojure and Emacs lisp. From time to time I edit some XML or json files. In all those usages it works great!</p>

<h4>JavaScript</h4>

<p>For editing JavaScript I use <a href="http://www.emacswiki.org/emacs/Js2Mode">js2-mode</a>. What I like in it is the proper auto-indentation of code, good syntax highlighting and built-in JS lint (it marks missing colons, not used variables and many others). All those features make that editing JS code is very pleasant experience.</p>

<h4>Clojure</h4>

<p>As I wrote before Clojure is a dialect of Lisp, so using Emacs to edit it is an obvious choice. But even this can be improved. By installing <a href="https://github.com/clojure-emacs/cider">cider</a> (and <a href="https://github.com/clojure-emacs/clojure-mode">clojure-mode</a>) mode you can change a text editor in fully featured IDE for editing Clojure. Lets list only main features of those modes:<br/>
&ndash; proper code indention<br/>
&ndash; using REPL from Emacs buffer<br/>
&ndash; code evaluation inside code buffer (ala <a href="http://lighttable.com/">LightTable</a>)<br/>
&ndash; documentation lookup<br/>
&ndash; debugger<br/>
&ndash; value inspector<br/>
and many many more&hellip;</p>

<p>I&rsquo;ve used most IDE and code editors (NetBeans, IntelliJ, Cursive, LightTable) for working with Clojure and I have to say that, from my perspective, they can&rsquo;t compete with this setup. If you do Clojure, you definitely should give it a try.</p>

<h3>Drawbacks</h3>

<p>Emacs has of course its own drawbacks. The obvious one is the learning curve when starting using it. The lack of knowledge of any kind of Lisp and strange key bindings can make it hard to use and configure at the first time.</p>

<p>From my perspective there is one more problem, Emacs has slow startup time. Especially if you have many packages installed, the opening it can take few seconds. To minimize this annoyance you should learn to work only with one instance of it. If you finish editing file, you shouldn&rsquo;t close the program, but only the buffer.</p>

<h3>Summary</h3>

<p>Emacs isn&rsquo;t for everybody. Some people can&rsquo;t live without graphical GUI, others can&rsquo;t stand its shortcut. But I highly recommend you give it a try. If you defeat initial problems you will find excellent text editor.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Leiningen: working with local repository]]></title>
    <link href="http://www.spacjer.com//blog/2015/03/23/leiningen-working-with-local-repository/"/>
    <updated>2015-03-23T20:46:01+01:00</updated>
    <id>http://www.spacjer.com//blog/2015/03/23/leiningen-working-with-local-repository</id>
    <content type="html"><![CDATA[<p><a href="http://leiningen.org/">Leiningen</a> is de facto standard for creating and managing projects in Clojure. To create new project we can simply write:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">lein</span> <span class="k">new </span><span class="nv">my-app</span>
</span></code></pre></td></tr></table></div></figure>


<p>and a basic structure of the project is created for us. Now to add any dependencies (and download them from Maven or Clojars repository) we need to modify <code>project.clj</code> file. Let say we want to generate markdown from a string, so we need to add <a href="https://github.com/clojure/data.json"><code>markdown-clj</code></a> project. To do so we need to modify <code>project.clj</code> file in our project:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defproject </span><span class="nv">my-app</span> <span class="s">&quot;0.1.0-SNAPSHOT&quot;</span>
</span><span class='line'>  <span class="ss">:description</span> <span class="s">&quot;FIXME: write description&quot;</span>
</span><span class='line'>  <span class="ss">:url</span> <span class="s">&quot;http://example.com/FIXME&quot;</span>
</span><span class='line'>  <span class="ss">:license</span> <span class="p">{</span><span class="ss">:name</span> <span class="s">&quot;Eclipse Public License&quot;</span>
</span><span class='line'>            <span class="ss">:url</span> <span class="s">&quot;http://www.eclipse.org/legal/epl-v10.html&quot;</span><span class="p">}</span>
</span><span class='line'>  <span class="ss">:dependencies</span> <span class="p">[[</span><span class="nv">org.clojure/clojure</span> <span class="s">&quot;1.6.0&quot;</span><span class="p">]</span>
</span><span class='line'>                 <span class="p">[</span><span class="nv">markdown-clj</span> <span class="s">&quot;0.9.63&quot;</span><span class="p">]]</span>
</span><span class='line'>   <span class="ss">:main</span> <span class="o">^</span><span class="ss">:skip-aot</span> <span class="nv">my-app.core</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>then we can write such code in <code>\my-app\src\my_app\core.clj</code> file:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">my-app.core</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">markdown.core</span> <span class="ss">:as</span> <span class="nv">mark</span><span class="p">]))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">-main</span> <span class="p">[]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">print</span>
</span><span class='line'>   <span class="p">(</span><span class="nf">mark/md-to-html-string</span> <span class="s">&quot;#Header&quot;</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Everything works great! But now for unknown reason, temporary, we want to modify <code>md-to-html-string</code> function (from <code>markdown-clj/src/markdown/core.clj</code>), to convert all our text to upper case. Figuring out how to do that can take a while (at least for me it wasn&rsquo;t so intuitive how to change depended code), so I will show you.</p>

<p>First we need to clone git repository to local disk.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>git clone https://github.com/yogthos/markdown-clj.git</span></code></pre></td></tr></table></div></figure>


<p>Then we can modify <code>md-to-html-string</code> function in <code>core.clj</code> file:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">md-to-html-string</span>
</span><span class='line'>  <span class="s">&quot;converts a markdown formatted string to an HTML formatted string&quot;</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">text</span> <span class="o">&amp;</span> <span class="nv">params</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">when </span><span class="nv">text</span>
</span><span class='line'>    <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">input</span> <span class="p">(</span><span class="k">new </span><span class="nv">StringReader</span> <span class="nv">text</span><span class="p">)</span>
</span><span class='line'>          <span class="nv">output</span> <span class="p">(</span><span class="k">new </span><span class="nv">StringWriter</span><span class="p">)]</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">apply </span><span class="p">(</span><span class="nb">partial </span><span class="nv">md-to-html</span> <span class="nv">input</span> <span class="nv">output</span><span class="p">)</span> <span class="nv">params</span><span class="p">)</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">clojure.string/upper-case</span>
</span><span class='line'>       <span class="p">(</span><span class="nf">.toString</span> <span class="nv">output</span><span class="p">)))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>and change a version name in <code>project.clj</code> file &ndash; to not interfere with the original one:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defproject </span><span class="nv">markdown-clj</span> <span class="s">&quot;0.9.63-SNAPSHOT&quot;</span>
</span><span class='line'>            <span class="ss">:description</span> <span class="s">&quot;Markdown parser&quot;</span>
</span><span class='line'>            <span class="ss">:url</span> <span class="s">&quot;https://github.com/yogthos/markdown-clj&quot;</span>
</span><span class='line'>            <span class="ss">:license</span> <span class="p">{</span><span class="ss">:name</span> <span class="s">&quot;Eclipse Public License&quot;</span>
</span><span class='line'>                      <span class="ss">:url</span> <span class="s">&quot;http://www.eclipse.org/legal/epl-v10.html&quot;</span><span class="p">}</span>
</span><span class='line'>                      <span class="ss">:dependencies</span> <span class="p">[[</span><span class="nv">org.clojure/clojure</span> <span class="s">&quot;1.6.0&quot;</span><span class="p">]]</span>
</span><span class='line'>                      <span class="nv">....</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>and the last (but most important part) is to install this library in our local Clojure repository. To do that we need to be in <code>markdown-clj</code> project directory (where the <code>project.clj</code> file is) and write:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>lein install</span></code></pre></td></tr></table></div></figure>


<p>This will install <code>jar</code> and <code>pom</code> of <code>markdown-clj</code> project, in version <code>0.9.63-SNAPSHOT</code> into local repo. We can use it now, by simply specifing correct version in our <code>project.clj</code> file:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defproject </span><span class="nv">my-app</span> <span class="s">&quot;0.1.0-SNAPSHOT&quot;</span>
</span><span class='line'>  <span class="ss">:description</span> <span class="s">&quot;FIXME: write description&quot;</span>
</span><span class='line'>  <span class="ss">:url</span> <span class="s">&quot;http://example.com/FIXME&quot;</span>
</span><span class='line'>  <span class="ss">:license</span> <span class="p">{</span><span class="ss">:name</span> <span class="s">&quot;Eclipse Public License&quot;</span>
</span><span class='line'>            <span class="ss">:url</span> <span class="s">&quot;http://www.eclipse.org/legal/epl-v10.html&quot;</span><span class="p">}</span>
</span><span class='line'>  <span class="ss">:dependencies</span> <span class="p">[[</span><span class="nv">org.clojure/clojure</span> <span class="s">&quot;1.6.0&quot;</span><span class="p">]</span>
</span><span class='line'>                 <span class="p">[</span><span class="nv">markdown-clj</span> <span class="s">&quot;0.9.63-SNAPSHOT&quot;</span><span class="p">]]</span>
</span><span class='line'>               <span class="ss">:main</span> <span class="o">^</span><span class="ss">:skip-aot</span> <span class="nv">my-app.core</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>After this the modified version of <code>markdown-clj</code> project will be used. When we execute this code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">mark/md-to-html-string</span> <span class="s">&quot;#Header&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>we will get:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;H1&gt;HEADER&lt;/H1&gt;</span></code></pre></td></tr></table></div></figure>


<p>This can be useful if you need temporary fix a bug or test your future pull request.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[wrocsharp 2015]]></title>
    <link href="http://www.spacjer.com//blog/2015/03/16/wrocsharp-2015/"/>
    <updated>2015-03-16T21:39:42+01:00</updated>
    <id>http://www.spacjer.com//blog/2015/03/16/wrocsharp-2015</id>
    <content type="html"><![CDATA[<p>On the 12 of March I&rsquo;ve attended <a href="http://wrocsharp.pl/"><em>Wroc# 2015</em></a> &ndash; a free software developer conference organized by <a href="http://www.objectivity.pl/">Objectivity</a> company. This was the first edition (and I hope not the last!) of this event, but the organization was so good, that you would rather say that it&rsquo;s the next one. The organizers provide free food, drinks, gadgets and even an after party with live music and table football tournament. Big thanks for that!</p>

<p>Of course infrastructure is only an addition to the sessions that define conference content. Hera are my opinions about those:</p>

<ul>
<li>&ldquo;Innovating the other web&rdquo; by <a href="http://christianheilmann.com/">Chris Heilmann</a></li>
</ul>


<p>Chris presented his point of view on HTML5 and its benefits over native, mobile, apps. It was also a very brief introduction to Microsoft new HTML browser &ndash; <a href="http://blogs.msdn.com/b/ie/archive/2015/01/22/project-spartan-and-the-windows-10-january-preview-build.aspx">Spartan</a>. It contains not many new informations, but overall I liked this talk.</p>

<ul>
<li>&ldquo;Little changes to make your app a lot faster&rdquo;<a href="http://sticklebackplastic.com/">Matt Ellis</a></li>
</ul>


<p>This presentation focused on micro-optimizations of C# code. Matt showed code samples and the ways you could improve performance of them (especially the memory and garbage collector usage). Probably you won&rsquo;t use this knowledge very often in your code, but it&rsquo;s good to know how C# compiler works and how you can improve parts of your critical code. This talk was good, but without anything above that.</p>

<ul>
<li>&ldquo;Type-Driven Development&rdquo; by <a href="http://blog.ploeh.dk/">Mark Seemann</a></li>
</ul>


<p>On stage coding session, showing power of F# type inference and how type system can guide you to better solutions. Right now I&rsquo;m interested in functional languages (although more in dynamic ones like Clojure) so this was ideal session for me. Be warn only that it requires your full attention to understand Mark&rsquo;s thinking process.</p>

<ul>
<li>&ldquo;I, Programmer&rdquo; by <a href="http://dannorth.net/blog/">Dan North</a></li>
</ul>


<p>This was my second time (previous on <a href="http://devday.pl/archive/2014/">DevDay 2014</a>) when I saw Dan live on stage and again I think he&rsquo;s a great speaker. In this light and entertaining talk he pointed directions on which good programmer should focus during his career. For me it was the best speech of the day.</p>

<ul>
<li>&ldquo;What is new in ASP.NET vNext?&rdquo; by <a href="http://blogs.msmvps.com/theproblemsolver/">Maurice De Beijer</a></li>
</ul>


<p>Quite complete overview of new version of <a href="http://www.asp.net/vnext">ASP.NET ecosystem</a> (mvc, web api). If you don&rsquo;t know a lot about next version of Microsoft web framework then this talk is for you. For me this wasn&rsquo;t anything new, but even though I think Maurice did a good job.</p>

<ul>
<li>&ldquo;Decomposing AngularJS&rdquo; by <a href="http://chris.59north.com/">Chris Klug</a></li>
</ul>


<p>I&rsquo;m not a big fan of <a href="https://angularjs.org/">AngularJS</a> framework (sounds like a good topic for a blog post) but this talk wasn&rsquo;t really about Angular. It was about data binding in this framework and how it works &ndash; which is very interesting thing! Chris, even with technical problems, did a great live &ndash; coding session. He started with empty text file and finished with working data binding solution in JavaScript. I like to understand how &ldquo;the magic&rdquo; works, so such presentation are for me.</p>

<ul>
<li>Discussion panel</li>
</ul>


<p>Unfortunately, the last speaker &ndash; <a href="http://blog.markrendle.net/">Mark Rendle</a> couldn&rsquo;t come to Wroclaw (because of the flue). Instead of his talk there was a discussion panel with all speakers moderated by Dan North. Dan again showed his talent by selecting interesting topics and &ldquo;forcing&rdquo; others for answers. This was great finishing session and I suggest you to watch it when it&rsquo;s available online.</p>

<p>I&rsquo;m glad that I could be on this event. It was well spent day with people that care about what they do. Such events make that I want to be part of this awesome community! It would be great to be there next year! Thanks guys for organizing it!</p>

<p>The only sad news that day was that <em>Terry Pratchett</em> died. Rest in peace Sir Terry!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ClojureScript: JavaScript Interop]]></title>
    <link href="http://www.spacjer.com//blog/2014/09/12/clojurescript-javascript-interop/"/>
    <updated>2014-09-12T17:16:26+02:00</updated>
    <id>http://www.spacjer.com//blog/2014/09/12/clojurescript-javascript-interop</id>
    <content type="html"><![CDATA[<p><em>(this post was updated on 15th of March 2015)</em></p>

<p>As I mentioned before on this blog, I&rsquo;m in the ongoing process of learning Clojure (and ClojureScript). To better understand the language, I&rsquo;ve written small web application. For fun I decided that all my front end code will be written in ClojureScript. Because I needed to use external JavaScript API (Bing Maps AJAX Control) I wrote quite a bit of JavaScript interop code &ndash; the syntax wasn&rsquo;t obvious for me and I couldn&rsquo;t find a place that have all that info, so I wrote this post. Please be warn, this is quite a long post!</p>

<h2>JavaScript example</h2>

<p>To easier understand all examples lets define simple JavaScript code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">//global variable</span>
</span><span class='line'><span class="nx">globalName</span> <span class="o">=</span> <span class="s2">&quot;JavaScript Interop&quot;</span><span class="p">;</span>
</span><span class='line'><span class="nx">globalArray</span> <span class="o">=</span> <span class="nx">globalArray</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;c&quot;</span><span class="p">]];</span>
</span><span class='line'><span class="nx">globalObject</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">a</span><span class="o">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">b</span><span class="o">:</span> <span class="mi">2</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">c</span><span class="o">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">],</span>
</span><span class='line'>  <span class="nx">d</span><span class="o">:</span> <span class="s2">&quot;some text&quot;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//global function</span>
</span><span class='line'><span class="nb">window</span><span class="p">.</span><span class="nx">hello</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">alert</span><span class="p">(</span><span class="s2">&quot;hello!&quot;</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//global function</span>
</span><span class='line'><span class="nb">window</span><span class="p">.</span><span class="nx">helloAgain</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">alert</span><span class="p">(</span><span class="nx">name</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//a JS type</span>
</span><span class='line'><span class="nx">MyType</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s2">&quot;MyType&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">MyComplexType</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">MyComplexType</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">hello</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">alert</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">MyComplexType</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">helloFrom</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">userName</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">alert</span><span class="p">(</span><span class="s2">&quot;Hello from &quot;</span> <span class="o">+</span> <span class="nx">userName</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Global scope</h2>

<p>ClojureScript defines special <code>js</code> namespace to allow accessing JavaScript types/functions/methods/objects defined in global scope (i.e. window object for browser).</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">text</span> <span class="nv">js/globalName</span><span class="p">)</span> <span class="c1">;; JS output: namespace.text = globalName;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Creating objects</h2>

<p>We can create JavaScript objects from ClojureScript by adding <code>.</code> to the end of constructor function:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">t1</span> <span class="p">(</span><span class="nf">js/MyType.</span><span class="p">))</span> <span class="c1">;; JS output: namespace.t1 = new MyType;</span>
</span></code></pre></td></tr></table></div></figure>


<p>(note: at first I thought that this generated JS code was wrong, because of the lack of parentheses, but as it clarifies it&rsquo;s valid &ndash; if constructor function doesn&rsquo;t have arguments, then parentheses can be <a href="http://stackoverflow.com/questions/6439290/parentheses-after-new-function-optional">skipped</a>)</p>

<p>with arguments:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">t2</span> <span class="p">(</span><span class="nf">js/MyComplexType.</span> <span class="s">&quot;Bob&quot;</span><span class="p">))</span> <span class="c1">;; JS output: namespace.t2 = new MyComplexType(&quot;Bob&quot;);</span>
</span></code></pre></td></tr></table></div></figure>


<p>There is also a different way of creating objects, by using the <code>new</code> function (the name of JS constructor function should be without period):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-type</span> <span class="p">(</span><span class="k">new </span><span class="nv">js/MyComplexType</span> <span class="s">&quot;Bob&quot;</span><span class="p">))</span> <span class="c1">;; JS output: namespace.my_type = new MyComplexType(&quot;Bob&quot;);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Invoking methods</h2>

<p>To invoke a JavaScript method we need to prefix the name of the method with the <code>.</code> (dot):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">.hello</span> <span class="nv">js/window</span><span class="p">)</span> <span class="c1">;; JS output: window.hello();</span>
</span></code></pre></td></tr></table></div></figure>


<p>which is a syntactic sugar of:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">. </span><span class="nv">js/window</span> <span class="p">(</span><span class="nf">hello</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>To pass arguments to the function we do:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">.helloAgain</span> <span class="nv">js/window</span> <span class="s">&quot;John&quot;</span><span class="p">)</span> <span class="c1">;; JS output: window.helloAgain(&quot;John&quot;);</span>
</span></code></pre></td></tr></table></div></figure>


<p>or</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">. </span><span class="nv">js/window</span> <span class="p">(</span><span class="nf">helloAgain</span> <span class="s">&quot;John&quot;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Same thing can be done with created object:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-type</span> <span class="p">(</span><span class="nf">js/MyComplexType.</span> <span class="s">&quot;Bob&quot;</span><span class="p">))</span> <span class="c1">;; JS output: namespace.my_type = new MyComplexType(&quot;Bob&quot;);</span>
</span><span class='line'><span class="p">(</span><span class="nf">.hello</span> <span class="nv">my-type</span><span class="p">)</span>                        <span class="c1">;; JS output: namespace.my_type.hello();</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Accessing properties</h2>

<p>ClojureScript provides a few ways of working with JavaScript properties. The simplest one is to use <code>.-</code> property access syntax:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-type</span> <span class="p">(</span><span class="nf">js/MyType.</span><span class="p">))</span>  <span class="c1">;; JS output: namespace.my_type = new MyType;</span>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nb">name </span><span class="p">(</span><span class="nf">.-name</span> <span class="nv">my-type</span><span class="p">))</span> <span class="c1">;; JS output: namespace.name = namespace.my_type.name;</span>
</span></code></pre></td></tr></table></div></figure>


<p>similar thing can be achieved by <code>aget</code> function, which takes object and the name of the property (as a string) as arguments:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nb">name </span><span class="p">(</span><span class="nb">aget </span><span class="nv">my-type</span> <span class="s">&quot;name&quot;</span><span class="p">))</span> <span class="c1">;; JS output: namespace.name = namespace.my_type[&quot;name&quot;];</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>aget</code> allows also accessing nested properties:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">aget </span><span class="nv">js/object</span> <span class="s">&quot;prop1&quot;</span> <span class="s">&quot;prop2&quot;</span> <span class="s">&quot;prop3&quot;</span><span class="p">)</span> <span class="c1">;; JS output: object[&quot;prop1&quot;][&quot;prop2&quot;][&quot;prop3&quot;];</span>
</span></code></pre></td></tr></table></div></figure>


<p>the same thing (generated code is different) can be done by using <code>..</code> syntax:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">.. </span><span class="nv">js/object</span> <span class="nv">-prop1</span> <span class="nv">-prop2</span> <span class="nv">-prop3</span><span class="p">)</span> <span class="c1">;; JS output: object.prop1.prop2.prop3;</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can also set a value of a property from the ClojureScript, to do this you can use <code>aset</code> or <code>set!</code> functions:</p>

<p>The <code>aset</code> function takes name of the property as a string:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-type</span> <span class="p">(</span><span class="nf">js/MyType.</span><span class="p">))</span>
</span><span class='line'><span class="p">(</span><span class="nb">aset </span><span class="nv">my-type</span> <span class="s">&quot;name&quot;</span> <span class="s">&quot;Bob&quot;</span><span class="p">)</span> <span class="c1">;; JS output: namespace.my_type[&quot;name&quot;] = &quot;Bob&quot;;</span>
</span></code></pre></td></tr></table></div></figure>


<p>and the <code>set!</code> takes a property access:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">set!</span> <span class="p">(</span><span class="nf">.-name</span> <span class="nv">my-type</span><span class="p">)</span> <span class="s">&quot;Andy&quot;</span><span class="p">)</span> <span class="c1">;; JS output: namespace.my_type.name = &quot;Andy&quot;;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Arrays</h2>

<p>The <code>aget</code> function can be also used for accessing JavaScript array element:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">aget </span><span class="nv">js/globalArray</span> <span class="mi">1</span><span class="p">)</span> <span class="c1">;; JS output: globalArray[1];</span>
</span></code></pre></td></tr></table></div></figure>


<p>or if you want to get nested element you can use it in this way:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">aget </span><span class="nv">js/globalArray</span> <span class="mi">3</span> <span class="mi">1</span><span class="p">)</span> <span class="c1">;; JS output: globalArray[3][1];</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Nested scopes</h2>

<p>This subject was a bit confusing for me. In my project I wanted to translate such a code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">map</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Microsoft</span><span class="p">.</span><span class="nx">Maps</span><span class="p">.</span><span class="nx">Map</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>to ClojureScript. As you can see the <code>Map</code> function is in nested scope. The idiomatic way of accessing nested properties is to use <code>..</code> or <code>aget</code> <a href="https://groups.google.com/d/topic/clojurescript/0paweKQU_ns/discussion">functions</a> but this can&rsquo;t be done for <a href="http://dev.clojure.org/jira/browse/CLJS-455">constructor function</a>. In such case, we need to use the dot notation (even it&rsquo;s not idiomatic for Clojure code):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">m2</span> <span class="p">(</span><span class="nf">js/Microsoft.Maps.Themes.BingTheme.</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>or with the <code>new</code> function:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">m1</span> <span class="p">(</span><span class="k">new </span><span class="nv">js/Microsoft.Maps.Themes.BingTheme</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we write this expression like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">m3</span> <span class="p">(</span><span class="k">new </span><span class="p">(</span><span class="nb">.. </span><span class="nv">js/Microsoft</span> <span class="nv">-Maps</span> <span class="nv">-Themes</span> <span class="nv">-BingTheme</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>we will get an exception:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class=''><span class='line'> First arg to new must be a symbol at line
</span><span class='line'>                core.clj:4403 clojure.core/ex-info
</span><span class='line'>             analyzer.clj:268 cljs.analyzer/error
</span><span class='line'>             analyzer.clj:265 cljs.analyzer/error
</span><span class='line'>             analyzer.clj:908 cljs.analyzer/eval1316[fn]
</span><span class='line'>             MultiFn.java:241 clojure.lang.MultiFn.invoke
</span><span class='line'>            analyzer.clj:1444 cljs.analyzer/analyze-seq
</span><span class='line'>            analyzer.clj:1532 cljs.analyzer/analyze[fn]
</span><span class='line'>            analyzer.clj:1525 cljs.analyzer/analyze
</span><span class='line'>             analyzer.clj:609 cljs.analyzer/eval1188[fn]
</span><span class='line'>             analyzer.clj:608 cljs.analyzer/eval1188[fn]
</span><span class='line'>             MultiFn.java:241 clojure.lang.MultiFn.invoke
</span><span class='line'>            analyzer.clj:1444 cljs.analyzer/analyze-seq
</span><span class='line'>            analyzer.clj:1532 cljs.analyzer/analyze[fn]
</span><span class='line'>            analyzer.clj:1525 cljs.analyzer/analyze
</span><span class='line'>            analyzer.clj:1520 cljs.analyzer/analyze
</span><span class='line'>             compiler.clj:908 cljs.compiler/compile-file*
</span><span class='line'>            compiler.clj:1022 cljs.compiler/compile-file</span></code></pre></td></tr></table></div></figure>


<h2>Creating JavaScript objects</h2>

<p>There are many cases where we need to pass JavaScript object to a method from ClojureScript. In general ClojureScript works with its own data structures (immutable, persistent vector, map, set etc.) that can be converted to plain JS objects. There are several ways of doing it.</p>

<p>If we want to create a simple JavaScript object from the list of key value pairs we can use <code>js-obj</code> macro:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-object</span> <span class="p">(</span><span class="nf">js-obj</span> <span class="s">&quot;a&quot;</span> <span class="mi">1</span> <span class="s">&quot;b&quot;</span> <span class="nv">true</span> <span class="s">&quot;c&quot;</span> <span class="nv">nil</span><span class="p">))</span> <span class="c1">;; JS output: namespace.my_object_4 = (function (){var obj6284 = {&quot;a&quot;:(1),&quot;b&quot;:true,&quot;c&quot;:null};return obj6284;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that <code>js-obj</code> forces you to use strings as keys and basic data literals (string, number, boolean) as values. The ClojureScript data structures won&rsquo;t be changed, so this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">js-object</span> <span class="p">(</span><span class="nf">js-obj</span>  <span class="ss">:a</span> <span class="mi">1</span> <span class="ss">:b</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">]</span> <span class="ss">:c</span> <span class="o">#</span><span class="p">{</span><span class="s">&quot;d&quot;</span> <span class="nv">true</span> <span class="ss">:e</span> <span class="nv">nil</span><span class="p">}))</span>
</span></code></pre></td></tr></table></div></figure>


<p>will create such JavaScript object:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="s2">&quot;:c&quot;</span> <span class="nx">cljs</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">PersistentHashSet</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;:b&quot;</span> <span class="nx">cljs</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">PersistentVector</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;:a&quot;</span> <span class="mi">1</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>as you can see there are used internal types such as:
<code>cljs.core.PersistentHashSet</code>
<code>cljs.core.PersistentVector</code>
and the ClojureScript keyword was changed to string prefixed with colon.</p>

<p>To solve this problem we can use <code>clj-&gt;js</code> function that:
&ldquo;Recursively transforms ClojureScript values to JavaScript.
sets/vectors/lists become Arrays, Keywords and Symbol become Strings,
Maps become Objects.&rdquo;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">js-object</span> <span class="p">(</span><span class="nf">clj-&gt;js</span>  <span class="ss">:a</span> <span class="mi">1</span> <span class="ss">:b</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">]</span> <span class="ss">:c</span> <span class="o">#</span><span class="p">{</span><span class="s">&quot;d&quot;</span> <span class="nv">true</span> <span class="ss">:e</span> <span class="nv">nil</span><span class="p">}))</span>
</span></code></pre></td></tr></table></div></figure>


<p>will produce such object:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="s2">&quot;a&quot;</span><span class="o">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;b&quot;</span><span class="o">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
</span><span class='line'>  <span class="s2">&quot;c&quot;</span><span class="o">:</span> <span class="p">[</span><span class="kc">null</span><span class="p">,</span> <span class="s2">&quot;d&quot;</span><span class="p">,</span> <span class="s2">&quot;e&quot;</span><span class="p">,</span> <span class="kc">true</span><span class="p">]</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>There is also one more way of producing JavaScript objects &ndash; we can use <code>#js</code> reader literal:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">js-object</span> <span class="o">#</span><span class="nv">js</span> <span class="p">{</span><span class="ss">:a</span> <span class="mi">1</span> <span class="ss">:b</span> <span class="mi">2</span><span class="p">})</span>
</span></code></pre></td></tr></table></div></figure>


<p>which generates code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">namespace</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">js_object</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;b&quot;</span><span class="o">:</span> <span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="s2">&quot;a&quot;</span><span class="o">:</span> <span class="p">(</span><span class="mi">1</span><span class="p">)};</span>
</span></code></pre></td></tr></table></div></figure>


<p>When working with <code>#js</code> you need to be cautious, because this literal also won&rsquo;t transform inner structures (it&rsquo;s <a href="https://github.com/swannodette/lt-cljs-tutorial/blob/master/lt-cljs-tutorial.cljs">shallow</a>):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">js-object</span> <span class="o">#</span><span class="nv">js</span> <span class="p">{</span><span class="ss">:a</span> <span class="mi">1</span> <span class="ss">:b</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">]</span> <span class="ss">:c</span> <span class="p">{</span><span class="s">&quot;d&quot;</span> <span class="nv">true</span> <span class="ss">:e</span> <span class="nv">nil</span><span class="p">}})</span>
</span></code></pre></td></tr></table></div></figure>


<p>will create such object:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="s2">&quot;c&quot;</span><span class="o">:</span> <span class="nx">cljs</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">PersistentArrayMap</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;b&quot;</span><span class="o">:</span> <span class="nx">cljs</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">PersistentVector</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;a&quot;</span><span class="o">:</span> <span class="mi">1</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>to solve this you need to add <code>#js</code> before every ClojureScript structure:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">js-object</span> <span class="o">#</span><span class="nv">js</span> <span class="p">{</span><span class="ss">:a</span> <span class="mi">1</span> <span class="ss">:b</span> <span class="o">#</span><span class="nv">js</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">]</span> <span class="ss">:c</span> <span class="o">#</span><span class="nv">js</span> <span class="p">[</span><span class="s">&quot;d&quot;</span> <span class="nv">true</span> <span class="ss">:e</span> <span class="nv">nil</span><span class="p">]})</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>JavaScript object: </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="s2">&quot;c&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="s2">&quot;e&quot;</span><span class="o">:</span> <span class="kc">null</span><span class="p">,</span>
</span><span class='line'>    <span class="s2">&quot;d&quot;</span><span class="o">:</span> <span class="kc">true</span>
</span><span class='line'>  <span class="p">},</span>
</span><span class='line'>  <span class="s2">&quot;b&quot;</span><span class="o">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span> <span class="p">],</span>
</span><span class='line'>  <span class="s2">&quot;a&quot;</span><span class="o">:</span> <span class="mi">1</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Using JavaScript objects</h2>

<p>There are situations when we need to convert JavaScript object or array into ClojureScript data structure. We can do this by using <code>js-&gt;clj</code> function that:
&ldquo;Recursively transforms JavaScript arrays into ClojureScript vectors, and JavaScript objects into ClojureScript maps.  With
option &lsquo;:keywordize-keys true&rsquo; will convert object fields from
strings to keywords.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-array</span> <span class="p">(</span><span class="nf">js-&gt;clj</span> <span class="p">(</span><span class="nf">.-globalArray</span> <span class="nv">js/window</span><span class="p">)))</span>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">first-item</span> <span class="p">(</span><span class="nb">get </span><span class="nv">my-array</span> <span class="mi">0</span><span class="p">))</span> <span class="c1">;; 1</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-obj</span> <span class="p">(</span><span class="nf">js-&gt;clj</span> <span class="p">(</span><span class="nf">.-globalObject</span> <span class="nv">js/window</span><span class="p">)))</span>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">a</span> <span class="p">(</span><span class="nb">get </span><span class="nv">my-obj</span> <span class="s">&quot;a&quot;</span><span class="p">))</span> <span class="c1">;; 1</span>
</span></code></pre></td></tr></table></div></figure>


<p>as the function doc states we can use <code>:keywordize-keys true</code> to convert string keys of created map to keywords:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">my-obj-2</span> <span class="p">(</span><span class="nf">js-&gt;clj</span> <span class="p">(</span><span class="nf">.-globalObject</span> <span class="nv">js/window</span><span class="p">)</span> <span class="ss">:keywordize-keys</span> <span class="nv">true</span><span class="p">))</span>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">a-2</span> <span class="p">(</span><span class="ss">:a</span> <span class="nv">my-obj-2</span><span class="p">))</span> <span class="c1">;; 1</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Addition</h2>

<p>If all other methods of working with JavaScript failed, there is a <code>js*</code> that takes a string as an argument and emits it as a JavaScript code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">js*</span> <span class="s">&quot;alert(&#39;my special JS code&#39;)&quot;</span><span class="p">)</span> <span class="c1">;; JS output: alert(&#39;my special JS code&#39;);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Exposing ClojureScript functions</h2>

<p>It is worth noting that the exact form of JavaScript code generated from ClojureScript depends on compiler settings. Those settings can be defined in <a href="http://leiningen.org/">Leiningen</a> <code>project.clj</code> file:</p>

<figure class='code'><figcaption><span>Part of project.clj file: </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="ss">:cljsbuild</span> <span class="p">{</span>
</span><span class='line'>    <span class="ss">:builds</span> <span class="p">[{</span><span class="ss">:id</span> <span class="s">&quot;dev&quot;</span>
</span><span class='line'>              <span class="ss">:source-paths</span> <span class="p">[</span><span class="s">&quot;src&quot;</span><span class="p">]</span>
</span><span class='line'>              <span class="ss">:compiler</span> <span class="p">{</span>
</span><span class='line'>                <span class="ss">:main</span> <span class="nv">your-namespace.core</span>
</span><span class='line'>                <span class="ss">:output-to</span> <span class="s">&quot;out/your-namespace.js&quot;</span>
</span><span class='line'>                <span class="ss">:output-dir</span> <span class="s">&quot;out&quot;</span>
</span><span class='line'>                <span class="ss">:optimizations</span> <span class="ss">:none</span>
</span><span class='line'>                <span class="ss">:cache-analysis</span> <span class="nv">true</span>
</span><span class='line'>                <span class="ss">:source-map</span> <span class="nv">true</span><span class="p">}}</span>
</span><span class='line'>             <span class="p">{</span><span class="ss">:id</span> <span class="s">&quot;release&quot;</span>
</span><span class='line'>              <span class="ss">:source-paths</span> <span class="p">[</span><span class="s">&quot;src&quot;</span><span class="p">]</span>
</span><span class='line'>              <span class="ss">:compiler</span> <span class="p">{</span>
</span><span class='line'>                <span class="ss">:main</span> <span class="nv">blog-sc-testing.core</span>
</span><span class='line'>                <span class="ss">:output-to</span> <span class="s">&quot;out-adv/your-namespace.min.js&quot;</span>
</span><span class='line'>                <span class="ss">:output-dir</span> <span class="s">&quot;out-adv&quot;</span>
</span><span class='line'>                <span class="ss">:optimizations</span> <span class="ss">:advanced</span>
</span><span class='line'>                <span class="ss">:pretty-print</span> <span class="nv">false</span><span class="p">}}]}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see above, there are two defined builds: <code>dev</code> and <code>release</code>. Please note the <code>:optimizations</code> <a href="https://github.com/clojure/clojurescript/wiki/Quick-Start#production-mode">parameter</a> &ndash; for <code>:advanced</code> value the code will be truncated (not used code is removed) and renamed (shorter names are used).</p>

<p>For example, this ClojureScript code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">add-numbers</span> <span class="p">[</span><span class="nv">a</span> <span class="nv">b</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">+ </span><span class="nv">a</span> <span class="nv">b</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>will be compiled to such JavaScript code in <code>:advanced</code> mode:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">yg</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">){</span><span class="k">return</span> <span class="nx">a</span><span class="o">+</span><span class="nx">b</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The function name is completely &ldquo;random&rdquo;, so you can&rsquo;t use it from JavaScript file. To be able to use defined in ClojureScript functions (with their original names) you should mark them with the <code>:export</code> metadata:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="o">^</span><span class="ss">:export</span> <span class="nv">add-numbers</span> <span class="p">[</span><span class="nv">a</span> <span class="nv">b</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">+ </span><span class="nv">a</span> <span class="nv">b</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>:export</code> keyword tells compiler to export given function name to the outside world. (This is done by <code>exportSymbol</code> function from <a href="https://developers.google.com/closure/compiler/docs/api-tutorial3">Google Closure Compiler</a> &ndash; but I won&rsquo;t go into the details). Then in your external JavaScript code you can invoke this function:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">your_namespace</span><span class="p">.</span><span class="nx">core</span><span class="p">.</span><span class="nx">add_numbers</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Please, notice that all dashes were replaced by underscors.</p>

<h2>Using external JavaScript libraries</h2>

<p>The <code>:advanced</code> mode affects also invocation of the external libreries, because all functions/methods names are changed to minimal form. Lets take a ClojureScript code, that invokes <code>PolarArea</code> function form the <code>Chart</code> object:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="o">^</span><span class="ss">:export</span> <span class="nv">creat-chart</span> <span class="p">[]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">ch</span> <span class="p">(</span><span class="nf">js/Chart.</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="k">. </span><span class="nv">ch</span> <span class="p">(</span><span class="nf">PolarArea</span> <span class="p">[]))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>after compilation this code will look similar to this one:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span><span class="p">(){</span><span class="k">return</span><span class="p">(</span><span class="k">new</span> <span class="nx">Chart</span><span class="p">).</span><span class="nx">Bc</span><span class="p">(</span><span class="nx">zc</span><span class="p">)}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, the <code>PolarArea</code> method was changed to <code>Bc</code> name, which of course will cause runtime error. To prevent this, we need to tell compiler which names shouldn&rsquo;t be changed. Those names should be defined in external JavaScript file  (i.e. <code>externs.js</code>) and provided to the compiler. For our example the <code>externs.js</code> file should look like this one:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">Chart</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'><span class="nx">Chart</span><span class="p">.</span><span class="nx">PolarArea</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{};</span>
</span></code></pre></td></tr></table></div></figure>


<p>The compiler should be informed about this file by <code>:externs</code> setting in <code>project.clj</code> file:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">{</span><span class="ss">:id</span> <span class="s">&quot;release&quot;</span>
</span><span class='line'>              <span class="ss">:source-paths</span> <span class="p">[</span><span class="s">&quot;src&quot;</span><span class="p">]</span>
</span><span class='line'>              <span class="ss">:compiler</span> <span class="p">{</span>
</span><span class='line'>                         <span class="ss">:main</span> <span class="nv">blog-sc-testing.core</span>
</span><span class='line'>                         <span class="ss">:output-to</span> <span class="s">&quot;out-adv/your-namespace.min.js&quot;</span>
</span><span class='line'>                         <span class="ss">:output-dir</span> <span class="s">&quot;out-adv&quot;</span>
</span><span class='line'>                         <span class="ss">:optimizations</span> <span class="ss">:advanced</span>
</span><span class='line'>                         <span class="ss">:externs</span> <span class="p">[</span><span class="s">&quot;externs.js&quot;</span><span class="p">]</span>
</span><span class='line'>                         <span class="ss">:pretty-print</span> <span class="nv">false</span><span class="p">}}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we do all those things, created JavaScript code will contain correct invocation of <code>PolarArea</code> function:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span><span class="p">(){</span><span class="k">return</span><span class="p">(</span><span class="k">new</span> <span class="nx">Chart</span><span class="p">).</span><span class="nx">PolarArea</span><span class="p">(</span><span class="nx">Ec</span><span class="p">)}</span>
</span></code></pre></td></tr></table></div></figure>


<p>To get more details about using external JavaScript libraries in ClojureScript I recommend you to read excellent blog post by Luke VanderHart about <a href="http://lukevanderhart.com/2011/09/30/using-javascript-and-clojurescript.html">this</a>.</p>

<p>As usual I&rsquo;m appreciated for any comments.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Defining Node.js task for Heroku Scheduler]]></title>
    <link href="http://www.spacjer.com//blog/2014/02/10/defining-node-dot-js-task-for-heroku-scheduler/"/>
    <updated>2014-02-10T16:36:44+01:00</updated>
    <id>http://www.spacjer.com//blog/2014/02/10/defining-node-dot-js-task-for-heroku-scheduler</id>
    <content type="html"><![CDATA[<p>For my pet project I&rsquo;ve needed to write a simple application, which checks if there is any data in specific table in my database and if there is, it sends me an email. After a few minutes of research I&rsquo;ve decided to use <a href="https://www.heroku.com/">Heroku service</a> for it. This was my first meeting with Heroku and I was curious how easy it would be to write an app.</p>

<p>Heroku supports Ruby, Node.js, Python and Java. From this list I feel quite comfortable with Node.js, so I&rsquo;ve chosen it.</p>

<p>In this blog post I guide you how to create a simple Node.js app, that can be used as a Heroku Scheduler task.</p>

<p>Before starting I suggest reading those articles from Heroku documentation:</p>

<ul>
<li><a href="https://devcenter.heroku.com/articles/getting-started-with-nodejs">Getting Started with Node.js on Heroku</a></li>
<li><a href="https://devcenter.heroku.com/articles/scheduler">Heroku Scheduler</a></li>
</ul>


<p>You also need to install <a href="https://toolbelt.heroku.com/">Heroku Toolbelt</a> and <a href="http://nodejs.org/">Node.js</a> (with <a href="https://npmjs.org/">npm package manager</a>) on your system.</p>

<h2>Getting Started</h2>

<p>Let&rsquo;s create a project directory:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>mkdir notyfication-sender</span></code></pre></td></tr></table></div></figure>


<p>with an empty git repository in it:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cd notyfication-sender
</span><span class='line'>git init</span></code></pre></td></tr></table></div></figure>


<p>Now we need to tell Heroku that we created Node.js app &ndash; this should be done by creating <code>package.json</code> <a href="https://npmjs.org/doc/json.html">file</a>. This file describes our application and defines all dependencies for it. To do this let&rsquo;s invoke command:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>npm init</span></code></pre></td></tr></table></div></figure>


<p>and answer to the questions. As the result the <code>package.json</code> file is generated.</p>

<p>This is good time to do our first commit:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>git add .
</span><span class='line'>git commit -m "init"</span></code></pre></td></tr></table></div></figure>


<h2>First deploy</h2>

<p>Now we are ready to create Heroku application. First we need to login to the service:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>heroku login</span></code></pre></td></tr></table></div></figure>


<p>then we can create the app:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>heroku create notyfication-sender</span></code></pre></td></tr></table></div></figure>


<p>If you want to use European server then you should add <code>--region eu</code> parameter to the <code>create</code> command.</p>

<p>If all is set up, let&rsquo;s do a deploy by pushing all our code from the git repository to Heroku server:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>git push heroku master</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s it! Our first, still empty app is ready to go &ndash; except there is no code ;)</p>

<h2>Installing add-ons</h2>

<p>Our application will use three add-ons:</p>

<ul>
<li>Heroku Postgres &ndash; for storing data and retrieving them</li>
<li>Heroku Scheduler &ndash; for running job every hour</li>
<li>SendGrid &ndash; for sending emails</li>
</ul>


<p>we need to add them to Heroku. This can be done by invoking:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>heroku addons:add heroku-postgresql:dev
</span><span class='line'>heroku addons:add scheduler
</span><span class='line'>heroku addons:add sendgrid</span></code></pre></td></tr></table></div></figure>


<p>One important note: to install add-ons you need to verify your Heroku account by providing information about valid credit card.</p>

<h2>Node.js dependecies</h2>

<p>To be able to use PostgreSQL and SendGrid in our JavaScript code, we need to install <code>npm packages</code> for them:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>npm install pg --save
</span><span class='line'>npm install sendgrid --save</span></code></pre></td></tr></table></div></figure>


<p>the <code>--save</code> argument adds those packages as a dependency to the <code>package.json</code> file &ndash; this helps installing/updating them in future.</p>

<h2>Scheduler</h2>

<p>You can find documentation for the scheduler <a href="https://devcenter.heroku.com/articles/scheduler">here</a>, but it lacks details about Node.js and it can take some time to figure out everything on your own.</p>

<p>First thing, a task should be placed in the <code>bin</code> folder in the root project directory:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>mkdir bin</span></code></pre></td></tr></table></div></figure>


<p>Second, the task should be written in a file without any extension (in our case it&rsquo;s <code>checkItems</code> file):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cd bin
</span><span class='line'>touch checkItems</span></code></pre></td></tr></table></div></figure>


<p>The last important thing is that the first line in the script file must contain <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> that defines which interpreter program is used to run the script (here: node.js):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>#!/usr/bin/env node</span></code></pre></td></tr></table></div></figure>


<p>Finally we are ready to write real code!</p>

<h2>Coding</h2>

<p>Let&rsquo;s open the <code>checkItems</code> file with our favorite editor. The file should contain only the shebang line.</p>

<p>At first we should require PostgreSQL (<a href="https://github.com/brianc/node-postgres/wiki">pg</a>) and <a href="https://devcenter.heroku.com/articles/sendgrid#node-js">SendGrid</a>  modules:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="err">#</span><span class="o">!</span><span class="err">/usr/bin/env node</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">pg</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;pg&#39;</span><span class="p">);</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">sendgrid</span>  <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;sendgrid&#39;</span><span class="p">)(</span>
</span><span class='line'>  <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">SENDGRID_USERNAME</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">SENDGRID_PASSWORD</span>
</span><span class='line'><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>process.env.SENDGRID_USERNAME</code> and <code>process.env.SENDGRID_PASSWORD</code> contains your <a href="http://sendgrid.com/">SendGrid</a> account information. Those environment variables are set by Heroku itself.</p>

<p>To connect to Postgres database we need to invoke code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">pg</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">DATABASE_URL</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">client</span><span class="p">,</span> <span class="nx">done</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>An important notice, to be able to use <code>DATABASE_URL</code> variable you need to promote your database. First we need to establish the exact URL of our database, to do it we need to execute command in the root folder:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>heroku pg:info</span></code></pre></td></tr></table></div></figure>


<p>It lists all available databases for you program, with values of URLs. The output should be similar to this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>=== HEROKU_POSTGRESQL_RED_URL
</span><span class='line'>Plan:        Dev
</span><span class='line'>Status:      available
</span><span class='line'>Connections: 0
</span><span class='line'>PG Version:  9.3.2
</span><span class='line'>Created:     2014-02-06 18:37 UTC
</span><span class='line'>Data Size:   6.4 MB
</span><span class='line'>Tables:      0
</span><span class='line'>Rows:        0/10000 (In compliance)
</span><span class='line'>Fork/Follow: Unsupported
</span><span class='line'>Rollback:    Unsupported</span></code></pre></td></tr></table></div></figure>


<p>now we can execute command:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>heroku pg:promote HEROKU_POSTGRESQL_RED_URL</span></code></pre></td></tr></table></div></figure>


<p>which sets <code>DATABASE_URL</code> variable to the value of <code>HEROKU_POSTGRESQL_RED_URL</code>.</p>

<p>I won&rsquo;t describe how to create tables and import data into them, you can read about this <a href="https://devcenter.heroku.com/articles/heroku-postgres-import-export#import">here</a>.</p>

<p>Let&rsquo;s return to <code>pg</code> module. There is one important thing to remember. When you finish your work with database, you have to invoke <code>done()</code> callback &ndash; otherwise the client will never be returned to the connection pool and you will leak <a href="https://github.com/brianc/node-postgres/wiki/pg#connectfunction-callback">clients</a>.</p>

<p>Before writing a query, lets write a function for error handling. We can use for that the code from <code>pg</code> <a href="https://github.com/brianc/node-postgres/wiki/Example">documentation</a>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">pg</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">DATABASE_URL</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">client</span><span class="p">,</span> <span class="nx">done</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">handleError</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">done</span><span class="p">(</span><span class="nx">client</span><span class="p">);</span>
</span><span class='line'>    <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'>  <span class="p">};</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>To query a table (I assume that there is <code>todos</code> table) in a database we can write such code:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">pg</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">DATABASE_URL</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">client</span><span class="p">,</span> <span class="nx">done</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">handleError</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">done</span><span class="p">(</span><span class="nx">client</span><span class="p">);</span>
</span><span class='line'>    <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'>  <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">client</span><span class="p">.</span><span class="nx">query</span><span class="p">(</span><span class="s1">&#39;SELECT * FROM todos&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="nx">handleError</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">client</span><span class="p">,</span> <span class="nx">done</span><span class="p">))</span> <span class="k">return</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nx">result</span><span class="p">.</span><span class="nx">rows</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">//send email</span>
</span><span class='line'>
</span><span class='line'>      <span class="nx">done</span><span class="p">();</span>
</span><span class='line'>      <span class="nx">pg</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>The idea here is to send an notification email only if there are any rows in <code>todos</code> table. Please pay attention that we invoke <code>done()</code> method when the query is done. I also invoke <code>pg.end();</code> to immediately close any connections to PostgreSQL server &ndash; I do this to save <a href="https://devcenter.heroku.com/articles/one-off-dynos">dynos</a> and close the app as fast as possible.</p>

<p>The last part is to write code that will send email with SendGrid module:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">sendgrid</span><span class="p">.</span><span class="nx">send</span><span class="p">({</span>
</span><span class='line'>    <span class="nx">to</span><span class="o">:</span> <span class="s1">&#39;my@email.com&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">from</span><span class="o">:</span> <span class="s1">&#39;app@email.com&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">subject</span><span class="o">:</span> <span class="s1">&#39;There are some items to do&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">text</span><span class="o">:</span> <span class="s1">&#39;You have items to do&#39;</span>
</span><span class='line'>  <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">json</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>so the whole code looks like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="err">#</span><span class="o">!</span><span class="err">/usr/bin/env node</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">pg</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;pg&#39;</span><span class="p">);</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">sendgrid</span>  <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;sendgrid&#39;</span><span class="p">)(</span>
</span><span class='line'>  <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">SENDGRID_USERNAME</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">SENDGRID_PASSWORD</span>
</span><span class='line'><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">pg</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">DATABASE_URL</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">client</span><span class="p">,</span> <span class="nx">done</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">handleError</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">done</span><span class="p">(</span><span class="nx">client</span><span class="p">);</span>
</span><span class='line'>    <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'>  <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">client</span><span class="p">.</span><span class="nx">query</span><span class="p">(</span><span class="s1">&#39;SELECT * FROM todos&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="nx">handleError</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">client</span><span class="p">,</span> <span class="nx">done</span><span class="p">))</span> <span class="k">return</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nx">result</span><span class="p">.</span><span class="nx">rows</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="nx">sendgrid</span><span class="p">.</span><span class="nx">send</span><span class="p">({</span>
</span><span class='line'>          <span class="nx">to</span><span class="o">:</span> <span class="s1">&#39;my@email.com&#39;</span><span class="p">,</span>
</span><span class='line'>          <span class="nx">from</span><span class="o">:</span> <span class="s1">&#39;app@email.com&#39;</span><span class="p">,</span>
</span><span class='line'>          <span class="nx">subject</span><span class="o">:</span> <span class="s1">&#39;There are some items to do&#39;</span><span class="p">,</span>
</span><span class='line'>          <span class="nx">text</span><span class="o">:</span> <span class="s1">&#39;You have items to do&#39;</span>
</span><span class='line'>        <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">json</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>            <span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'>          <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>          <span class="nx">done</span><span class="p">();</span>
</span><span class='line'>          <span class="nx">pg</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
</span><span class='line'>      <span class="p">});</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Please notice, that I&rsquo;ve moved <code>done(); pg.end();</code> code to the callback of <code>send</code> method.</p>

<p>To run and test the code we should deploy it on the server and run:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>git add .
</span><span class='line'>git commit -m "Code for scheduler task"
</span><span class='line'>git push heroku master
</span><span class='line'>heroku run checkItems</span></code></pre></td></tr></table></div></figure>


<p>If everything is OK, the code should run without any errors.</p>

<p>This is very simple code, that illustrate only the way of doing such task. For a production ready it should be extended and more tested.</p>

<p>Now when we have a code of our scheduler task, we can set scheduler on Heroku site.</p>

<h2>Setting Heroku scheduler</h2>

<p>To configure scheduler we need to go to its dashboard page by invoking command:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>heroku addons:open scheduler</span></code></pre></td></tr></table></div></figure>


<p><a href='http://www.spacjer.com//images/posts/2014/02/heroku_scheduler_empty.jpg' rel='lightbox[HerokuScheduler]'><img class='center' src='http://www.spacjer.com//images/posts/2014/02/scheduler_empty_small.png' alt='Heroku Scheduler page'></a></p>

<p>On the page click <code>Add Job...</code> link, in the text box write the name of the file (without any extension) that defines the task and is located in the <code>bin</code> folder (in our case it is: <code>checkItems</code>). From the drop-down list select the frequency and adjust next run time. Commit you changes by clicking the <code>save</code> button.</p>

<p><a href='http://www.spacjer.com//images/posts/2014/02/heroku_scheduler_add.jpg' rel='lightbox[HerokuScheduler]'><img class='center' src='http://www.spacjer.com//images/posts/2014/02/scheduler_add_small.png' alt='Heroku Scheduler page'></a></p>

<p>This is it, you defined your scheduler task. From now on it will run every defined period.</p>

<p><a href='http://www.spacjer.com//images/posts/2014/02/heroku_scheduler_added.jpg' rel='lightbox[HerokuScheduler]'><img class='center' src='http://www.spacjer.com//images/posts/2014/02/scheduler_added_small.png' alt='Heroku Scheduler page'></a></p>

<p>I hope this article will help you create your own, custom task for Heroku scheduler. Enjoy!</p>

<p>You can clone this code from my GitHub <a href="https://github.com/rspacjer/notyfication-sender">repository</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[NDepend 5]]></title>
    <link href="http://www.spacjer.com//blog/2013/10/31/ndepend-5/"/>
    <updated>2013-10-31T18:58:00+01:00</updated>
    <id>http://www.spacjer.com//blog/2013/10/31/ndepend-5</id>
    <content type="html"><![CDATA[<p>I&rsquo;ve written about <a href="http://www.ndepend.com/">NDepend</a> on my blog in <a href="http://www.spacjer.com/blog/2013/08/11/ndepend/">August</a> and since then the new version (5) has been released. This is a major update, with many changes and improvements. In this text I focus on parts that are most interesting for me.</p>

<h2>Trends</h2>

<p>When we work with the code, we introduce changes, which affect whole code base. We can increase or decrease number of lines of code, complexity of the methods, cohesion of methods and many more. All of those informations can be calculated and shown by NDepend. Those statistics show only the condition of the project in the current point of time. It would be very helpful to know how those metrics change in time. What was the complexity two weeks age? How many types did we have one month ago? If we could compare current values with the historical ones we could say if our changes are good for the project, if the quality of the code is increasing. In essence the &lsquo;Trends&rsquo; feature give us such possibilities.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/10/trend.png' rel='lightbox[ndepend5]'><img class='center' src='http://www.spacjer.com//images/posts/2013/10/trend.png' alt='Trend chart'></a></p>

<center><sup>(The image is from <a href="http://www.ndepend.com/ndependv5.aspx">NDepend site</a>, because I didn&#8217;t have enough data to present a nice chart)</sup></center>


<p></p>

<p>NDepend has now the ability to store it&rsquo;s own analyzed data and base on it computes charts. On those charts we can see changes of various code metrics in time. The longer we work with NDepend (by default the trends log are calculated once a day) the more accurate our charts are.</p>

<p>By default the tool comes with a set of predefined trend queries. As usual, with NDepend, you can write your own queries (using <a href="http://www.ndepend.com/Doc_CQLinq_Syntax.aspx">CQLinq</a>) and use them.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/10/trends_queries.jpg' rel='lightbox[ndepend5]'><img class='center' src='http://www.spacjer.com//images/posts/2013/10/trends_queries.jpg' alt='Trend queries'></a></p>

<p>The trends became my favorite, new, feature of NDepend. I can say, that I&rsquo;m a bit addicted with them. At least once a day I like to spend a few minutes on analyzing them. This gives me a good overview of a whole project.</p>

<h2>Dashboard</h2>

<p>It is always nice to have one, central, place where you can look and see the most important things. NDepend5 introduces such a place in the form of a &lsquo;Dashboard&rsquo; view.</p>

<p>The dashboard contains predefined section with basic statistics and code rules. In addition we can change the view by adding various trends charts to it. It is worth mentioning that every chart view can be fully customized.</p>

<p>The dashboard is a nice starting point for using NDepend. For the new users it can also lower the learning curve, giving the advices which features are the most important and should be checked.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/10/dashboard.jpg' rel='lightbox[ndepend5]'><img class='center' src='http://www.spacjer.com//images/posts/2013/10/dashboard_thumb.jpg' alt='Dashboard'></a></p>

<h2>New look and feel</h2>

<p>The user interface has been completely redesigned in the spirit of &lsquo;flat&rsquo; design principles. It uses pastel colors on white backgrounds, clear and easy to read fonts. The GUI is now coherent with Visual Studio 2012/2013 style. When you work with NDepend5 you feel that it&rsquo;s part of Visual Studio and not a separate add-in.</p>

<p>Still there is a small place to improvement in &lsquo;Metrics&rsquo; and &lsquo;Matrix&rsquo; views, which, in my opinion, stand out from the rest of the design (maybe it&rsquo;s because of the colors and the textures?).</p>

<p>Overall I&rsquo;m pleased with those changes.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/10/report.jpg' rel='lightbox[ndepend5]'><img class='center' src='http://www.spacjer.com//images/posts/2013/10/report_thumb.jpg' alt='NDepend report'></a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Installing Clojure on Nitrous.IO platform]]></title>
    <link href="http://www.spacjer.com//blog/2013/09/27/installing-clojure-on-nitrousio-platform/"/>
    <updated>2013-09-27T20:15:00+02:00</updated>
    <id>http://www.spacjer.com//blog/2013/09/27/installing-clojure-on-nitrousio-platform</id>
    <content type="html"><![CDATA[<p>I&rsquo;ve evaluated <a href="https://www.nitrous.io/">Nitrous.IO</a> service for a few weeks now. In a nutshell it allows you to create virtual, development, environment, to which you can connect remotely (using the <a href="http://help.nitrous.io/getting-started/">terminal</a>, but also the <a href="http://help.nitrous.io/ide-general/">web page</a>, chrome app or even <a href="https://www.nitrous.io/mac">Mac app</a>). By default Nitrous.IO comes with preconfigured boxes with Ruby/Rails, Node.js, Python/Django and Go.</p>

<p>I&rsquo;ve started using Nitrous, because I wanted to have an easy and fast access to programming languages without installing them on my Windows machine.</p>

<p>Lately I try to get my head around <a href="http://clojure.org/">Clojure</a> language (I&rsquo;ve learned functional programming during my studies, but it was a long time ago, so I have to discover it again). Because of that I wanted to install <code>Clojure</code> on the Nitrous.IO platform. It isn&rsquo;t hard, but requires few steps to do. Below I will show you, how you can do it (this tutorial is inspired by <a href="http://help.nitrous.io/faq-erlang/">&lsquo;Installing Erlang&rsquo; guide</a>).</p>

<p>Let&rsquo;s start:</p>

<ul>
<li><p>Create a new development box (I&rsquo;ve used the one with the <code>Go</code> language, but it doesn&rsquo;t matter) or use the existing one.</p></li>
<li><p>Connect to the box &ndash; you can even use the web page &ndash; start terminal then.</p></li>
<li><p>The easiest way to install Clojure on any system is to use <a href="http://leiningen.org/">Leiningen</a> tool. First lets make a folder where we will store installation script:</p></li>
</ul>


<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>mkdir ~/.tools</span></code></pre></td></tr></table></div></figure>


<p>then we should get the script:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cd .tools
</span><span class='line'>wget https://raw.github.com/technomancy/leiningen/stable/bin/lein</span></code></pre></td></tr></table></div></figure>


<p></p>

<ul>
<li>Once the script is downloaded, we need to modify <code>~/.bashrc</code> file to add the <code>.tools</code> directory to our <code>$PATH</code> &ndash; we can do it with the <code>vim</code>:</li>
</ul>


<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>vim ~/.bashrc</span></code></pre></td></tr></table></div></figure>


<p>add this line to the end of the file:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>PATH=$PATH:$HOME/.tools/</span></code></pre></td></tr></table></div></figure>


<p>save and exit.</p>

<ul>
<li>Reload the <code>~/.bashrc</code> file:</li>
</ul>


<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>source ~/.bashrc</span></code></pre></td></tr></table></div></figure>


<p>and check if the $PATH contains <code>.tool</code> directory</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>echo $PATH</span></code></pre></td></tr></table></div></figure>


<p>the output should be similar to this one:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/action/.gem/ruby/1.9.1/bin:/home/action/.go/bin:/home/action/workspace/bin:/home/action/.google_appengin
</span><span class='line'>e:/home/action/.go/bin:/home/action/workspace/bin:/home/action/.google_appengine:/home/action/.tools/</span></code></pre></td></tr></table></div></figure>


<ul>
<li>Now we need to set Leiningen script to be executable</li>
</ul>


<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>chmod a+x ~/.tools/lein</span></code></pre></td></tr></table></div></figure>


<p>and we are ready to go.</p>

<ul>
<li>To start using Clojure REPL (read-eval-print loop) type:</li>
</ul>


<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>lein repl</span></code></pre></td></tr></table></div></figure>


<p>For the first time Leiningen will download and install <code>Clojure</code>, after it finishes you can start playing with the new language.</p>

<p>If you need more information about Leiningen, you can read the <a href="https://github.com/technomancy/leiningen/blob/stable/doc/TUTORIAL.md">documentation.</a></p>

<p>A small tip if you work in the web page terminal, you can copy or past using those <a href="http://help.nitrous.io/faq-copy-paste/">shortcuts</a>:</p>

<p>Windows Users:<br/>
Copy:  <code>Shift + Ctrl + c</code><br/>
Paste: <code>Shift + Ctrl + v</code></p>

<p>Mac Users:<br/>
Copy:  <code>Command + c</code><br/>
Paste: <code>Command + v</code></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[DevDay 2013]]></title>
    <link href="http://www.spacjer.com//blog/2013/09/24/devday-2013/"/>
    <updated>2013-09-24T20:27:00+02:00</updated>
    <id>http://www.spacjer.com//blog/2013/09/24/devday-2013</id>
    <content type="html"><![CDATA[<p>Last Friday (20th of September 2013) I was at the <a href="http://devday.pl/">DevDay</a> conference in Krakow, Poland. This was a free event, but &ndash; in my humble opinion &ndash; it could easily compete with the paid ones and it would probably won.</p>

<p>First of all, the conference organizers invited well known and good <a href="http://devday.pl/Speakers">speakers</a>. Second there was superior service (lots of good food and drinks) and a crew willing to help (cheers for the debugging team!). Ending on the enthusiastic participants.</p>

<p>The conference started at 8am and took whole day. Expect the first and the last talk, two sessions were taking place simultaneously.</p>

<p>I&rsquo;ve attended to those:</p>

<ul>
<li><strong>&ldquo;Back to basics: the mess we&rsquo;ve made of our fundamental data types&rdquo;</strong> by <a href="http://msmvps.com/blogs/jon_skeet/">Jon Skeet</a></li>
</ul>


<p>  A funny presentation about how even simple things in programming (like float, string, date and time types) can be complex when you use them without understanding. Additional benefit: you could see how Jon spoke with &ldquo;Tony the pony&rdquo; ;)</p>

<ul>
<li><strong>&ldquo;Implementing Continuous Delivery&rdquo;</strong> by <a href="http://www.thekua.com/atwork/">Patrick Kua</a></li>
</ul>


<p>  An introduction to the <a href="http://en.wikipedia.org/wiki/Continuous_delivery">continuous delivery</a> subject &ndash; what it is, how to use it and what are the benefits. I had some basics about CD, so it was interesting for me.</p>

<ul>
<li><strong>&ldquo;Guerilla Framework Design&rdquo;</strong> by <a href="http://elegantcode.com/about/andreas-hakansson/">Andreas Håkansson</a></li>
</ul>


<p>  One of the creators of the Nancy framework talked about how simple and clever design &ldquo;tricks&rdquo; can encourage developers to use API their created. This talk gave me few ideas and I will definitely use them in my code.</p>

<ul>
<li><strong>&ldquo;Building Startups and Minimum Viable Products&rdquo;</strong> by <a href="http://blog.benhall.me.uk/">Ben Hall</a></li>
</ul>


<p>  If you think about a startup, then this talk is for you. Simplifying the essence, you should focus on creating product fast and evaluating it in real world &ndash; waiting one year (or even more) with the release isn&rsquo;t a good idea. I&rsquo;ve listened this talk with the enjoyment.</p>

<ul>
<li><strong>&ldquo;Full-text search with Lucene and neat things you can do with it&rdquo;</strong> by <a href="http://code972.com/blog">Itamar Syn-Hershko</a></li>
</ul>


<p>  I’m Lucene user at work and I&rsquo;ve also learned about searching during my studies, the first part of the talk was not so interesting for me, but the second part about Elastic Search makes that I&rsquo;m very happy that I attended to this session. Itamar is one of the Apache Lucene contributor and a good speaker, so I recommend watching this talk.</p>

<ul>
<li><strong>&ldquo;The Architecture of StackOverflow&rdquo;</strong> by <a href="https://twitter.com/sklivvz">Marco Cecconi</a></li>
</ul>


<p>  An overview about architecture of a site, that every developer probably know (and if not, then he should!). You could learn some interesting facts about the team and the way they create this great website.</p>

<ul>
<li><strong>&ldquo;The software journeyman&rsquo;s guide to being homeless and jobless.&rdquo;</strong> by <a href="http://codeofrob.com/">Rob Ashton</a></li>
</ul>


<p>  An inspiring, funny and probably too short talk about Rob&rsquo;s one year journey during which he met people, coded and drank ;) I have to say that Rob has lot&rsquo;s of charisma and is a very good speaker, so you listen him with the real pleasure. Be aware: if you&rsquo;re Belgian then this talk probably isn&rsquo;t for you ;)</p>

<hr />

<p>I&rsquo;m very happy that I was invited for this conference and I hope I will be there in the next year. One more time thank you guys for creating such a great event!</p>

<p><a href='http://www.spacjer.com//images/posts/2013/09/devday.jpg' rel='lightbox[devday]'><img  src='http://www.spacjer.com//images/posts/2013/09/devday_thumb.jpg' alt='Dev Day 2013'></a>
<a href='http://www.spacjer.com//images/posts/2013/09/JonSkeet.jpg' rel='lightbox[devday]'><img  src='http://www.spacjer.com//images/posts/2013/09/JonSkeet_thumb.jpg' alt='Jon Skeet'></a>
<a href='http://www.spacjer.com//images/posts/2013/09/PatrickKua.jpg' rel='lightbox[devday]'><img  src='http://www.spacjer.com//images/posts/2013/09/PatrickKua_thumb.jpg' alt='Patrick Kua'></a>
<a href='http://www.spacjer.com//images/posts/2013/09/RobAshton.jpg' rel='lightbox[devday]'><img  src='http://www.spacjer.com//images/posts/2013/09/RobAshton_thumb.jpg' alt='Rob Ashton'></a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[NDepend]]></title>
    <link href="http://www.spacjer.com//blog/2013/08/11/ndepend/"/>
    <updated>2013-08-11T19:13:00+02:00</updated>
    <id>http://www.spacjer.com//blog/2013/08/11/ndepend</id>
    <content type="html"><![CDATA[<p>As I wrote previously I’m a big fan of tools and programs that helps me in my day-to-day work. I also like to test new programs, which is probably the reason that <a href="http://codebetter.com/patricksmacchia/">Patrick Smacchia</a> contacted me with a proposition to evaluate his program: <a href="http://www.ndepend.com/"><strong>NDepend</strong></a>.</p>

<p>In simple words NDepend is a tool that helps improve .Net code quality, by measuring and presenting information about code metrics. It supports C# and Visual Basic languages and integrates nicely with Visual Studio (from 2008 to 2012), providing an easy usage for users.</p>

<h2>Functionality</h2>

<p>The list of features that NDepend provides is quite big. From my perspective the most interesting parts are:</p>

<ul>
<li>Metrics</li>
<li>Queries and Rules Explorer/Editor</li>
<li>Dependency Matrix/Graph</li>
</ul>


<p>in addition there are also modules for:</p>

<ul>
<li>Code comparison</li>
<li>Test coverage</li>
<li>Searching</li>
</ul>


<p>In this blog post I will focus only on the main functionality.</p>

<h2>Metrics</h2>

<p>In our life we can describe objects by simply telling about their properties (like mass, dimension, speed and so on). Using those properties we can compare objects and evaluate which of them is better (for example: which car is faster). In science and engineering we use predefined <a href="http://en.wikipedia.org/wiki/Systems_of_measurement">system of measurement</a> to define item attributes, where every attribute is expressed in its own <a href="http://en.wikipedia.org/wiki/Units_of_measurement">unit of measure</a> (like kilogram, meter&hellip;).</p>

<p>The same approach is introduced in computer science &ndash; for the last 30 years, or even more, researchers introduce many <a href="http://en.wikipedia.org/wiki/Software_metric">metrics</a> that can describe software and code. Those metrics can tell us:</p>

<ul>
<li>how hard is to introduce changes

<ul>
<li>nesting Depth</li>
<li>afferent coupling</li>
<li>efferent coupling</li>
</ul>
</li>
<li>how large and complex the code is

<ul>
<li>number of lines of code</li>
<li>cyclomatic complexity</li>
</ul>
</li>
<li>how good the code is documented

<ul>
<li>lines of comment</li>
</ul>
</li>
</ul>


<p>… naming only few from the <strong>big</strong> list.</p>

<p>NDepend offers 82 most popular <a href="http://www.ndepend.com/Features.aspx#Metrics">code metrics</a> that can be calculated and shown.</p>

<p>Because of hierarchical nature of code elements (fields and methods are in type, type is in namespace, which in in assembly) the metrics are shown using <a href="http://en.wikipedia.org/wiki/Treemapping">treemapping</a> technique. Depending of selected scope (level) code element is represented by rectangle, which size is determined by the code metric. The rectangle can contain other, smaller rectangles (i.e. namespace has methods). This allows to easily spot problems and <a href="http://www.ndepend.com/Doc_Treemap.aspx#Scenarios">patterns</a>. For example if we select a &lsquo;method&rsquo; as our scope and &lsquo;Lines of code&rsquo; as a metric, the big rectangles will indicate the methods with many lines of code (usually this isn&rsquo;t good and those methods should be split to smaller ones). The metrics view is one of the most unique and helpful parts of NDepend.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/08/metrics.jpg' rel='lightbox[metrics]'><img class='center' src='http://www.spacjer.com//images/posts/2013/08/metrics_thumb.jpg' alt='Treemap'></a></p>

<p>I should mention that some (not many) metrics can&rsquo;t be calculated and shown for code written in Visual Basic, so if you use VB please be warned.</p>

<h2>Queries and Rules Explorer</h2>

<p>In essence NDepend provides mechanism for querying your code base for various <a href="http://www.ndepend.com/metrics.aspx]">code metrics</a> and problems that occurs in it. To achieve it, it defines CQLinq (Code Query over LINQ) language, that should be familiar for every .Net developer. For example to find all methods that have more then 30 lines of code we could write such a query:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">from</span> <span class="n">m</span> <span class="k">in</span> <span class="n">JustMyCode</span><span class="p">.</span><span class="n">Methods</span> <span class="k">where</span>
</span><span class='line'>   <span class="n">m</span><span class="p">.</span><span class="n">NbLinesOfCode</span> <span class="p">&gt;</span> <span class="m">30</span>
</span></code></pre></td></tr></table></div></figure>


<p>By default NDepend comes with big number of predefine queries which cover such topics as:</p>

<ul>
<li>Code quality</li>
<li>Object oriented design</li>
<li>Architecture</li>
<li>Dead code</li>
<li>Naming conventions</li>
</ul>


<p>and more&hellip;</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2013/08/rules.jpg" title="[Queries and Rules Explorer]" ></p>

<p>Those queries help spot the places in code that should be improved: methods split into smaller ones, types renamed, complexity reduced and so on. Working with NDepend creates a workflow, where you look for the problem, fix it and then check again if the metrics are improved. After some time your code should be cleaner and easier to maintain.</p>

<p>The nice part about those queries is that you can modify them and even write your own. This gives you full flexibility in adjustment
the tool to your needs.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/08/query.jpg' rel='lightbox[queries]'><img class='center' src='http://www.spacjer.com//images/posts/2013/08/query_thumb.jpg' alt='Query editor'></a></p>

<h2>Dependency Graph</h2>

<p>When the application comes bigger and bigger it is harder to see the big picture of it. We lost idea about dependences in it. The easiest way to see them is to paint a graph, that will reveal all the dependencies. Here comes NDepend, it can create such a graph for us. The graph can have many levels, that shows dependences between assemblies, namespaces, types or even members (methods and fields).</p>

<p><a href='http://www.spacjer.com//images/posts/2013/08/graph.jpg' rel='lightbox[graph]'><img class='center' src='http://www.spacjer.com//images/posts/2013/08/graph_thumb.jpg' alt='Dependency Graph'></a></p>

<p>When we work with NDepend, in Visual Studio, we can use it to get various information about the code elements (types, fields, methods, namespaces or assemblies) in the solution. We can query about  direct and indirect callers/callees, inheritors, implementers, type usage and so on. All such queries can be shown in a graph, which is nice and helpful addition.</p>

<p>There is only one problem with the graph representation, it becomes blurry, when there is too much objects in it. I had such problem when I used NDepend with large and quite legacy project &ndash; the graph was so big that I wasn&rsquo;t able to read it. I even ask Patrick what should I do, to be able to work with the graph in this project. He pointed me that I should use dependency matrix instead.</p>

<h2>Dependency Matrix</h2>

<p>As you probably know a graph can be represented as <a href="http://en.wikipedia.org/wiki/Adjacency_matrix">adjacency matrix</a>. This idea was used by NDepend to present a solution dependency graph as a matrix. The main benefit of such representation is that it is more compact and clear to read. This is especially important when you have many assemblies in your solution &ndash; in such case graph is too big to be easily read.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/08/matrix.jpg' rel='lightbox[matrix]'><img class='center' src='http://www.spacjer.com//images/posts/2013/08/matrix_thumb.jpg' alt='Dependency Graph'></a></p>

<p>At first I didn&rsquo;t feel comfortable with dependency matrix and it took me a while to be able to read it correctly. The context-sensitive help speeds the process of learning and after few days the matrix was completely natural for me.</p>

<p>There is also one, big, advantage of dependency matrix over the graph. The matrix allows you to spot the structural patterns, which are nice described in the <a href="http://www.ndepend.com/Doc_Matrix.aspx#Patterns">documentation</a> &ndash; that I highly recommend you to read.</p>

<h2>Visual Studio integration</h2>

<p>By default NDepend comes as a standalone application called <code>Visual NDepend</code>, in addition you can integrate it with Visual Studio. After installing plug-in, you see new menu in the main bar, new context menu in <em>Solution Explorer</em> and few other items here and there. From the menu you have access to all features of NDepend, you can start:</p>

<ul>
<li>Class Browser</li>
<li>Search</li>
<li>Queries and Rule Explorer/Editor</li>
<li>Dependency Matrix and Graph</li>
<li>Metrics</li>
<li>Analysis</li>
<li>Compare</li>
<li>Coverage by Tests</li>
</ul>


<p>and few others.</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2013/08/main_menu.jpg" title="[NDepend's main menu in Visual Studio]" ></p>

<p>To be able to use NDepend in Visual Studio we need to create new project file and add to it all assemblies, that we want to analyze. The project file can be added to the solution file, but this isn&rsquo;t required. After the analyze is finished the NDepend is ready to go. It is worth mentioning that an HTML report, that summarize all information about your code, is generated after the analyze ends.</p>

<p>The integration is nice and gives impression of well organized. For sure this is very strong point of this tool.</p>

<h2>Summary</h2>

<p>NDepend is very interesting product. It offers wide range of features focused on improving the quality of the code you and your team write.</p>

<p>There is also another, very useful, usage of NDepend that I would like to mention. Sometimes in my work I get a source code from completely unknown project and I need to estimate how much work is to introduce changes to it. In such case I start NDepend, run queries and look at the metrics &ndash; if those aren&rsquo;t bad I can assume that changing this code won&rsquo;t be so hard. Of course this isn&rsquo;t always true, but if you need to do the time estimation, everything can be helpful.</p>

<p><em>Note: I&rsquo;ve used source code from <a href="http://nancyfx.org/">Nancy project</a> in the presented screenshots</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Making mechanical keyboard less loud]]></title>
    <link href="http://www.spacjer.com//blog/2013/07/12/making-mechanical-keyboard-less-loud/"/>
    <updated>2013-07-12T21:11:00+02:00</updated>
    <id>http://www.spacjer.com//blog/2013/07/12/making-mechanical-keyboard-less-loud</id>
    <content type="html"><![CDATA[<p>The thing, I&rsquo;ve complained about in my review of the <code>Tesoro Durandal G1N</code> keyboard was the noise. It was especially disturbing late night, when I was still working on my PC and my wife wanted to sleep ;). To solve this problem I&rsquo;ve decided to reduce the noise of my keyboard by applying sound dampeners &ndash; a special soft rubber O-rings that are installed on every keycap stem.</p>

<h2>O-rings</h2>

<p>After some searching I&rsquo;ve found that good quality O-rings can be bought from the <a href="http://www.wasdkeyboards.com/index.php/">WASD Keyboards company</a>. This is an American store, but the shipping cost, even for European clients, is quite low &ndash; sending it by <em>&lsquo;USPS First-Class International&rsquo;</em> (normal envelope) costs around $7 and it takes about 10 days to ship.</p>

<p>As for today the shop offers two types of <a href="http://www.wasdkeyboards.com/index.php/cherry-mx-rubber-switch-dampeners.html">O-rings</a>:<br/>
&ndash; <code>40A-L (0.2mm Reduction) Red</code><br/>
&ndash; <code>40A-R (0.4mm Reduction) Blue</code></p>

<p><a href='http://www.spacjer.com//images/posts/2013/07/oring1.png' rel='lightbox[oring]'><img  src='http://www.spacjer.com//images/posts/2013/07/oring1_thumb.png' alt='40A-L (0.2mm Reduction) Red'></a>
<a href='http://www.spacjer.com//images/posts/2013/07/oring3.png' rel='lightbox[oring]'><img  src='http://www.spacjer.com//images/posts/2013/07/oring3_thumb.png' alt='40A-L (0.2mm Reduction) Red'></a></p>

<p>I&rsquo;ve bought the <code>40A-L red ones</code>, because they reduce the noise of the keyboard with only minimal impact on the writing experience. Right now I&rsquo;ve been using them for more than a month and I&rsquo;m really happy with the effect. When I hit the key the sound isn&rsquo;t so &ldquo;plastic&rdquo; as before and it&rsquo;s much more deaden. Still I can easily feel the moment when the key switch has been activated.</p>

<p>Unfortunately I wasn&rsquo;t able to test the blue one (40A-R &ndash; 0.4mm reduction), so I can&rsquo;t write about the differences between them.</p>

<h2>Installation</h2>

<p>O-rings installation is a bit painful, because you have to remove every keycap, put the O-ring into the stem and then put it back into keyboard. To see how to do it properly I recommend you to watch this <a href="http://www.youtube.com/watch?feature=player_detailpage&amp;v=NsZqAU9u0-w">short movie</a> created by WASD company. It took me about one hour to install them on all stems. To pull out the keycap I&rsquo;ve used two thin wires, that I put on the opposite sides of the keycap and then pull them up. After 10 keys you can do it without thinking ;)</p>

<h2>Final words</h2>

<p>If you like your mechanical keyboard, but you think it&rsquo;s too loud, you should use those O-rings and probably you will be happy with the result &ndash; at least I&rsquo;m.</p>

<p><a href='http://www.spacjer.com//images/posts/2013/07/oring2.png' rel='lightbox[oring]'><img  src='http://www.spacjer.com//images/posts/2013/07/oring2_thumb.png' alt='40A-L (0.2mm Reduction) Red'></a>
<a href='http://www.spacjer.com//images/posts/2013/07/oring4.png' rel='lightbox[oring]'><img  src='http://www.spacjer.com//images/posts/2013/07/oring4_thumb.png' alt='40A-L (0.2mm Reduction) Red'></a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Tesoro Durandal G1N Mechanical Keyboard Review]]></title>
    <link href="http://www.spacjer.com//blog/2013/04/20/tesoro-durandal-g1n-mechanical-keyboard-review/"/>
    <updated>2013-04-20T13:05:00+02:00</updated>
    <id>http://www.spacjer.com//blog/2013/04/20/tesoro-durandal-g1n-mechanical-keyboard-review</id>
    <content type="html"><![CDATA[<p>A few weeks ago I&rsquo;ve decided to change my keyboard. For the last two or three years I&rsquo;ve been using Microsoft Wired Keyboard 600, which is nice, low profile keyboard. During my research for the new keyboard I&rsquo;ve found many articles about mechanical keyboards and how good they are in the comparison to &ldquo;traditional rubber domes&rdquo; ones. This convinced me to buy one. Still there was a question about exact model. So again I&rsquo;ve spent some time reading materials about key switches and their purpose (I can recommend this <a href="http://www.overclock.net/t/491752/mechanical-keyboard-guide">thread</a> &ndash; it contains lots of useful information). The final decision was to buy keyboard with <a href="http://deskthority.net/wiki/Cherry_MX_Brown">Cherry MX Brown switches</a>.</p>

<p>Unfortunately in Europe and especially in Poland there is problem with getting mechanical keyboard, not saying about the possibility of choosing the type of keys switches. You can buy some keyboards with <a href="http://deskthority.net/wiki/Cherry_MX_Red">Cherry MX Redswitches</a>, because those are used in the &ldquo;gaming keyboards&rdquo; and therefore more popular. Of course I could import a keyboard from the USA, but then the price would be too big for me &ndash; the shipping cost is pretty big, plus I would need to pay duty and value added tax. All of this makes that I bought <strong>Tesoro</strong> (in the United States it&rsquo;s <strong>Max Keyboard</strong>) <a href="http://www.maxkeyboard.com/max-keyboard-durandal-g1n-mechanical-keyboard.html">Durandal G1N</a> mechanical keyboard, which has just shown on the European and Polish market.</p>

<h2>Quality</h2>

<p>The keyboard has simple, US international layout. There is one additional key &ndash; <code>Fn</code> which allows using multimedia functions keys that are mapped to keys from <code>F1</code> to <code>F6</code> (mute, change volume, play, pause, rewind). The keyboard looks almost like the &ldquo;normal&rdquo; one, except the right, upper corner, which is a bit bigger and has lighting sign of <a href="http://www.tesorotec.com/">Tesoro</a> company. You can also see the name of the brand on the bottom of the keyboard, just beneath the space bar. The maximal dimension of the keyboard is 46 cm (18.1 in) length and 17 cm (6.7 in) width. The front case imitate brushed metal, which looks good and prevents leaving fingerprints. On the back of the case there are rubberized elements that prevents slipping keyboard on the table. The keyboard itself is heavy and made from good quality of plastic. It has braided cable, which is also a nice addition. In summary I can say, that I&rsquo;m pleased with the quality of my new keyboard.</p>

<h2>Writing experience</h2>

<p>This is my first mechanical keyboard and I have to admit that writing on it is a real pleasure. You can easily feel the moment when the key switch has been activated (for the <em>MX Brown switch</em> it&rsquo;s in the half way through the key press) and you can release it. This allows you to use less force to write something on the keyboard. The result is that you can write faster and your hands are less exhausted. Unfortunately I haven&rsquo;t used any other mechanical keyboard so I can&rsquo;t compare this one with others &ndash; maybe in the future I will be able to do this ;)</p>

<p>I&rsquo;ve decided to buy keyboard with the brown switches because they are advertiser as quieter than the blue ones (for the blue switches you can hear the &lsquo;click&rsquo; sound when you press the key), but still good for typing. Despite of it I have to admit that this keyboard is quite loud. For me this isn&rsquo;t a problem, but for my wife it is ;) She complains about the noise, so I have to close the door to my room when I&rsquo;m typing a lot of text or playing any game. To solve this problem I&rsquo;m going to buy the <em>rubber o-ring switch dampeners</em> to reduce the noise. When I do this I will try to write something about it on my blog.</p>

<h2>Conclusion</h2>

<p>If you are looking for good and not expensive keyboard for writing and occasionally playing games I can recommend you the Tesoro Durandal G1N keyboard. I&rsquo;ve been using it for more than a month and I&rsquo;m very happy with it. My wife apparently not &hellip;</p>

<p><a href='http://www.spacjer.com//images/posts/2013/04/Tesoro_Durandal_G1N_1.jpg' rel='lightbox[tesoro]'><img  src='http://www.spacjer.com//images/posts/2013/04/Tesoro_Durandal_G1N_1_thumb.jpg' alt='Tesoro Durandal G1N'></a>
<a href='http://www.spacjer.com//images/posts/2013/04/Tesoro_Durandal_G1N_2.jpg' rel='lightbox[tesoro]'><img  src='http://www.spacjer.com//images/posts/2013/04/Tesoro_Durandal_G1N_2_thumb.jpg' alt='Tesoro Durandal G1N'></a>
<a href='http://www.spacjer.com//images/posts/2013/04/Tesoro_Durandal_G1N_3.jpg' rel='lightbox[tesoro]'><img  src='http://www.spacjer.com//images/posts/2013/04/Tesoro_Durandal_G1N_3_thumb.jpg' alt='Tesoro Durandal G1N'></a>
<a href='http://www.spacjer.com//images/posts/2013/04/mx_cherry_brown.jpg' rel='lightbox[tesoro]'><img  src='http://www.spacjer.com//images/posts/2013/04/mx_cherry_brown_thumb.jpg' alt='Cherry MX Brown switches'></a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Moving to Octopress]]></title>
    <link href="http://www.spacjer.com//blog/2013/04/17/moving-to-octopress/"/>
    <updated>2013-04-17T19:39:00+02:00</updated>
    <id>http://www.spacjer.com//blog/2013/04/17/moving-to-octopress</id>
    <content type="html"><![CDATA[<p>I&rsquo;ve decided to move, my not very often updated blog, to the new platform. I&rsquo;ve made a choice to use <a href="http://octopress.org/"><strong>Octopress</strong> blog engine</a>. Octopress uses <a href="http://jekyllrb.com/">Jekyll</a>, a static site generator in <code>ruby</code>, that basically takes bunch of templates files and produce a set of html pages. I could use Jekyll, but Octopress extends it with additional files,  themes and plugins, so you can create your blog very fast with small effort.</p>

<p>Till now my blog was powered by <a href="http://wordpress.org/"><strong><em>Wordpress</em></strong> engine</a>, so why I wanted to change it? The main reason is that Wordpress is quite complex engine (with many plugins and settings) that I&rsquo;ve never fully understood. Changing something in it was more like hacking for me than full understandable programming. Additionally Wordpress uses <code>mysql</code> database to store posts which adds complexity to backup strategy &ndash; I needed to store all Wordpress files, plus database with posts. This is why I decided to change it.</p>

<p>What I like in Octopress:</p>

<ul>
<li>simplicity &ndash; the whole blog can be generated by one command (<code>rake generate</code>), which creates a set of <code>HTML</code> and <code>JavaScript</code> files that can be uploaded to the server; by default Octopress supports <em>Github Pages</em> and <em>Heroku</em>, it can also uses <code>rsync</code> to deploy files to custom server.</li>
<li>customization &ndash; there are many plugins you can use (twitter and github integration for example) and the design of the blog can also be easily changed by tweaking <code>Sass</code> (<code>*.scss</code>) files.</li>
<li>beautiful, built-in code highlighter with support for many languages &ndash; it uses <code>Solarized</code> <a href="http://ethanschoonover.com/solarized">color scheme</a> (light or dark) which looks great;</li>
<li>possibility of writing post as a simple text file, using markdown &ndash; you can create/edit them in your favorite text editor (<code>Sublime Text</code> is my choice);</li>
<li>easy backup &ndash; I store all files in a git repository;</li>
<li>a chance to work with <code>ruby</code> language &ndash; currently I&rsquo;m trying to extend my knowledge about it, which gives me a lot of fun.</li>
</ul>


<p>Finally I would like to say few words about my transition from Wordpress to Octopress. The migration process I&rsquo;ve started from exporting all my posts from Wordpress to markdown files &ndash; I did it by using <a href="https://github.com/benbalter/wordpress-to-jekyll-exporter">WordPress to Jekyll Exporter</a> plugin. After this I&rsquo;ve tweaked generated posts, installed default theme (I&rsquo;ve changed it a little bit &ndash; my inspiration was <code>darkstripes</code> <a href="https://github.com/amelandri/darkstripes">theme</a> by Alessandro Melandri) and set base settings in <code>_config.yml</code> file. The final results you can see here, I hope you like it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Live Template for Caliburn.Micro framework]]></title>
    <link href="http://www.spacjer.com//2011/08/19/live-template-for-caliburn-micro-framework/"/>
    <updated>2011-08-19T00:00:00+02:00</updated>
    <id>http://www.spacjer.com//2011/08/19/live-template-for-caliburn-micro-framework</id>
    <content type="html"><![CDATA[<h1></h1>

<p>Recently I’ve been working on the WPF application that extensively uses <a href="http://caliburnmicro.codeplex.com/" title="Caliburn.Micro">Caliburn.Micro</a> framework for <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" title="MVVM">MVVM</a> pattern. <code>MVVM</code> usually force you to write many properties in the <code>ViewModel</code>. Those properties should typically notify <code>View</code> about their values changes – to do that every <code>ViewModel</code> has to implement <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" title="INotifyPropertyChanged">INotifyPropertyChanged</a> interface and in the setters of the properties we need to rise <code>NotifyPropertyChanged</code> event, for example like that:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">SelectTagsViewModel</span> <span class="p">:</span> <span class="n">Screen</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">event</span> <span class="n">PropertyChangedEventHandler</span> <span class="n">PropertyChanged</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">private</span> <span class="kt">string</span> <span class="n">_property</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="kt">string</span> <span class="n">Property</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_property</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">set</span>
</span><span class='line'>      <span class="p">{</span>
</span><span class='line'>          <span class="n">_property</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span>
</span><span class='line'>          <span class="n">NotifyPropertyChanged</span><span class="p">(</span><span class="s">&quot;Property&quot;</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">private</span> <span class="k">void</span> <span class="nf">NotifyPropertyChanged</span><span class="p">(</span><span class="kt">string</span> <span class="n">name</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="n">PropertyChanged</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span>
</span><span class='line'>      <span class="p">{</span>
</span><span class='line'>          <span class="n">PropertyChanged</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="k">new</span> <span class="n">PropertyChangedEventArgs</span><span class="p">(</span><span class="n">name</span><span class="p">));</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In <code>Caliburn.Mirco</code> your <code>ViewModel</code> can inherit from <code>PropertyChangedBase</code> class (or other classes that internally inherit from <code>PropertyChangedBase</code> class – like <code>Screen</code>, <code>Conductor</code> and so on) and then in properties you can use <code>NotifyOfPropertyChange</code> method:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">private</span> <span class="kt">string</span> <span class="n">_property</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">public</span> <span class="kt">string</span> <span class="n">Property</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_property</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">set</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="n">_property</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span>
</span><span class='line'>      <span class="n">NotifyOfPropertyChange</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="n">Property</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Because in my project I have to write many properites with <code>NotifyOfPropertyChange</code> method in their setters, I’ve decided that I write <a href="http://www.jetbrains.com/resharper/features/codeTemplate.html" title="Live Templates">Resharper Live Template</a> for this purpose. To do this you should go to the ‘<strong>Resharper</strong>‘ menu in Visual Studio, then ‘<strong>Live Templates</strong>‘ and there you should select ‘<strong>New Template</strong>‘ button. After this you will get the window like this one:</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2011/08/LiveTemplate_new_template.png" title="[Live Template editor [Live Template editor]]" ></p>

<p>In the left part you can write the code of the snipped. In my case it was:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">private</span> <span class="err">$</span><span class="n">TYPE</span><span class="err">$</span> <span class="n">_</span><span class="err">$</span><span class="n">PRIVATEMEMBER</span><span class="err">$</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">public</span> <span class="err">$</span><span class="n">TYPE</span><span class="err">$</span> <span class="err">$</span><span class="n">PROPERTYNAME</span><span class="err">$</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">get</span>   <span class="p">{</span> <span class="k">return</span> <span class="n">_</span><span class="err">$</span><span class="n">PRIVATEMEMBER</span><span class="err">$</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'>  <span class="k">set</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="n">_</span><span class="err">$</span><span class="n">PRIVATEMEMBER</span><span class="err">$</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>      <span class="n">NotifyOfPropertyChange</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="err">$</span><span class="n">PROPERTYNAME</span><span class="err">$</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>where:</p>

<ul>
<li><code>$TYPE$</code> – the type of the property</li>
<li><code>$PRIVATEMEMBER$</code> – the name of the private field</li>
<li><code>$PROPERTYNAME$</code>– the name of the property</li>
</ul>


<p>The most tricky part of this template is setting properties of the template in the right pane of the window. This should be done in this way:</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2011/08/LiveTemplate_new_template_edited.png" title="[Editing live template [Editing live template]]" ></p>

<ul>
<li>In the <code>Editable Occurrence</code> column we specify the order of defining elements of the template – first the type and the name of the field that will be used for this property.</li>
<li>In the <code>Value</code> column we can set how the value of the template field will be calculated – the value of <code>$PROPERTYNAME$</code> property is set as name of the private field with first character as upper case.</li>
</ul>


<p>The last thing to do is to set a shortcut for this snippet (in this case: <code>propnot</code>) and the description. And this is it, after saving you have a new live template that can be use in code editor by simply writing “<em>propnot</em>“. If you do that you will get something like this in Visual Studio:</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2011/08/LiveTemplate_output.png" title="[Live Template in Visual Studio [Live Template in Visual Studio]]" ></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Case insensitive Contains method for a string class]]></title>
    <link href="http://www.spacjer.com//2011/03/30/case-insensitive-string-contains-method-2/"/>
    <updated>2011-03-30T00:00:00+02:00</updated>
    <id>http://www.spacjer.com//2011/03/30/case-insensitive-string-contains-method-2</id>
    <content type="html"><![CDATA[<h1></h1>

<p>I decided that in the next few weeks I will write series of short blog posts with code snippets that I use during my everyday work.</p>

<p>Very often I have to check if a string contains specific sequence of characters, to do it I write something like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">if</span> <span class="p">(</span><span class="n">text</span><span class="p">.</span><span class="n">Contains</span><span class="p">(</span><span class="s">&quot;some text&quot;</span><span class="p">))</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">DoSomething</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>and after few seconds I realize that it won’t work correctly because <a href="http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx">Contains</a> method is <strong>case-sensitive</strong>. To solve this problem I’ve written an extension method that does case-insensitive checking. Here is my method:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">static</span> <span class="k">class</span> <span class="nc">StringExtensionMethods</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">static</span> <span class="kt">bool</span> <span class="nf">ContainsWithIgnoreCase</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">text</span><span class="p">,</span> <span class="kt">string</span> <span class="k">value</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>     <span class="k">if</span> <span class="p">(</span><span class="n">text</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span>
</span><span class='line'>     <span class="p">{</span>
</span><span class='line'>         <span class="k">return</span> <span class="k">false</span><span class="p">;</span>
</span><span class='line'>     <span class="p">}</span>
</span><span class='line'>     <span class="k">return</span>
</span><span class='line'>      <span class="p">(</span><span class="n">text</span><span class="p">.</span><span class="n">IndexOf</span><span class="p">(</span><span class="k">value</span><span class="p">,</span> <span class="n">StringComparison</span><span class="p">.</span><span class="n">OrdinalIgnoreCase</span><span class="p">)</span> <span class="p">&gt;=</span> <span class="m">0</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>and now I can simply write:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">if</span> <span class="p">(</span><span class="n">text</span><span class="p">.</span><span class="n">ContainsWithIgnoreCase</span><span class="p">(</span><span class="s">&quot;some text&quot;</span><span class="p">))</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">DoSomething</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>At the beginning of the extension method I check if text isn’t null and if so I return false. This causes that I don’t have to check if a variable is null before using this method.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Template method pattern]]></title>
    <link href="http://www.spacjer.com//2010/11/04/template-method-patter/"/>
    <updated>2010-11-04T00:00:00+01:00</updated>
    <id>http://www.spacjer.com//2010/11/04/template-method-patter</id>
    <content type="html"><![CDATA[<h1></h1>

<p>Sometimes I write a class that has many similar methods. All those methods start from initialization block, then there is a processing part and at the end there is finalizing segment. In other words those methods are only different in processing code – rest is the same.</p>

<p>When I see that kind of class I immediately start thinking about using <em>template method patter</em>. You can read about this pattern <a href="http://en.wikipedia.org/wiki/Template_method_pattern">here</a> . The general idea is that you have an abstract class that creates template method for concrete classes that derive from it and add its own functionality to this method.</p>

<p>I will show it by examples.</p>

<p>First let’s assume we have to write a class that will process a text file. The file contains only one line with an integer number. Our task is to write a methods that will increment (n ), decrement (n–) or multiple by two (n=*2) this number.</p>

<p>Let’s start from trivial implementation:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">IntegerFileOperation</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">private</span> <span class="kt">string</span> <span class="n">filePath</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="nf">IntegerFileOperation</span><span class="p">(</span><span class="kt">string</span> <span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="n">filePath</span> <span class="p">=</span> <span class="n">filePath</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">Increment</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="kt">string</span> <span class="n">numerText</span> <span class="p">=</span> <span class="n">File</span><span class="p">.</span><span class="n">ReadAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="kt">int</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="kt">int</span><span class="p">.</span><span class="n">TryParse</span><span class="p">(</span><span class="n">numerText</span><span class="p">,</span> <span class="k">out</span> <span class="n">integer</span><span class="p">))</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">integer</span>  <span class="p">;</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">File</span><span class="p">.</span><span class="n">WriteAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">integer</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">return</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">Decrement</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="kt">string</span> <span class="n">numerText</span> <span class="p">=</span> <span class="n">File</span><span class="p">.</span><span class="n">ReadAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="kt">int</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="kt">int</span><span class="p">.</span><span class="n">TryParse</span><span class="p">(</span><span class="n">numerText</span><span class="p">,</span> <span class="k">out</span> <span class="n">integer</span><span class="p">))</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">integer</span><span class="p">--;</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">File</span><span class="p">.</span><span class="n">WriteAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">integer</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">return</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">MultipleByTwo</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="kt">string</span> <span class="n">numerText</span> <span class="p">=</span> <span class="n">File</span><span class="p">.</span><span class="n">ReadAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="kt">int</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="kt">int</span><span class="p">.</span><span class="n">TryParse</span><span class="p">(</span><span class="n">numerText</span><span class="p">,</span> <span class="k">out</span> <span class="n">integer</span><span class="p">))</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">integer</span> <span class="p">*=</span> <span class="m">2</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">File</span><span class="p">.</span><span class="n">WriteAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">integer</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">return</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As we can see our  class has three methods: <code>Increment()</code>, <code>Decrement()</code> and <code>MultipleByTwo()</code> – the only difference between them is in line where we change integer variable, rest is the same. There is lots of redundant code that should be removed, to do it we can introduce a base, abstract class.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">abstract</span> <span class="k">class</span> <span class="nc">IntegerFileOperationBase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">private</span> <span class="kt">string</span> <span class="n">filePath</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">protected</span> <span class="k">abstract</span> <span class="kt">int</span> <span class="nf">DoOperation</span><span class="p">(</span><span class="kt">int</span> <span class="n">number</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="nf">IntegerFileOperationBase</span><span class="p">(</span><span class="kt">string</span> <span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="n">filePath</span> <span class="p">=</span> <span class="n">filePath</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">ChangeNumber</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="kt">string</span> <span class="n">numerText</span> <span class="p">=</span> <span class="n">File</span><span class="p">.</span><span class="n">ReadAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="kt">int</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="kt">int</span><span class="p">.</span><span class="n">TryParse</span><span class="p">(</span><span class="n">numerText</span><span class="p">,</span> <span class="k">out</span> <span class="n">integer</span><span class="p">))</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">integer</span> <span class="p">=</span> <span class="n">DoOperation</span><span class="p">(</span><span class="n">integer</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">File</span><span class="p">.</span><span class="n">WriteAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">integer</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">return</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In <code>IntegerFileOperationBase</code> class we have a method <code>ChangeNumber()</code> which is a template method. This method opens a text file, reads its content, parses it as an integer and then invokes <code>DoOperation</code> method to change the integer variable. After all the new value is saved to the file. By various implementation of <code>DoOperation</code> method we can change the way that <code>ChangeNumber</code> method works. Here’s how we can do it:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">IncrementIntegerInFile</span> <span class="p">:</span> <span class="n">IntegerFileOperationBase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">public</span> <span class="nf">IncrementIntegerInFile</span><span class="p">(</span><span class="kt">string</span> <span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>        <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">protected</span> <span class="k">override</span> <span class="kt">int</span> <span class="nf">DoOperation</span><span class="p">(</span><span class="kt">int</span> <span class="n">number</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span>   <span class="n">number</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">DecrementIntegerInFile</span> <span class="p">:</span> <span class="n">IntegerFileOperationBase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">public</span> <span class="nf">DecrementIntegerInFile</span><span class="p">(</span><span class="kt">string</span> <span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>        <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">protected</span> <span class="k">override</span> <span class="kt">int</span> <span class="nf">DoOperation</span><span class="p">(</span><span class="kt">int</span> <span class="n">number</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="p">--</span><span class="n">number</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">MultipleByTwoIntegerInFile</span> <span class="p">:</span> <span class="n">IntegerFileOperationBase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">public</span> <span class="nf">MultipleByTwoIntegerInFile</span><span class="p">(</span><span class="kt">string</span> <span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>        <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">protected</span> <span class="k">override</span> <span class="kt">int</span> <span class="nf">DoOperation</span><span class="p">(</span><span class="kt">int</span> <span class="n">number</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">number</span> <span class="p">*</span> <span class="m">2</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The result of our  refactoring is a code without redundancy. I don’t like this solution, because I feel it’s too complicated for such an easy task (we have four classes instead of one). We can solve this problem by reimplementing it with delegate mechanism. Instead of an abstract  <code>DoOperation()</code> method we can use a delegate that will be passed to the <code>ChangeNumber()</code> method as a parameter (I use generic <code>Func&lt;int, int&gt;</code> delegate as a type – <a href="http://msdn.microsoft.com/en-us/library/bb534647.aspx">read more</a>. To implement <code>Increment()</code>, <code>Decrement()</code> and <code>MultipleByTwo()</code> methods we use <code>ChangeNumber</code> method with lambda expression as a parameter:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
</pre></td><td class='code'><pre><code class='csharp'><span class='line'><span class="k">public</span> <span class="k">class</span> <span class="nc">IntegerFileOperationWithDelegate</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">private</span> <span class="kt">string</span> <span class="n">filePath</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="nf">IntegerFileOperationWithDelegate</span><span class="p">(</span><span class="kt">string</span> <span class="n">filePath</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="n">filePath</span> <span class="p">=</span> <span class="n">filePath</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">ChangeNumber</span><span class="p">(</span><span class="n">Func</span> <span class="n">doOperation</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="kt">string</span> <span class="n">numerText</span> <span class="p">=</span> <span class="n">File</span><span class="p">.</span><span class="n">ReadAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="kt">int</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="kt">int</span><span class="p">.</span><span class="n">TryParse</span><span class="p">(</span><span class="n">numerText</span><span class="p">,</span> <span class="k">out</span> <span class="n">integer</span><span class="p">))</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">integer</span> <span class="p">=</span> <span class="n">doOperation</span><span class="p">(</span><span class="n">integer</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">File</span><span class="p">.</span><span class="n">WriteAllText</span><span class="p">(</span><span class="n">filePath</span><span class="p">,</span> <span class="n">integer</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'>            <span class="k">return</span> <span class="n">integer</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">Increment</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nf">ChangeNumber</span><span class="p">(</span><span class="n">n</span> <span class="p">=&gt;</span>   <span class="n">n</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">Decrement</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nf">ChangeNumber</span><span class="p">(</span><span class="n">n</span> <span class="p">=&gt;</span> <span class="p">--</span><span class="n">n</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="kt">int</span> <span class="nf">MultipleByTwo</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nf">ChangeNumber</span><span class="p">(</span><span class="n">n</span> <span class="p">=&gt;</span> <span class="n">n</span> <span class="p">*</span> <span class="m">2</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This makes that we have only one, clean class without redundant code. If I need to write a class with lots of repeated code I try to use this pattern to solve it.</p>

<p>Do you like that kind of solution? Or maybe it’s completely wrong? Please tell me in the comments.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My top 10 features of JetBrains ReSharper]]></title>
    <link href="http://www.spacjer.com//2010/06/20/my-top-10-features-of-jetbrains-resharper/"/>
    <updated>2010-06-20T00:00:00+02:00</updated>
    <id>http://www.spacjer.com//2010/06/20/my-top-10-features-of-jetbrains-resharper</id>
    <content type="html"><![CDATA[<h1></h1>

<p>I have to say that I’m a big fan of <a href="http://www.jetbrains.com/resharper/"><strong>ReSharper</strong></a> plugin (or maybe a better word: tool) for Visual Studio. I’ve been using it for almost 3 years now.  After this time I can only say that I’m really angry if I have to code in VS without ReSharper. Working with it is a pleasure and every .Net developer should at least try it, you don’t regret it!</p>

<p>To give only a small overview of its potential I will describe here 10 functions that I use the most:</p>

<p><em>(ReSharper allows us to use one of two keymaps: Visual Studio scheme or IDEA scheme. In all my description I will use VS scheme)</em></p>

<ol>
<li><p><strong>Go to Type</strong> <em>(Ctrl + T)</em></p>

<p>In my work I usually know a name of a class I’m looking for, but I don’t know where it is (in which project, folder etc.). The solution is very simple: I hit <code>Ctrl + T</code> and I start writing class name – ReSharper shows me all matching types, I only have to select the one I need.
In the editor we can use special characters that can substitute zero or more characters (*), zero or one character (?) and one or more characters ( )</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image.png" title="[Go to Type [Go to Type]]" ></p></li>
<li><p><strong>Go to Inheritor </strong> <em>(Alt + End)</em></p>

<p>As the name says, it allows us to go to the inheritor of selected class. After hitting the shortcut you get the dialog with all inheritors. Especially I like it when I see an interface in code and I want to go to its implementation. Really cool!</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image1.png" title="[Go to Inheritor [Go to Inheritor]]" ></p></li>
<li><p><strong>Find Usages</strong> <em>(Shift + F12)</em></p>

<p>It helps find all usages of a class, method, property and any other object in code. With this functionality we can easily understand where and why any object is used.</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image2.png" title="[Find Usages [Find Usages]]" ></p></li>
<li><p><strong>Locate in Solution Explorer</strong> <em>(Shift + Alt + L)</em></p>

<p>As I wrote before I use “<em>Go to type</em>” all the time, but sometimes when I work with a file in Visual Studio I need to change one of its properties or simply see in which folder in solution it is. Everyone who works with VS knows that finding a file in big solution is frustrating, with this option it won’t be anymore. Simple use this method for opened file and it will be highlighted in solution explore.</p></li>
<li><p><strong>Create from Usage</strong> <em>(Alt + Enter)</em></p>

<p>Another great feature of ReSharper.  Let’s assume you’re writing a code in VS that needs to parse some string variable. So you can write something like this:</p>

<p>string number = ParseMyVariable(someText);</p>

<p>The name of the method will be marked with red, so you can simply hit <code>Alt + Enter</code> and select: <code>Create method ParseMyVariable</code>, RS will create method with good signature for you.</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image3.png" title="[Create from Usage [Create from Usage]]" ></p></li>
<li><p><strong>Symbol Code Completion</strong> <em>(Ctrl + Space)</em></p>

<p>This function is great for lazy people (yes, I’m and I use it all the time).  Again I will show it by example: I need to create instance of <code>SqlCommand</code>, so I write <code>SqlCommand</code> and hit <code>Ctrl + Space</code>, after that ReSharper will suggest me possible names for a variable. I only need to select the one I like. The names are usually very smart created, so for <code>SqlCommand</code> proposition will be <code>command</code> or <code>sqlCommand</code></p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image4.png" title="[Symbol Code Completion [Symbol Code Completion]]" ></p></li>
<li><p><strong>Rename</strong> <em>(Ctrl + R, R)</em></p>

<p>I can’t say much about it, because the name tells everything.  It changes the name of a symbol (method, class, property and so on) in every file it is.</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image5.png" title="[Rename [Rename]]" ></p></li>
<li><p><strong>Introduce Parameter</strong> <em>(Ctrl + R, P)</em></p>

<p>Sometime, during refactoring we need to change a local variable to the method parameter and this function is great for this. The best is that ReSharper will try to change every invocation of the method by adding appropriate parameter – it takes it value from the context.</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image6.png" title="[Introduce Parameter [Introduce Parameter]]" ></p></li>
<li><p><strong>‘Surround With’ Templates</strong> <em>(Ctrl + E, U)</em></p>

<p>I use it usually when I need to add <em>try…catch </em>block or a <em>region</em> in code (there is more build in statements). To do it I select a part of a code, hit the shortcut and select the statement I want – rest is done automatically (ex. code is nice formatted)</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image7.png" title="[Surround With Templates [Surround With Templates]]" ></p></li>
<li><p> <strong>Go to File Member</strong> <em>(Alt + \)</em></p></li>
</ol>


<p>It allows us quickly go to any file member:<br/>
&ndash; method, property, field for a class<br/>
&ndash; node element for xml file</p>

<p><img class="center" src="http://www.spacjer.com//images/posts/2010/06/image8.png" title="[Go to File Member [Go to File Member]]" ></p>

<p>So this is my top 10 of ReSharper features and what is your? Or maybe you’re big fan of <strong><a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a></strong>  if so please write me why it’s better then ReSharper.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Useful applications for Android phones]]></title>
    <link href="http://www.spacjer.com//2010/06/06/useful-applications-for-android-phones/"/>
    <updated>2010-06-06T00:00:00+02:00</updated>
    <id>http://www.spacjer.com//2010/06/06/useful-applications-for-android-phones</id>
    <content type="html"><![CDATA[<h1></h1>

<p>I’ve been using <a href="http://www.htc.com/europe/product/hero/overview.html">Htc Hero</a> for almost half a year. After I bought the phone I’ve started exploring the Android Market in search of free, useful applications. There is so many of them that it’s really hard to find any good among them.  I’m still searching but for now I’m using those (this is my list of best tools for Android):</p>

<ul>
<li><a href="http://www.androidtapp.com/advanced-task-killer/"><strong>Advanced Task Killer</strong></a> – simple task manager that allows us to kill not used apps (it saves battery lifetime and releases memory).</li>
<li><a href="http://www.metago.net/astro/fm/index.php"><strong>ASTRO File Manager</strong></a> – advance file manager (copy, cut, past files; working with zip files; editing text files etc.).</li>
<li><a href="http://www.evernote.com/about/download/android.php"><strong>Evernote</strong></a> – mobile version of great PC/Mac notes taking tool. I use the PC version to manage all my notes and this small app allows me to view them on my phone. We can also create notes and send them to the Evernote account.</li>
<li><a href="https://www.dropbox.com/android"><strong>Dropbox</strong></a> – another mobile version of PC/Mac tool. Dropbox lets us store, sync and share files using Internet. It’s especially helpful if we have many computers and we need to have access to the same files on them.</li>
<li><a href="http://weloveastrid.com/"><strong>Astrid</strong></a> – a simply, but powerful TODO list. It has tagging, partial progress, remainders, syncing with Remember The Milk.</li>
<li><a href="http://twidroid.com/"><strong>Twidroid</strong></a> – twitter client with very clear GUI.</li>
<li><a href="http://newsrob.blogspot.com/"><strong>NewsRob</strong></a> – RSS/Atom reader that syncs with Google Reader. I like the interface – it’s simple but powerful</li>
<li><a href="http://www.jaqpot.net/netcounter/"><strong>NetCounter</strong></a> – a simple network traffic counter for EDGE/3G and WiFi.</li>
<li><a href="http://www.tunewiki.com/device/tunewiki-android.aspx"><strong>TuneWiki</strong></a> – music player with many options (it can even show lyrics of currently listened song).  It can also stream music from <a href="http://www.last.fm">last.fm</a> which is great.</li>
<li><a href="http://www.aldiko.com/"><strong>Aldiko</strong></a> – an eBook (ePub file format) reader for Android with option to download books from internet.</li>
<li><a href="http://android.wordpress.org/"><strong>WordPress</strong></a> – it allows us to manage (write posts, create pages, moderate comments) our WordPress blog.</li>
</ul>

]]></content>
  </entry>
  
</feed>
