<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Notes with race conditions</title>
    <link>/post.html</link>
    <description>Recent content in Notes with race conditions</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 22 Mar 2019 22:21:14 +0000</lastBuildDate>

        <atom:link href="/post/index.xml" rel="self" type="application/rss+xml" />


    <item>
      <title>Last year books and harmfull perfectionism</title>
      <link>/last-year-books.html</link>
      <pubDate>Fri, 22 Mar 2019 22:21:14 +0000</pubDate>

      <guid>/last-year-books.html</guid>
      <description>Good friends, good books, and a sleepy conscience: this is the ideal life. ― Mark Twain  The power of the book Quite a while ago, when I was starting my professional career I decided to dig deeper into the LabVIEW programming language to make the project I was working on back then better. As a part of that process, I read 4 books. After that I became a go-to person on the subject inside the company, I answered many questions on programming forums and gain some reputation there, participated in a couple of online competitions and won one of them.</description>

      <content:encoded><![CDATA[<div align="right"><i>
Good friends, good books, and a sleepy conscience: this is the ideal life.
― Mark Twain
</i></div>
<h3 id="the-power-of-the-book">The power of the book</h3>
<p>Quite a while ago, when I was starting my professional career I decided to dig deeper into the <a href="https://en.wikipedia.org/wiki/LabVIEW"target="_blank">LabVIEW</a> programming language to make the project I was working on back then better. As a part of that process, I read 4 books. After that I became a go-to person on the subject inside the company, I answered many questions on programming forums and gain some reputation there, participated in a couple of online competitions and won one of them. That was the first time I was really hit by the power of a good book.</p>
<p>A couple of years ago I adopted a more consistent and systematic approach to my reading and I&rsquo;m still happy about it:</p>
<ol>
<li>Be consistent and read/listen 22-25 books a year</li>
<li>Make notes (3-10 sentences) on every book during the reading process and revise notes once in a while</li>
<li><strong>Drop the book if I don&rsquo;t like it.</strong></li>
</ol>
<p>Number 3 is not easy to do. If you like a lot to finish everything you start, then this may often apply to books as well. However, there are so many good books. Does it really worth to push till the end the book we don&rsquo;t like at all and don&rsquo;t find useful? Just to say: &ldquo;I did it! I read it!&rdquo; Definitely not. Being able to finish what we start is an amazing skill, but in this case, it&rsquo;s an example of harmful perfectionism. So, I&rsquo;m quite happy about the following rule #3.</p>
<h3 id="the-best-from-the-last-year">The best from the last year</h3>
<p>Back to the last year books&hellip; I won&rsquo;t bother you with the long review of all 25 books, dear reader, but I want to say a couple of words about books from different categories, which I liked the most:</p>
<p><code>Authenticity (by David Posen)</code>
I would say this was the best book in 2018 for me. It is fun to read it, but at the same time it opens up the nature of the stress from many different angles.</p>
<p><code>How to Talk So Kids Will Listen &amp; Listen So Kids Will Talk</code>
A big part of parenting is working on yourself. This book has many great advises and real-world examples. I have listened this book and it&rsquo;s so good, that I plan to read it this year. And I can&rsquo;t say I often go through the same book more than once.</p>
<p><code>The War of Art</code>
When I have downtime or when I feel <em>lazy</em>, I go and re-read the first chapter. It&rsquo;s inspirational and simply amazing. I also liked a lot this quote: <em>Someone once asked Somerset Maugham if he wrote on a schedule or only when struck by inspiration. &ldquo;I write only when inspiration strikes,&rdquo; he replied. &ldquo;Fortunately it strikes every morning at nine o&rsquo;clock sharp.&quot;</em></p>
<p><code>Born to Run</code>
Some history, some running theory, facts about the human body, biography of very respectable people unified around one common topic: running.
I&rsquo;m not even close to &ldquo;serious runners&rdquo;, but I do like to run 10-12km one-two times a week. This book is fun to read, contains a lot of useful information and it, definitely, improved the quality of my runs. Also, this book was recommended by a friend of mine, who was a running coach and who participated in 100km races, which speaks for itself.</p>
<p><code>The Go Programming Language</code>
The best book about the programming language for experienced programmers. Very short and detailed (yes, both, at the same time) explanation of important <a href="https://golang.org/"target="_blank">Golang</a> idioms. It was my second Golang book, after working with Golang for a bit and reading multiple tutorials, but I would recommend this as a first Golang book for experienced software developers.</p>
<p><code>Hans-On Machine Learning with Scikit-Learn and TensorFlow</code>
There is a hype around machine learning for quite a while already. If you <em>google</em> something like &ldquo;top N in-demand tech skills 2019&rdquo;, machine learning will be in each of the lists. Very likely at the top. I see that some machine learning knowledge can be a great complementary skill to software engineering skills, especially if you work with large distributed systems and big data (these days we see ML everywhere we see big data, isn&rsquo;t it?). This book is &ldquo;developer friendly&rdquo;, it&rsquo;s very pragmatic, it has a good mix of theory and code (although it has quite a bit of math as well) and covers a lot of ML topics.</p>
<h3 id="fiction-category-and-movies">Fiction category and movies</h3>
<p>I found that I read more professional, business and self-improvement books lately and watch a movie for a &ldquo;fiction&rdquo; content. I did enjoy fiction books last year as well, I just didn&rsquo;t include them in my short list.
I also noticed that when that rare &ldquo;movie evening&rdquo; comes (you know, two little kids&hellip;), I often have a hard time selecting a good movie to watch. I have never had such issues with books. My &ldquo;want to read&rdquo; list grows way faster when I &ldquo;clean&rdquo; it.</p>
]]></content:encoded>

    </item>

    <item>
      <title>Redirects with AWS Route53, S3 and CloudFront</title>
      <link>/redirects-with-aws-route53-s3-and-cloudfront.html</link>
      <pubDate>Thu, 17 Jan 2019 21:28:17 +0000</pubDate>

      <guid>/redirects-with-aws-route53-s3-and-cloudfront.html</guid>
      <description>Recently I had to change domains and subdomains for a project I have worked on a couple of years ago. Usually redirects are simple, but, sometimes, not that simple many of us would like them to be. Changing DNS service records may not be enough. We may need to create an S3 bucket and a Cloud Front distribution.
