<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: detoix</title>
    <description>The latest articles on DEV Community by detoix (@detoix).</description>
    <link>https://dev.to/detoix</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F304046%2Fda12e00a-fd8b-4003-9b47-05a213d5313b.png</url>
      <title>DEV Community: detoix</title>
      <link>https://dev.to/detoix</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/detoix"/>
    <language>en</language>
    <item>
      <title>How many Harry Potters can your team maintain?</title>
      <dc:creator>detoix</dc:creator>
      <pubDate>Tue, 24 Oct 2023 21:04:49 +0000</pubDate>
      <link>https://dev.to/detoix/how-many-harry-potters-can-your-team-maintain-j1g</link>
      <guid>https://dev.to/detoix/how-many-harry-potters-can-your-team-maintain-j1g</guid>
      <description>&lt;p&gt;Talking to the business is hard. Convincing them that your application requires time for refactoring or maintenance is even harder. I've been there and I want to share with you an interesting metric that bridges business people and engineers.&lt;/p&gt;

&lt;p&gt;A couple of months ago I was assigned with a task to explain why we need time for refactoring, why can't we just remove the code we don't need. And it was a struggle because I didn't know how to quantify the system we had. How do I tell the business people how much cognitive effort we put into just understanding what's under the hood of a running system?&lt;/p&gt;

&lt;p&gt;And I came up with an idea to compare it to a book. Simple as that: every line of code takes time to read and understand, just as every line of a book.&lt;/p&gt;

&lt;h2&gt;
  
  
  How much is a Harry Potter?
&lt;/h2&gt;

&lt;p&gt;I chose Harry Potter series because it's well-known (eventually it turned out to be the bull's eye). I found &lt;a href="https://wordcounter.net/blog/2015/11/23/10922_how-many-words-harry-potter.html"&gt;a couple of places like this&lt;/a&gt; that claim that &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the Harry Potter books contain 1,084,170 words&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yeah, maybe. My concern was to find out lines count.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why lines?
&lt;/h2&gt;

&lt;p&gt;A line of code is an entity a developer works with in the first place. Also, it is easy to imagine a line of written text. My assumption here is that every word somewhat corresponds with a single token in code (variable, assignment, invocation), because it represents a single thing to understand in the bigger context.&lt;/p&gt;

&lt;p&gt;So I decided to find out myself how many lines are there in the Harry Potter books. And the results are as follows.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Word count&lt;/th&gt;
&lt;th&gt;Line count&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Harry Potter and the Philosopher's Stone&lt;/td&gt;
&lt;td&gt;76,944&lt;/td&gt;
&lt;td&gt;7,890&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Harry Potter and the Chamber of Secrets&lt;/td&gt;
&lt;td&gt;85,141&lt;/td&gt;
&lt;td&gt;8,695&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Harry Potter and the Prisoner of Azkaban&lt;/td&gt;
&lt;td&gt;107,253&lt;/td&gt;
&lt;td&gt;11,190&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Harry Potter and the Goblet of Fire&lt;/td&gt;
&lt;td&gt;190,637&lt;/td&gt;
&lt;td&gt;19,077&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Harry Potter and the Order of the Phoenix&lt;/td&gt;
&lt;td&gt;257,045&lt;/td&gt;
&lt;td&gt;23,004&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Harry Potter and the Half-Blood Prince&lt;/td&gt;
&lt;td&gt;168,923&lt;/td&gt;
&lt;td&gt;13,454&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Harry Potter and the Deathly Hallows&lt;/td&gt;
&lt;td&gt;198,227&lt;/td&gt;
&lt;td&gt;20,630&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1,084,170&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;103,940&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I did some double checks and confirmed these numbers - &lt;strong&gt;100k lines in the whole series&lt;/strong&gt;. Now, how does it compare to actual code? I used some of my &lt;a href="https://github.com/detoix/git_scripts#oneliners"&gt;onliners&lt;/a&gt; to get some data about popular repositories.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Token count&lt;/th&gt;
&lt;th&gt;Line count&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/autofac/Autofac"&gt;Autofac&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;152,249&lt;/td&gt;
&lt;td&gt;42,137&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/mrdoob/three.js"&gt;three.js&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;1,614,528&lt;/td&gt;
&lt;td&gt;272,856&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/pandas-dev/pandas"&gt;pandas&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;1,804,215&lt;/td&gt;
&lt;td&gt;457,886&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3,570,992&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;772,879&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As you can easily notice, the ratios are much different. Whereas in a typical novel there are about ten words per line, in the code I pulled there is less than 5 tokens per line.&lt;/p&gt;

&lt;p&gt;These two numbers are difficult to compare, but I would argue that software code requires significantly more cognitive effort to be understood. Remember that we're doing this to establish some common ground between business and software people, these numbers don't have to be equal.&lt;/p&gt;

&lt;p&gt;This is the part where you can disagree, but for the sake of simplicity of calculations I claim the following.&lt;/p&gt;

&lt;h2&gt;
  
  
  1 Harry Potter ~ 100k lines of code
&lt;/h2&gt;

&lt;p&gt;With this in mind I literally put it on a Power Point slide and said that &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;we currently maintain an equivalent of &lt;em&gt;X&lt;/em&gt; Harry Potter series with a team of &lt;em&gt;Y&lt;/em&gt; engineers and that's why we have to simplify the system&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and I think i made a point. If you're reading this for the first time and this idea is fresh in your mind, try to imagine being a business professional and learning this from a software engineer. Is it intelligible? Does it help you understand the situation?&lt;/p&gt;

&lt;p&gt;Such comparisons, the &lt;em&gt;Harry Potter Metric&lt;/em&gt; in particular, can be a novel approach to bridging the gap between business professionals and engineers; it's simple to calculate and easy to understand for both parties.&lt;/p&gt;

&lt;p&gt;So, how many Harry Potters can your team maintain?&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>software</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>SRP for couples</title>
      <dc:creator>detoix</dc:creator>
      <pubDate>Sun, 03 Jan 2021 15:09:29 +0000</pubDate>
      <link>https://dev.to/detoix/srp-for-couples-1fmj</link>
      <guid>https://dev.to/detoix/srp-for-couples-1fmj</guid>
      <description>&lt;p&gt;Single Responsibility Principle is a well-recognized rule that all software developers (and, as you're going to see, others as well) should follow in order to make systems maintainable and easy to extend.&lt;/p&gt;

&lt;p&gt;There are many definitions and ways to explain it. Here I'd like to lay out what I believe to be a comprehensive real-world explanation of how should this principle be applied. And to do that, we're going to visit a couple's bedroom.&lt;/p&gt;

&lt;h1&gt;
  
  
  Bedroom drawers
&lt;/h1&gt;

&lt;p&gt;When two people live with each other, in their bedroom, there usually are drawers with underwear. And there is not one, but two of them. It's common sense, but let's spend some time to find out why is it so. Why are there 2 separate drawers and, particularly, why is their responsibility split like that?&lt;/p&gt;

&lt;h1&gt;
  
  
  Other setups
&lt;/h1&gt;

&lt;p&gt;Let's ponder for a while and imagine that you're moving in with someone, and you need to find out how where to put your socks and underwear. Some of the possibilities are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Socks in one drawer, underwear in the other one&lt;/li&gt;
&lt;li&gt;White in one drawer, colored in the other one&lt;/li&gt;
&lt;li&gt;His in one drawer, hers in the other one&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Problem setting
&lt;/h1&gt;

&lt;p&gt;What we generally want to achieve when developing a working (software) system is loose coupling. In our example, we expect a solution that allows for maximum independence of both partners, so it is not acceptable that one person waits until the other one dresses up. &lt;/p&gt;

&lt;p&gt;If we, for instance, split responsibilities with the second solution we've listed above, out actors will encounter a problem when one of them will look for white socks, whereas the other one will be waiting for their turn to pick theirs.&lt;/p&gt;

&lt;h1&gt;
  
  
  The principle
&lt;/h1&gt;

&lt;p&gt;The only way to prove one solution better than other is to follow the Single Responsibility Principle, which states that "an entity should have only one reason to change".&lt;/p&gt;

&lt;p&gt;This reason is that &lt;em&gt;a person wants to get dressed&lt;/em&gt;. Putting both partners' socks in one drawer and underwear in the other one causes that for any drawer there are two reasons to change - it is that &lt;em&gt;any&lt;/em&gt; of two partners wants to get something and it leads to obvious problems.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Design your systems like bedroom drawers: let your code areas change independently by different reasons and you will find out that it is nothing more than the same common sense that leads to putting underwear in bedroom drawings in this well-known way.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>solid</category>
      <category>srp</category>
    </item>
    <item>
      <title>3 ways to use boolean in a bad way</title>
      <dc:creator>detoix</dc:creator>
      <pubDate>Fri, 24 Apr 2020 15:41:37 +0000</pubDate>
      <link>https://dev.to/detoix/3-ways-to-use-boolean-in-a-bad-way-21ad</link>
      <guid>https://dev.to/detoix/3-ways-to-use-boolean-in-a-bad-way-21ad</guid>
      <description>&lt;h1&gt;
  
  
  1. Carry checkbox up to your high-level code
&lt;/h1&gt;

&lt;p&gt;Every application is a sort of data processing. As such, in order to provide output, it requires some input. More often than not it’s a so-called “user input” and among user interfaces there happen to be checkboxes.&lt;/p&gt;

&lt;p&gt;A checkbox indicate that some option is enabled or disabled and, as such, is a valuable way of controlling application flow. But a developer’s job is to put code into layers and split the ugly low-level code from the pure object-oriented (or functional) high-level code.&lt;/p&gt;

&lt;p&gt;Unfortunately, we often fail and as we receive the checkbox value in a form of &lt;strong&gt;isPremium&lt;/strong&gt;, &lt;strong&gt;isValid&lt;/strong&gt; or &lt;strong&gt;isAuthorized&lt;/strong&gt;, we tend to pass this argument deep into the application.&lt;/p&gt;

&lt;p&gt;It should never be like this, because the more objects or functions know about &lt;strong&gt;isPremium&lt;/strong&gt; or similar, the more conditional statements will appear to handle this information.&lt;/p&gt;

&lt;p&gt;And the solution is to convert the raw input data as soon as possible into a high-level domain code with objects, higher-order functions and so on.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Use boolean to indicate objects interaction
&lt;/h1&gt;

&lt;p&gt;Dealing with object-to-object interactions within a video game is an incredibly difficult process. So we have a character which is able co crunch when there’s not enough space to walk. How would you introduce such a feature?&lt;/p&gt;

&lt;p&gt;Do you think that the character can check for &lt;strong&gt;isSomethingAbove&lt;/strong&gt; and depending on that flag switch to crunching? Of course not, the character should be responsible for its actions, there should be some communication between character and its environment!&lt;/p&gt;

&lt;p&gt;Such a model appears mostly when the developer does not expect much behavior from some minor thing. Here for example we may have a low ceiling that makes our character crunch. As the sole responsibility of the ceiling is to change the way the character moves, one may avoid to introduce a separate entity for it. But it's wrong! Even a single behavior is enough to delegate this responsibility for a separate entity!&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Expose boolean in an API
&lt;/h1&gt;

&lt;p&gt;Let’s assume you have a code where you need to compare two objects by their nature. For example, your application deals with door-to-door deliveries, you have to compare addresses for equality and expose &lt;strong&gt;areEqual&lt;/strong&gt; to some sort of API.&lt;/p&gt;

&lt;p&gt;Looks good, but remember that a boolean can only provide true or false! So there comes a business request to change the conditions for equality, but you still can expose only true or false. Can you see the problem?&lt;/p&gt;

&lt;p&gt;The problem is that as you have exposed a boolean you cannot tell everyone that it is no longer valid! Maybe someone depends on it and will never know that a "new false" is a different "false".&lt;/p&gt;

&lt;p&gt;If you need to expose raw data in such a form, always use some sort of nullable or enum to make sure you can easily make the field obsolete to tell everyone they can no longer count on it as they did before.&lt;/p&gt;

</description>
      <category>cleancode</category>
    </item>
  </channel>
</rss>