So this will be a short blog post, which describes a couple of scenarios with domain redirects on AWS.</description>

      <content:encoded><![CDATA[<p>Recently I had to change domains and subdomains for a project I have worked on a couple of years ago. Usually redirects are simple, but, sometimes, not that simple many of us would like them to be. Changing DNS service records may not be enough. We may need to create an S3 bucket and a Cloud Front distribution.</p>
<p>So this will be a short blog post, which describes a couple of scenarios with domain redirects on AWS.</p>
<h3 id="what-do-we-have">What do we have</h3>
<p>Imagine, we have a project hosted on AWS, we have a static content which goes through S3 + CloudFront, we have some API implemented with AWS API Gateway + Lambda, maybe some microservices running on top of ELB/ALB plus EC2 or ECS. Basically, something similar to what I have described <a href="https://victormarinsky.com/serverless-on-aws-not-a-beginner-tutorial/"target="_blank">here</a>.
In addition to that, we have a relatively complex Route 53 hosted zone with 20+ records (A records, subdomains, email records, aliases, etc.).</p>
<p>Now, for whatever reason, we want to host <strong>part</strong> of the platform outside of AWS.</p>
<h3 id="redirect-subdomain-to-an-external-hostname">Redirect subdomain to an external hostname</h3>
<p>Let&rsquo;s start with something very simple first. Our domain is <code>example.com</code> and we want to have <code>blog.example.com</code> let&rsquo;s say on <a href="https://ghost.org/"target="_blank">Ghost</a>. For this, we just need to create a CNAME <code>blog.example.com</code> with Alias &lsquo;No&rsquo;, which points to something like <code>example.ghost.org</code>.</p>
<h3 id="redirect-root-to-www">Redirect root to www</h3>
<p>If <code>www.example.com</code> is an alias to an internal AWS resource (not a CNAME like in the example above), then it&rsquo;s quite easy. We can just create an alias for root A record which points to <code>www.example.com</code>. If <code>www.*</code> is a CNAME to an external resource, then it&rsquo;s a bit more complicated.</p>
<h3 id="redirect-root-to-an-external-hostname">Redirect root to an external hostname</h3>
<p>What if we want our main domain <code>example.com</code> to be outside of AWS and keep AWS Route 53 as our main DNS service? Also, we want customers to always see our main domain in their browser.</p>
<p>Our <code>www.example.com</code> is a CNAME which points to a resource, which is outside of your AWS environment. Here is what we need to do to redirect root (as of today).</p>
<p>We need to create an S3 bucket with the name which matches our main domain name (<code>example.com</code>), then go to bucket properties and enable <strong>Static Website Hosting</strong> with a redirect option. We will be redirecting to <code>www.example.com</code>. After that, we can create <strong>A Record Set</strong> in Route53 for our root domain with an alias to our <code>example.com</code> S3 bucket. Still simple enough.</p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/staticwbst.png"
        alt="staticwbst"/></span></span>
<br></p>
<p>At this point, everything should be working except HTTP<strong>S</strong> <code>https://example.com</code>. To make <code>https</code> redirect work for the root domain we need to create a CloudFront distribution for our <code>example.com</code> bucket. After that, point our <strong>A Record</strong> on Route 53 to CloudFront distribution instead of the bucket and <code>https</code> will work. A couple of possible pitfalls in this step:</p>
<ul>
<li>when we set up your CloudFront distribution, in the Origin Domain Name field we need to specify the Static Website Hosting endpoint (see the screenshot above) and not the bucket name: <code>example.com.s3-website-us-east-1.amazonaws.com</code>, not <code>example.com.s3.amazonaws.com</code>; <strong>this is very important</strong></li>
<li>if you see an error like <em>&ldquo;This XML file does not appear to have any&hellip;&quot;</em> when you&rsquo;re trying to access Cloud Front distribution link, you probably set an Origin Domain Name incorrectly; you have to fix that as described above and <strong>create an invalidation to update CloudFront cache</strong></li>
<li>we don&rsquo;t need to make our bucket public or create any bucket policies, because our distribution origin is not the bucket itself</li>
</ul>
<p>So what we ended up with: root domain will redirect to <code>www.example.com</code> using CloudFront + S3. <code>www.example.com</code> is a CNAME record in Route 53, which points to whatever we need.</p>
<p>This is the simplest way I have found today to redirect your root domain if you don&rsquo;t want to move all your DNS records from Route53 to another DNS.</p>
<h3 id="dealing-with-taken-bucket-names">Dealing with taken bucket names</h3>
<p>If you can&rsquo;t create a bucket with a name of your root domain, which shouldn&rsquo;t happen often, you can create a bucket with a name like <code>redirect.example.com</code>. Do the steps from above for that bucket and then create an alias in Route53, which points your root domain to <code>redirect.example.com</code> record. We can create an alias from root to <code>redirect.example.com</code> record because the latter is an alias to an AWS resource (CloudFront distribution).</p>
<h3 id="conclusion">Conclusion</h3>
<p>To paraphrase great Warren Buffett: &ldquo;<em>Redirects are</em> simple, but not easy&rdquo;. Hopefully, eventually AWS team will give us a way to achieve the above with one little Route 53 record and without S3 bucket and CloudFront distribution.</p>
]]></content:encoded>

    </item>

    <item>
      <title>Five undervalued git commands</title>
      <link>/five-undervalued-git-commands.html</link>
      <pubDate>Tue, 19 Jun 2018 00:43:39 +0000</pubDate>

      <guid>/five-undervalued-git-commands.html</guid>
      <description>Today, git is the standard when it comes to version control system in software development and for many other uses.
 Commands like git commit, checkout, pull, push, status are executed multiple times a day. However, git has a lot of more advanced features, which are not frequently used. Today I want to talk about five commands, which usually are not needed that often, but are quite useful at the right moment.</description>

      <content:encoded><![CDATA[<p>Today, <a href="https://git-scm.com/"target="_blank">git</a> is the standard when it comes to version control system in software development and for many other uses.</p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/code-4.jpg"
        alt="code"/></span></span>
<br></p>
<p>Commands like <code>git commit</code>, <code>checkout</code>, <code>pull</code>, <code>push</code>, <code>status</code> are executed multiple times a day. However, git has a lot of more advanced features, which are not frequently used. Today I want to talk about five commands, which usually are not needed that often, but are quite useful at the right moment.
<br></p>
<h3 id="1-git-bisect">1. git bisect</h3>
<p>Imagine a situation where something was recently working, but now it does not. Or, perhaps, all the tests were passing, but now one little test fails. It is not trivial to understand what happened. To simplify the investigation process we may want to find out the exact commit, which introduced the bug. <a href="https://git-scm.com/docs/git-bisect"target="_blank">git bisect</a> is our friend.</p>
<p>It uses binary search between <em>bad</em> (current commit which is not working) and <em>good</em> (a commit from the past which was working) commits to find which commit introduced the bug.</p>
<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash">1. git bisect start <span class="c1"># let&#39;s start our workflow</span>
2. git bisect bad   <span class="c1"># current commit is broken, mark it a bad</span>
3. git bisect good &lt;commit_id&gt;  <span class="c1"># let bisect know which commit was working</span>
<span class="c1"># After step #3 bisect checks out a commit between good and bad</span>
4. <span class="c1"># we check if current commit has the issue or not</span>
5a. git bisect good <span class="c1"># if test at step #4 succeeded, we mark current commit a good</span>
5b. git bisect bad  <span class="c1"># if test at step #4 failed, we mark current commit as bad</span>
<span class="c1"># repeat #4 and #5 until we find the commit which introduced the bug</span>
</code></pre></div><p>Instead of <code>bad</code> and <code>good</code> we can also use <code>new</code> and <code>old</code>.
<br></p>
<h3 id="2-git-worktree">2. git worktree</h3>
<p>Let&rsquo;s talk about different scenario. We have a large repository, which many people are working on simultaneously. It takes quite a bit of time to clone the repo and also some time to fetch all the changes regularly. We use <a href="https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow"target="_blank">git-flow</a> and have a branch per feature.</p>
<p>Now imagine that, for some reason, we need to work on a different branch and keep our current one open at the same time. There are a couple of ways to achieve this. The most obvious one is to clone the same repo once again and <em>checkout</em> a different branch. However cloning takes time as our repo is quite big plus it creates a bit of mess/confusion as now we should have something like my-project and my-project2 in our projects directory. Also we will have to keep both folders in sync with the origin which means extra <code>pull</code> operations. This is where <a href="https://git-scm.com/docs/git-worktree"target="_blank">git worktree</a> helps. Instead of cloning the same repo one more time we can do this:</p>
<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash">1. git worktree add ../worktrees/myproject mybranch <span class="c1"># create new working tree</span>
2. git worktree list <span class="c1"># list all working trees connected to our repo</span>
... <span class="c1"># do some work, then clean up</span>
3. rm -r ../worktrees/myproject
4. git worktree prune
</code></pre></div><p>After step #1, we have two folders in our projects directory: <code>myproject</code> and <code>worktrees/myproject</code>. Both may point to the same branch or to two different branches, but they are connected to the same repository and we didn&rsquo;t have run <code>git clone</code> second time.
<br></p>
<h3 id="3-git-stash-push--m-message">3. git stash push -m &ldquo;message&rdquo;</h3>
<p><a href="https://git-scm.com/docs/git-stash"target="_blank">stash</a> command is much more popular than the two mentioned above. It is handy when there is a need to save unfinished changes somewhere without committing them. So we can do:</p>
<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash">git stash <span class="c1"># save local changes to stash entry</span>
... <span class="c1"># do something here, e.g. jump to a different branch</span>
git stash list <span class="c1"># print all available stash entries</span>
git stash apply stash@<span class="o">{</span>0<span class="o">}</span> <span class="c1"># apply changes from entry &#39;0&#39; without removing it from the list</span>
git stash pop <span class="c1"># apply changes from the last added entry and remove it from the list</span>
</code></pre></div><p>What is very important when creating a stash entry in my opinion is command options. By default <code>git stash</code> associates a message from the latest commit with the newly stash entry. This may be good enough if we need our stash entry for a couple of minutes, but what if we need to create two stash entries one after another and keep them for a couple of days or a week? The output of <code>git stash list</code> will be very confusing.</p>
<p>So, it&rsquo;s always better to use:</p>
<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash">git stash push -m <span class="s2">&#34;changes description&#34;</span>
git stash save <span class="s2">&#34;changes description&#34;</span> <span class="c1"># older deprecated alternative</span>
</code></pre></div><p><code>stash push</code> has a couple of other useful options:</p>
<ul>
<li><code>-k</code>, <code>--keep-index</code> will not stash changes which have been already added to the index</li>
<li><code>-u</code>, <code>--include-untracked</code> will add untracked files to the stash</li>
</ul>
<p>It&rsquo;s a good idea to create two-three <a href="https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases"target="_blank">git aliases</a> for different variations of <code>git stash push</code>.
<br></p>
<h3 id="4-git-var--l">4. git var -l</h3>
<p>This command is different from the others, because it is educational. It prints out git logical and configuration variables. From its output we can find out:</p>
<ul>
<li>preconfigured aliases and aliases configured by us</li>
<li>that our default editor is <code>vim</code></li>
<li>what&rsquo;s the current default push strategy</li>
<li>and much much more&hellip;</li>
</ul>
<p>It&rsquo;s a good educational tool. If needed a setting from <code>git var -l</code> output can be researched deeper and changed.</p>
<p>As per <a href="https://git-scm.com/docs/git-var"target="_blank">git var docs</a> configuration variables listing functionality is deprecated in favor of <code>git config -l</code>.
Also, in case you have noticed an alias, which you have never configured, in <code>git var/config -l</code> output. E.g.:</p>
<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash">alias.ac<span class="o">=</span>!git add . <span class="o">&amp;&amp;</span> git commit
</code></pre></div><p>Check out <code>git config -l --show-origin</code> output to find where does it come from.
<br></p>
<h3 id="5-git-commit--v---amend---no-edit">5. git commit [-v, &ndash;amend &ndash;no-edit]</h3>
<p>This command could be an exception from the list, as it is needed quite often and it is more widely known and used. But it&rsquo;s useful enough that I want to include it in my list.</p>
<p>Sometimes, we need to review our changes before committing to come up with a better commit description. <code>git commit -v</code> will open a text editor and will display all the changes in addition to filenames to be committed. I have an alias <code>git ci</code> for this command.</p>
<p><code>git commit --amend</code> is something I was really missing back in <a href="https://subversion.apache.org/"target="_blank">svn</a> times. It is useful when we make a commit and realize that we forgot to mention something in the commit message or that it has a typo. <code>--no-edit</code> option can be added when we are happy with our commit message, but want to include a forgotten file into the last commit. I have aliases <code>git ca</code> and <code>git can</code> for these two commands.
<br></p>
<h2 id="afterword">Afterword</h2>
<p>git is really powerful. It is worthwhile to take a quick look at the output of <code>git help -a</code> to get a very general idea of what it can do in addition to commands we all use every day and to check out periodically other people <code>.gitconfig</code> files on Github for new ideas on what else in our git workflow can be optimized and improved.</p>
]]></content:encoded>

    </item>

    <item>
      <title>An ode to password managers</title>
      <link>/how-do-you-store-all-your-passwords.html</link>
      <pubDate>Thu, 31 May 2018 08:34:01 +0000</pubDate>

      <guid>/how-do-you-store-all-your-passwords.html</guid>
      <description>Do you remember all your passwords? If so, that&#39;s not good!  This post will not be as technical as my usual posts are and so it&amp;rsquo;s for everyone, as everyone uses a computer, phone or tablet and sooner or later needs to deal with passwords.
This post is about the importance of password managers. There are so many of them these days, both free and paid. They are easy to install and easy to use, but many people still either don&amp;rsquo;t know about this approach or don&amp;rsquo;t bother trying it.</description>

      <content:encoded><![CDATA[<div align="right"><i>
Do you remember all your passwords?
If so, that's not good!
</i></div>
<br>
<p>This post will not be as technical as my usual posts are and so it&rsquo;s for everyone, as everyone uses a computer, phone or tablet and sooner or later needs to deal with passwords.</p>
<p>This post is about the importance of <a href="https://en.wikipedia.org/wiki/Password_manager"target="_blank">password managers</a>. There are so many of them these days, both free and paid. They are easy to install and easy to use, but many people still either don&rsquo;t know about this approach or don&rsquo;t bother trying it. If this post will convince at least one person to move from memorizing 1-5 passwords to using a password manager, I will be more than happy.</p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/pass_lock-1.jpg"
        alt/></span></span>
<br></p>
<h3 id="common-approaches">Common approaches</h3>
<p>Let&rsquo;s list common approaches to password management:</p>
<ol>
<li>One memorized password for everything.</li>
<li>Two passwords: first for important stuff, second for everything else.</li>
<li>Multiple (probably five-ten) passwords - all memorized. This approach makes it easy to forget which password is for which website.</li>
<li>Password system: for example for gmail.com I may have password <code>liamgrotciv</code> (reversed &lsquo;gmail&rsquo; plus reversed first name).</li>
<li>A paper or electronic list of all passwords.</li>
<li>Saving all passwords in the browser, e.g. <a href="passwords.google.com">google passwords</a></li>
<li>Online cloud based password managers, which securely generate and save passwords somewhere in the cloud and allows access to them from all devices.</li>
<li>Offline password managers that securely save passwords somewhere on a certain device. It is the user&rsquo;s job to synchronize passwords between all devices.</li>
<li>Hardware password managers.</li>
</ol>
<p>If you use methods 7-9, then you probably have already thought about the importance of this topic. If not, please bare with me for three more minutes.</p>
<h3 id="reasons">Reasons</h3>
<p>How we manage our passwords becomes more and more important, because we all use tons of services (which almost always require you to have login/password) and their count only grows every day.</p>
<p>Let&rsquo;s be honest, no matter how good the memory is, sometimes we may forget a password for a particular website or even a home WiFi router.</p>
<p>Also, if you use the same password for multiple services (or even more than one), it is extremely risky. If one little service you signed up long ago, used it once and forgot about it will leak your password, then all your other accounts can be compromised.</p>
<p>Many services allow us to connect using Facebook or Gmail account, but not all. Plus, those services may ask us to allow them to access our Gmail/Facebook contact list and we don&rsquo;t want that.</p>
<p>Some people may ignore the importance of the problem and think that there is no need to hack them, or they have nothing to hide, or who needs to read their emails. If this is your approach, please, reconsider it. There are so many ways to hurt you by knowing your email password: starting from erasing your cloud drive to getting access to your profile on different social networks.</p>
<h3 id="what-a-password-manager-can-do-for-you">What a password manager can do for you</h3>
<ul>
<li>generate strong, complex and unique passwords</li>
<li>store passwords, logins and other data (e.g. credit card numbers) securely</li>
<li>synchronize passwords between multiple platforms and devices</li>
<li>backup and restore data</li>
<li>remind to change passwords once in a while (which is a very good practice) and simplify the changing process
<br></li>
</ul>
<h3 id="managed-cloud-based-password-managers-vs-local-password-managers">Managed cloud based password managers vs local password managers</h3>
<p>Local password managers are usually free to use. They represent an app, which you can run on your device (often many platforms are supported). After you provide your master password, the app opens a file, which is stored locally and contains all your password in an encrypted form. Then you can search your username/password for a particular website or create a new one.
<br></p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/keepass-2.png"
        alt="keepass"/></span></span>
<em><p align="center" font-style="italic"><a href="">KeePassX</a> password manager</p></em>
<br></p>
<p>You have to carry yourself about synchronization between multiple platforms and devices, doing backups and updating your client. For synchronization, you can use something like [Dropbox]. The approach with your own synchronization allows you to add another layer of encryption. You can use a tool like <a href="https://cryptomator.org/"target="_blank">Cryptomator</a> to encrypt your local files before uploading them to a cloud drive. Encrypting some files in your cloud drive may be a good thing to do even when they have nothing to do with passwords.</p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/crypto.png"
        alt/></span></span>
<em><p align="center" font-style="italic">The image is from <a href="https://tools.stefankueng.com/CryptSync.html"target="_blank">Cryptsync</a> website</p></em>
<br></p>
<p>Cloud password managers are not free, but they solve all the problems mentioned above (synchronization and backups) and add an extra convenience, as you can integrate them with your browser and won&rsquo;t need to jump to a different application to copy your username/password.</p>
<p>1Password on the picture below displays a little icon in your login form and can either generate a new password for you (when you are creating a new account) or insert the proper password (when you want to login using an existing account). It makes life incredibly simpler.
<br></p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/1pass-3.png"
        alt="1password"/></span></span>
<br></p>
<p>Unfortunately the browser integration adds another layer of software, which an attacker can use. E.g.: <a href="https://www.darknet.org.uk/2017/03/lastpass-chrome-extension-leaking-passwords/"target="_blank">read more</a> details on how LastPass (probably, the most popular password manager) leaked passwords through chrome extension. It is still not a reason for not using browser integration. Such issues are fixed very quickly (often before they become known publicly) and the good thing is that browser updates your extensions automatically and you won&rsquo;t have to worry about this.</p>
<h3 id="which-password-manager-is-better">Which password manager is better</h3>
<p>There is no right answer. I prefer <a href="https://1password.com/"target="_blank">1Password</a> or <a href="https://www.keepassx.org/"target="_blank">KeyPassX</a> + <a href="https://cryptomator.org/"target="_blank">Cryptomator</a> + CloudDrive. But be aware, that most password managers were found to have at least minor security exploits one or multiple times over the years on at least one platform they support and there will be more in the future. So it&rsquo;s important to always keep your client up to date.</p>
<h3 id="how-long-my-passwords-should-be">How long my passwords should be?</h3>
<p>There is no need to argue on this topic. Longer - better, with as many different symbols as possible. You won&rsquo;t have to memorize them anyway. In my opinion 16-32 symbols is a good interval.</p>
<h3 id="what-about-my-master-password">What about my master password?</h3>
<p>Take this very seriously. It should be long (a sentence with at least six words and numbers) and hard to guess. The most important thing to keep in mind is that you should never forget your master password.</p>
<p>Good password managers should not allow you to reset your master password. If they do, then this is a potential hole in security system. Simply put it: if you have an option to reset your master password, then your service provider has this option as well.</p>
<h3 id="conclusion">Conclusion</h3>
<p>Any password manager is better and much more secure than none. Download a password manager and start using it. It will only take fifteen minutes to set up. Then you will love it.</p>
]]></content:encoded>

    </item>

    <item>
      <title>Serverless with AWS Lambda and API Gateway: not a beginner tutorial</title>
      <link>/serverless-on-aws-not-a-beginner-tutorial.html</link>
      <pubDate>Wed, 16 May 2018 17:38:36 +0000</pubDate>

      <guid>/serverless-on-aws-not-a-beginner-tutorial.html</guid>
      <description>I have been working intensively on building serverless applications with AWS over the past few years. Some projects used serverless architecture with lambda in its core, some used lambda functions only for small parts of the system.
Below I want to share a couple of lessons learned and describe bottlenecks, which you may face while developing even a simple system. This is not a tutorial for absolute beginners. I will not be talking about why serverless approach is good or bad and I expect that you are familiar with AWS, learned about lambda and have played a bit with it.</description>

      <content:encoded><![CDATA[<p>I have been working intensively on building serverless applications with AWS over the past few years. Some projects used serverless architecture with lambda in its core, some used lambda functions only for small parts of the system.</p>
<p>Below I want to share a couple of <em>lessons learned</em> and describe bottlenecks, which you may face while developing even a simple system. This is not a tutorial for absolute beginners. I will not be talking about why serverless approach is good or bad and I expect that you are familiar with AWS, learned about lambda and have <em>played</em> a bit with it.</p>
<h2 id="overview">Overview</h2>
<p>A simplified architecture of serverless application may look like this:
<br></p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/simple_serverless.png"
        alt/></span></span>
<br>
I intentionally omitted Route53, VPC and a couple of other services (which you most likely will need) to keep focus on the core serverless components.</p>
<p>Usually we use S3+CloudFront for all the static content and all the requests go through API Gateway to lambda, which is used as a backend-core. CloudWatch also plays an important role in aggregating logs and in some cases triggers lambda functions at a specific day/time. From lambda we can access a database to save/load the data, SES/SNS to send notifications and multiple 3rd party services.</p>
<p>You can read more details on serverless webapp creation <a href="https://aws.amazon.com/getting-started/serverless-web-app/"target="_blank">here</a>. Now let&rsquo;s focus on some issues, which you may face while designing and developing a serverless application.
<br></p>
<h3 id="vpc-or-no-vpc">VPC or no-VPC?</h3>
<p>VPC is one of the core services and is used in almost every project, however there are a couple of extra things we need to consider with serverless architecture.</p>
<p>Usually, a database is placed into a private subnet and instance which requires an internet access and database access can be placed into a public subnet. You <strong>cannot</strong> do it with lambda. <a href="https://docs.aws.amazon.com/lambda/latest/dg/vpc.html"target="_blank">Each lambda is assigned a private IP address, but is not assigned any public IP addresses</a>, so you <strong>have to</strong> place your lambda function into a private subnet. If lambda requires an internet access, you will have to add a NAT Gateway/instance, which costs money and you will end-up paying for what you could get for free in a non-serverless approach.</p>
<p>Another important thing to watch for when using VPC+Lambda combination is the number of available IP addresses in your subnet. And it may not be easy, as you don&rsquo;t control the number of lambda functions running at the moment. So one of the <a href="https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html"target="_blank">Lambda Best Practices</a> is: <strong>Don&rsquo;t put your lambda function in a VPC unless you really have to</strong>.
<br></p>
<h3 id="rds-and-sql-databases-in-serverless-world">RDS and SQL databases in serverless world</h3>
<p>SQL databases are not easy to scale. It becomes even more complex in a serverless app, where you don&rsquo;t control how many lambda you have at any given moment, so your database may quickly become a bottleneck. However even in a simpler scenario, where we don&rsquo;t expect thousands of lambdas running at the same time, we may face potential problems.</p>
<p>It&rsquo;s considered good practice to place a heavy initialization code (e.g. connection to the database, loading and applying configuration) out of the lambda handler, because you only want to execute it once instead of loading exactly the same configuration objects on every request.</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="c1"># This will be run only once for each lambda (when it&#39;s created)</span>
<span class="n">config</span> <span class="o">=</span> <span class="n">load_configuration</span><span class="p">()</span>

<span class="c1"># This will be run on every call to lambda function</span>
<span class="k">def</span> <span class="nf">handler_name</span><span class="p">(</span><span class="n">event</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
    <span class="c1"># use configuration object</span>
    <span class="o">...</span>
    <span class="k">return</span> <span class="n">some_value</span>
</code></pre></div><br>
Creating a database connection only once in the beginning and reusing it for multiple requests makes sense as it affects performance (in a good way). However with the lambda we will face the following problem: we don't know when to close the connection and the lambda function may be *killed* at any moment. As a result, we may end up with many stale connections.
<p>So, creating a database connection every time the request is received and closing it after the request is handled <strong>is not such a bad idea</strong> when using RDS + Lambda.
<br></p>
<h3 id="warming-up-your-lambdas">Warming up your lambdas</h3>
<p>If your lambda is not used, it will eventually be <em>killed</em>. The good thing is that it all happens automatically, AWS scales it up and down depending on the load, and we don&rsquo;t have to worry about this.</p>
<p>The bad thing is that, eventually, we may have a situation when there are no running lambdas at the moment. The one will be created as soon as customer triggers a corresponding functionality. The problem with that is lambda initialization may take a couple of seconds, plus some time is required to handle the request. 2018 is not the year when customer should be waiting 5-6 seconds (network delay is not even considered here) for a response from the server. It may hurt your business. The solution is simple: we can create a CloudWatch rule which will trigger the lambda ever X minutes (There are no information on how often exactly the lambda should be triggered to be alive. You may do your own experiments or research, but ~15 min should solve the problem) to keep at least one instance of lambda alive. In the handler we can catch this <em>keep-alive</em> CloudWatch event and return from function immediately.</p>
<p>However we should remember that each call to lambda does affect our budget.
<br></p>
<h3 id="deployment">Deployment</h3>
<p>Obviously we want an easy automated deployment. Fortunately we have multiple options here:</p>
<ul>
<li>CloudFormation/Terraform</li>
<li><a href="https://serverless.com/"target="_blank">Serverless Framework</a> - wrapper on CloudFormation, which give a very nice experience, has tons of plugins and works across different clouds</li>
<li>rich API which AWS provides</li>
<li><a href="https://github.com/apex"target="_blank">apex</a></li>
</ul>
<p>Usually we want to have multiple stages: dev, qa, prod and we will need to run different lambda functions in each stage and you can&rsquo;t have two lambda functions with the same name in the region. I saw very different approaches to <em>solve</em> this problem and support multiple deployment stages:</p>
<ul>
<li>different suffixes for API and lambda in the same region</li>
<li>using different regions</li>
<li>using different accounts for dev and prod</li>
<li><a href="https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variables-to-manage-lambda-functions/"target="_blank">lambda versions and aliases &amp; API Gateway stage variables</a></li>
</ul>
<p>The last two options are the most convenient in my opinion and probably the most popular.
<br></p>
<h3 id="lambda-configuration">Lambda configuration</h3>
<p>It&rsquo;s always convenient when your configuration is decoupled from your code. It simplifies many things and we won&rsquo;t have to redeploy the whole package when everything we want to do is changing log level from <em>Info</em> to <em>Warning</em>.</p>
<p>This is the topic which is discussed often enough during breaks on conferences and meetups. The article <a href="https://www.concurrencylabs.com/blog/configure-your-lambda-function-like-a-champ-sail-smoothly/"target="_blank">Configure your lambda functions like a champ and let your code sail smoothly to Production</a> describes the topic really well. Please, read it, it is worth it.</p>
<p>I will just mention once again, that in your lambda handler you receive two objects: event and context; and you can check what version of lambda was called (to load the corresponding configuration from S3/DynamoDB/etc) using code like this:</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="n">alias</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">invoked_function_arn</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="k">if</span> <span class="n">alias</span> <span class="o">==</span> <span class="s1">&#39;prod&#39;</span><span class="p">:</span>
    <span class="o">...</span>
<span class="k">else</span><span class="p">:</span>
    <span class="o">...</span>
</code></pre></div><br>
<h3 id="other">Other</h3>
<p>The article is already quite long and I haven&rsquo;t mentioned even half of what I wanted to say. I will be writing separate blog posts on other serverless topics. For now I just want to mention a couple of important features, which may be useful:</p>
<ul>
<li>API Gateway caching - is a great and easy way to offload the lambda function, but remember, that it doesn&rsquo;t support different parameters in the same request. For example, the two following requests may return the same result when caching is enabled:</li>
</ul>
<pre><code class="language-api" data-lang="api">GET /v1/products?p1=1&amp;p2=2
</code></pre><pre><code>GET /v1/products?p1=29&amp;p2=28
</code></pre><ul>
<li>CORS - another piece of WebLogic which we can keep on API Gateway side and it&rsquo;s quite easy. Let&rsquo;s never set <code>Access-Control-Allow-Origin</code> to &lsquo;*&rsquo; in production unless it&rsquo;s actually needed</li>
<li>lambda policies - easy to add and it has to be done in a granular level for each alias of your lambda function. If lambda only needs to be called by API Gateway and only by POST request with a unique path, then write a corresponding lambda policy and allow only what is necessary</li>
<li><a href="https://www.youtube.com/watch?v=uxmxVjXjcF4"target="_blank">Serverless application security</a> is in not in a bad state, but it continues to be a challenge and developer still has to carry about many things, and the most popular one <code>SQL injection</code> is still here.
<br></li>
</ul>
]]></content:encoded>

    </item>

    <item>
      <title>A safe approach to project setup</title>
      <link>/cpp-basic-project-setup.html</link>
      <pubDate>Sun, 08 Jan 2017 12:24:00 +0000</pubDate>

      <guid>/cpp-basic-project-setup.html</guid>
      <description>A ship will sail the way you build it.  TLDR Proper project setup may require more effort in the beginning, but it will save you months in the long term by reducing the likelihood of a mistake and by simplifying the learning curve for new developers.
Here are some techniques (in random order) that you should consider using in your development process and your project setup. The bolded items will be the focus of this blogpost.</description>

      <content:encoded><![CDATA[<div align="right"><i>
A ship will sail the way you build it.
</i></div>
<h3 id="tldr">TLDR</h3>
<p>Proper project setup may require more effort in the beginning, but it will save you months in the long term by reducing the likelihood  of a mistake and by simplifying the learning curve for new developers.</p>
<p>Here are some techniques (in random order) that you should consider using in your development process and your project setup. The bolded items will be the focus of this blogpost.</p>
<ul>
<li><strong>source code control</strong></li>
<li><strong>clear project structure</strong></li>
<li><strong>maximum warning level</strong></li>
<li><strong>unit tests</strong></li>
<li><strong>style consistency</strong></li>
<li>continuous integration</li>
<li>static code analysis</li>
<li>dynamic code analysis</li>
<li>code reviews</li>
<li>benchmarking</li>
<li>integration tests</li>
</ul>
<p>See <a href="https://github.com/marinsky/cpp_basic_project_setup"target="_blank">sample project on Github</a>. It sets warning level to maximum and treats all warnings as errors; it also uses code style analysis as well as unit testing and mocking frameworks. The project was written in C++ (and this blog post is also a bit C++-centric), but everything discussed here is really language agnostic. I used <a href="https://github.com/google/googletest"target="_blank">googletest</a> and <a href="https://github.com/google/styleguide/blob/gh-pages/cpplint/cpplint.py"target="_blank">Google CppLint</a> in my example, but other libraries could be also easily used. If you&rsquo;re from  the C++ world, take a look at <a href="https://www.youtube.com/watch?v=cg7uFBmUh_Q"target="_blank">great talk by Marshall Clow</a> about project setup from CppCon 2014.</p>
<h3 id="background">Background</h3>
<p>I have seen multiple different project setups and the varying outcomes, that resulted. This topic is extremely important, because proper setup will save development time, reduce the  number of errors and inconsistencies, simplify the learning curve for new developers and make everybody&rsquo;s life easier in general.</p>
<p>There are not so many books that emphasize the importance of this topic. As a new developer, you can easily learn about this by looking at some high-quality projects on <a href="https://github.com"target="_blank">GitHub</a>, but you need to know which projects to look at. Plus, if you&rsquo;re just starting your career, usually you do not focus your attention on project setup approaches. However proper project setup has a huge impact on product quality - just as a foundation impacts the quality of the house it supports.</p>
<p><span class="image-container"><span class="link"><img class="img" src="/images/cards.jpg"
        alt/></span></span></p>
<p>Let&rsquo;s go through items from the list above step by step.</p>
<h3 id="source-code-control">Source code control</h3>
<p>Not much to comment on here. Fortunately, nowadays everyone is using it. The important thing here is to be consistent with your work-flow (are you using <a href="https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow"target="_blank">gitflow workflow</a> or something else? Do you have a separate branch for each feature? What versioning approach are you using? Do you create a tag for every release? Do you have a release branch? Or both?).</p>
<h3 id="clear-project-structure">Clear project structure</h3>
<p>By clear I mean that it should be not only intuitive and logical, but that it should also align with worldwide known practices. For example, a common practice is to have folders like <code>src</code>, <code>include</code>, <code>build</code>, <code>test</code> and have a <code>Readme</code> file in the root folder. So it would be unexpected to see the main <code>Readme</code> five directories down or half of the source files in the <code>build</code> folder.</p>
<h3 id="warnings">Warnings</h3>
<p>Every warning should be treated very seriously and cannot be ignored. Compiler developers put tons of effort and do an amazing job to prevent errors by directing us to potentially dangerous pieces of code.</p>
<p>It&rsquo;s always a good idea to enable the maximum warning level (<code>/Wall</code>, <code>/Wextra</code>) and enable the setting &ldquo;treat warnings as errors&rdquo; (<code>/Werror</code>) in new projects.</p>
<p>I witnessed a situation when a colleague of mine spent hours trying to figure out the reason behind the random crash in a huge project. He didn&rsquo;t pay attention to warnings, because the project had thousands of them. The answer was simple:</p>
<div class="highlight"><pre class="chroma"><code class="language-cpp" data-lang="cpp"><span class="kt">void</span> <span class="nf">foo</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">s</span><span class="p">(</span><span class="s">&#34;test&#34;</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;%s&#34;</span><span class="p">,</span> <span class="n">s</span><span class="p">);</span>  <span class="c1">// Oops! char* (s.c_str()) is expected.
</span><span class="c1"></span><span class="p">}</span>
</code></pre></div><p>This situation occurred long ago. Actually, these days many (but not all) modern compilers would generate an error and not a warning on the <code>printf</code> line. Still, this example illustrates the situation well. If the project was better set up, it would take two seconds to fix it or it would likely not happen at all.</p>
<p>It&rsquo;s harder with legacy code, which has hundreds of warnings, but I have no doubts, that it&rsquo;s worthwhile to invest time and clean all those warnings slowly, step by step.</p>
<h2 id="unit-tests">Unit tests</h2>
<p>This item is the most important one. If you want to integrate only one item from the list above, integrate this one. Unit tests have been popular for a long time. There are hundreds of books about this topic. All the languages I have worked with (procedural, OO, functional and even very specialized ones like <a href="https://en.wikipedia.org/wiki/LabVIEW"target="_blank">LabVIEW</a>) have <strong>free</strong> libraries for unit testing. Many languages even come with unit test libraries out of the box; but surprisingly, there are tons of serious projects out there (even new ones, not only legacy projects), which don&rsquo;t use tests.</p>
<p>Not only do unit tests help you to verify your code through different execution paths and edge cases, prevent regression errors, and make new developers more confident in making changes, but they also force you to design your classes/interfaces/functions better and decouple modules from each other.</p>
<p>Another approach I see very often is writing unit tests, but not using <a href="https://en.wikipedia.org/wiki/Mock_object"target="_blank">mock objects</a>. Many developers don&rsquo;t know about them. It&rsquo;s definitely better having tests without mocks than not having tests at all. However when you don&rsquo;t use mocks, you are most likely using the Unit Test library to create integration tests. As a result, you don&rsquo;t test the failure path, you don&rsquo;t simulate exceptions which may be thrown in the dependency of testable objects, and you don&rsquo;t decouple modules from each other well enough. The result is likely that the execution time of your test is very long. Mock frameworks are very powerful (and very often <strong>free</strong>) tools, so there is no reason not to use them. <a href="https://github.com/marinsky/cpp_basic_project_setup/blob/master/test/temperature_monitor_test.cpp"target="_blank">This example of unit test</a> illustrates how valuable mocks can be (MockObject is injected into testable object as its dependency).</p>
<p>Moreover,  don&rsquo;t go to the other extreme - <em>mocks returning mocks returning mocks</em> is a sign of a bad design. <a href="https://www.destroyallsoftware.com/blog/2014/test-isolation-is-about-avoiding-mocks"target="_blank">This post from Gary Bernhardt</a> explains very well what &lsquo;too many mocks&rsquo; means.</p>
<p><u>Unit test execution should be part of the build.</u></p>
<p>In my opinion, it&rsquo;s important to run your tests as part of the build (and keep execution time short enough). If your total test execution time takes hours, then nobody will bother running tests before pushing new changes.</p>
<p>Tests almost always take a long time (let&rsquo;s exclude situations when your project has hundred millions lines of code and you have millions of tests) because you need to create and then remove databases, or send a network request. This means, you&rsquo;re writing an integration test. It may be useful to have integration tests, but they should be an addition to the unit tests, not a replacement. Nevertheless, integration tests are not in the scope of this post.</p>
<p>Unlike in the past, everyone uses source code control these days. I have no doubts, that eventually unit tests will be where source code control is now - <u>everywhere</u>. Start using it today (if you aren&rsquo;t already) and your product quality will improve dramatically, allowing you to pull ahead of your competitors.</p>
<h3 id="style-consistency">Style consistency</h3>
<p>Do you like seeing spaces and tabs in the same file? Do you enjoy having CamelCase and underscore_style classes/methods/variables in the same project? What about methods which are two thousand  lines long, half of those are commented out? Or ten empty lines between two lines of code?</p>
<p>I know people who don&rsquo;t care about that, but I don&rsquo;t really know anyone who enjoys it. Most people want the code to be consistent. Consistency makes code easier to read and maintain. If you&rsquo;re using well-known code style practices (which you should), it will make the life of new developers much easier. Yet, it&rsquo;s hard to be consistent even when you are the only one who writes the whole project. I believe that the only way to achieve style consistency is to use a tool that analyzes all your files. It is important to run this tool as a part of your build and then treat output of the tool as compilation errors. All the languages that I know have such tools (often you have many of them for your language): <a href="https://stylecop.codeplex.com/"target="_blank">StyleCop</a> for C#, <a href="https://bitbucket.org/verateam/vera/wiki/Home"target="_blank">Vera++</a> or <a href="https://github.com/google/styleguide/tree/gh-pages/cpplint"target="_blank">Google CppLint</a> for C++, Go lang comes with <a href="https://golang.org/cmd/gofmt/"target="_blank">a style checker</a> out of the box, etc.</p>
<p>Don’t worry if you may have to customize or disable some rules. Every team/project has different preferences.</p>
<p>In my example project I used Google CppLint and I enabled usage of C++11 features, streams and references. It also worth the mention that if you&rsquo;re from C++ word, it&rsquo;s a good idea to keep an eye on <a href="https://github.com/isocpp/CppCoreGuidelines"target="_blank">core guidelines</a> and the automation tools that come with them.</p>
<h3 id="conclusion">Conclusion</h3>
<p>Chaos creates chaos. If from the start your project has multiple warnings, no tests, tricky  project structure and inconsistent style, all future commits will look the same. The probability of mistakes will be high, even if you hire top-notch developers to add a new feature.</p>
<p>Invest time in the project setup from the very beginning and always pay attention to the structure of great projects in order to learn from them.</p>
]]></content:encoded>

    </item>

    <item>
      <title>Raw pointers in modern C&#43;&#43;</title>
      <link>/raw-pointers-in-cpp.html</link>
      <pubDate>Sat, 01 Oct 2016 03:42:00 +0000</pubDate>

      <guid>/raw-pointers-in-cpp.html</guid>
      <description>- Hi. May I have 3 owning raw pointers and a couple of delete keywords, please?
- No, sir. Not anymore. Not in 2016.  The rule There is almost no need to use owning raw pointers and delete keyword in today&amp;rsquo;s C++. It is highly unlikely that your situation is exceptional unless you&amp;rsquo;re working on a brand new, super cool, memory manager library, or another very specific low-level tool.</description>

      <content:encoded><![CDATA[<div align="right"><i>
- Hi. May I have 3 owning raw pointers and a couple of delete keywords, please?<br>
- No, sir. Not anymore. Not in 2016.
</i></div>
<h3 id="the-rule">The rule</h3>
<p>There is <em>almost</em> no need to use owning raw pointers and delete keyword in today&rsquo;s C++. It is highly unlikely that your situation is exceptional unless you&rsquo;re working on a brand new, super cool, memory manager library, or another very specific low-level tool.</p>
<h3 id="owning-vs-non-owning">Owning vs non-owning</h3>
<p>The word &lsquo;owning&rsquo; is important. All problems described below are related to owning pointers. There is nothing wrong with non-owning raw pointers.</p>
<div class="highlight"><pre class="chroma"><code class="language-cpp" data-lang="cpp"><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="k">const</span> <span class="n">SomeClass</span><span class="o">*</span> <span class="k">const</span> <span class="n">p1</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">SomeClass</span><span class="o">*</span> <span class="n">p2</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SomeClass</span><span class="p">();</span>
    <span class="c1">// p1 - non-owning, p2 - owning
</span><span class="c1"></span>    <span class="c1">// ...
</span><span class="c1"></span><span class="p">}</span>
</code></pre></div><h3 id="whats-wrong-with-raw-pointers">What&rsquo;s wrong with raw pointers</h3>
<p>The raw pointer was an amazing invention in its time. They simplified development and pushed our industry forward. However, that time has passed and today, they generate a lot of potential problems. Some of them are shown below. Let&rsquo;s take a look at this simple snippet. Do you see the mistake?</p>
<div class="highlight"><pre class="chroma"><code class="language-cpp" data-lang="cpp"><span class="kt">void</span> <span class="nf">use_resource</span><span class="p">(</span><span class="k">const</span> <span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">auto</span> <span class="n">buff</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">int</span><span class="p">[</span><span class="n">n</span> <span class="o">*</span> <span class="mi">1024</span><span class="p">];</span>
    <span class="c1">// do some operation with the buff
</span><span class="c1"></span>    <span class="k">delete</span> <span class="n">buff</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>First, we have a well-known memory leak problem. If the exception happens between allocation and deallocation, then memory owned by <code>buff</code> will not be released. And even if you never use exceptions and you&rsquo;re sure that all people who will work with your code do the same, there is still a danger. For example, in time someone may write a validation line and jump out of the method before deallocation happens:</p>
<div class="highlight"><pre class="chroma"><code class="language-cpp" data-lang="cpp"><span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&gt;</span> <span class="mi">1000</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// some validation
</span><span class="c1"></span>    <span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>Another problem with the <code>use_resource()</code> snippet above is that I accidentally used <code>delete</code> instead of <code>delete[]</code> (we are people and people make mistakes sometimes, right?). And, guess what? Memory leak? It&rsquo;s actually worse - we got <strong>undefined behaviour</strong>. <br>
Now let&rsquo;s try to be more defensive and wrap <code>buff</code> allocation and de-allocation into a class:</p>
<div class="highlight"><pre class="chroma"><code class="language-cpp" data-lang="cpp"><span class="k">class</span> <span class="nc">Resource</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
    <span class="n">Resource</span><span class="p">()</span> <span class="p">{</span>
        <span class="n">buff_</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">int</span><span class="p">[</span><span class="mi">10</span><span class="p">];</span>
        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">buff_</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="kt">void</span> <span class="nf">use</span><span class="p">()</span> <span class="p">{</span>
        <span class="c1">// do some operation with the resource, print second element for example
</span><span class="c1"></span>        <span class="n">printf</span><span class="p">(</span><span class="s">&#34;%d</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">buff_</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
    <span class="p">}</span>

    <span class="o">~</span><span class="n">Resource</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">delete</span><span class="p">[]</span> <span class="n">buff_</span><span class="p">;</span>
    <span class="p">}</span>

<span class="k">private</span><span class="o">:</span>
    <span class="kt">int</span><span class="o">*</span> <span class="n">buff_</span>
<span class="p">}</span>
</code></pre></div><p>It&rsquo;s better, right? Now, we can write:</p>
<div class="highlight"><pre class="chroma"><code class="language-cpp" data-lang="cpp"><span class="kt">void</span> <span class="nf">use_resource</span><span class="p">(</span><span class="k">const</span> <span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">Resource</span> <span class="n">buff</span><span class="p">;</span>
    <span class="c1">// do some operation with the buff
</span><span class="c1"></span><span class="p">}</span> <span class="c1">// here buff destructor will be called automatically
</span></code></pre></div><p>The code became simpler and we don’t need to worry about memory leaks because the destructor (which will be called automatically when buff goes out of scope) will take care of everything. Unfortunately, this code still has problems. Imagine the following use case:</p>
<div class="highlight"><pre class="chroma"><code class="language-cpp" data-lang="cpp"><span class="kt">void</span> <span class="nf">add_resource</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Resource</span><span class="o">&gt;&amp;</span> <span class="n">resources</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">save</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">Resource</span> <span class="n">new_resource</span><span class="p">;</span>
    <span class="c1">// do something with new resource
</span><span class="c1"></span>    <span class="n">new_resource</span><span class="p">.</span><span class="n">use</span><span class="p">();</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">save</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// save it for future usage
</span><span class="c1"></span>        <span class="n">resources</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">new_resource</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">Resource</span><span class="o">&gt;</span> <span class="n">resources</span><span class="p">;</span>
    <span class="c1">// create 2 resources and save both of them to the vector
</span><span class="c1"></span>    <span class="n">add_resource</span><span class="p">(</span><span class="n">resources</span><span class="p">,</span> <span class="nb">true</span><span class="p">);</span>
    <span class="n">add_resource</span><span class="p">(</span><span class="n">resources</span><span class="p">,</span> <span class="nb">true</span><span class="p">);</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">auto</span><span class="o">&amp;</span> <span class="nl">r</span> <span class="p">:</span> <span class="n">resources</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">r</span><span class="p">.</span><span class="n">use</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><p>Here, when we push resources to the vector, a copy of our object is created. And, oops, I forgot to create a copy constructor, so the compiler generated one for me. The generated constructor performs a so-called <code>shallow copy</code>, which means it copies only the pointer and not the data.  We have two objects that point to the same data. As soon as one of them goes out of scope (the end of <code>add_resource()</code> method), the data is removed. Our resources vector will store dangling pointers. So line <code>r.use_resource()</code> leads to undefined behavior. If you&rsquo;re lucky your program will crash, if not&hellip; well, anything can happen.</p>
<p>Raw pointers have a lot of other disadvantages. See <a href="https://www.amazon.com/Effective-Modern-Specific-Ways-Improve/dp/1491903996/ref=sr_1_1?ie=UTF8&amp;qid=1475279620&amp;sr=8-1&amp;keywords=effective&#43;modern&#43;c%2B%2B"target="_blank">Scott Meyers&rsquo;s book</a> for details or do your own research.</p>
<h3 id="why-do-people-still-use-it">Why do people still use it?</h3>
<p><span class="image-container"><span class="link"><img class="img" src="/images/right_wrong.jpg"
        alt/></span></span></p>
<p>Legacy code is a separate topic. C++ is an old language and there are tons of projects written in it, so obviously they cannot be refactored quickly. Nevertheless, why are owning raw pointers still used so heavily in modern C++ in recent projects? The only answer I have is “bad habit”. There were multiple talks, books, blogs (see below), but not everyone unlearned the old stuff and learned the better way.</p>
<h3 id="so-what-to-use-instead">So&hellip; What to use instead?</h3>
<p>In short - use <a href="http://en.cppreference.com/w/cpp/language/raii"target="_blank">RAII</a> idiom</p>
<ul>
<li>Allocate object in the stack if possible</li>
<li>Use <a href="http://en.cppreference.com/w/cpp/memory"target="_blank">smart pointers</a></li>
<li>Only as a last step - wrap your resource into its own class, where you acquire in constructor and release in destructor, but do not forget about copy/move constructor and assignment operators, which may be generated for you</li>
</ul>
<h3 id="a-couple-of-links">A couple of links</h3>
<p>Still not convinced? Take a look at amazing text/videos from C++ experts:</p>
<ul>
<li><a href="https://herbsutter.com/elements-of-modern-c-style/"target="_blank">Herb Sutter&rsquo;s blog post</a> on modern C++</li>
<li><a href="https://www.youtube.com/watch?v=nesCaocNjtQ"target="_blank">Bjarne Stroustrup&rsquo;s talk at CPPCON 2014</a>. <a href="https://www.youtube.com/watch?v=nesCaocNjtQ#t=30m50s"target="_blank">Jump to 30:50</a> for Resource example</li>
<li><a href="https://www.amazon.com/Effective-Modern-Specific-Ways-Improve/dp/1491903996/ref=sr_1_1?ie=UTF8&amp;qid=1475279620&amp;sr=8-1&amp;keywords=effective&#43;modern&#43;c%2B%2B"target="_blank">Scott Meyers&rsquo; Effective Modern C++ Book</a> Chapter 4</li>
</ul>
]]></content:encoded>

    </item>

  </channel>
</rss>
