<?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: Bob</title>
    <description>The latest articles on DEV Community by Bob (@bob).</description>
    <link>https://dev.to/bob</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%2F16737%2Ff079a07a-f716-4b51-98b8-9956c5e7b327.jpg</url>
      <title>DEV Community: Bob</title>
      <link>https://dev.to/bob</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bob"/>
    <language>en</language>
    <item>
      <title>How do you know your code is bad?</title>
      <dc:creator>Bob</dc:creator>
      <pubDate>Sun, 23 Jul 2017 14:01:14 +0000</pubDate>
      <link>https://dev.to/bob/how-do-you-know-your-code-is-bad</link>
      <guid>https://dev.to/bob/how-do-you-know-your-code-is-bad</guid>
      <description>

&lt;p&gt;The other day, I was watching &lt;a href="https://www.youtube.com/watch?v=TMuno5RZNeE"&gt;this video&lt;/a&gt; of Robert Martin where he mentions the three symptoms to spot a bad code. And I couldn’t have agreed more.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Rigid Code
&lt;/h3&gt;

&lt;p&gt;Your code is rigid if you can’t change a piece of code without changing the other modules that are irrelevant and have no relationship to the code you try to change.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rigid code is the code that has dependencies that snake out in so many directions that you cannot make an isolated change without changing everything else around it. — Robert Martin&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For instance, you try to change some low-level detail in the data layer, and all of a sudden you get a compile error in the class that does the formatting for your View.&lt;/p&gt;

&lt;p&gt;If you often find yourself touching almost all the modules of your project, whenever you make changes in a single class, it might mean a symptom of rigid code.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Fragile Code
&lt;/h3&gt;

&lt;p&gt;Fragile code is a lot worse than Rigid code. At least we get a compile error in a rigid code.&lt;/p&gt;

&lt;p&gt;In fragile code, if you change a small piece of code in a module, you break an entirely different feature in an entirely different module. And there is no easy way to find these errors during development.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fragile code breaks in bizarre and strange ways that you cannot predict. — Robert Martin&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you often get customer tickets saying a feature is broken, whenever you push a fix for an entirely different use case, it might mean your code is fragile.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Non-reusable code
&lt;/h3&gt;

&lt;p&gt;Non-reusable code is, as the name suggests, the code that cannot be reused.&lt;/p&gt;

&lt;p&gt;For instance, you might want to implement a feature in your project that is the same as the one your colleague has done for a different project. But you cannot easily reuse the code from that project because that code depends on some other irrelevant feature of their project which in turn depends on some framework or database system which you don’t want in your project. And you better off writing your own implementation for that feature.&lt;/p&gt;

&lt;p&gt;This is the famous &lt;a href="https://www.johndcook.com/blog/2011/07/19/you-wanted-banana/"&gt;Gorilla-Banana problem&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You want a banana but what you get is a gorilla holding a banana and the entire jungle with it. — Joe Armstrong&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  How can we avoid these 3 flaws?
&lt;/h1&gt;

&lt;p&gt;When you write code, ask yourselves these questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Is this code too rigid? Is it possible to change the internals of this module in the future without touching the code in other modules and other layers?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is this code too fragile? Will it be hard to find all the places to refactor for any changes in the future?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Should this feature be reusable? If so, does this code depends on any unwanted modules or frameworks that can be avoided?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we look closely, the common thread of all the above three problems is coupling. The modules depend upon each other in undesirable ways and that results in spaghetti code.&lt;/p&gt;

&lt;p&gt;The code should be decoupled across the modules and layers. High-level policies and the abstractions should not depend on low-level details. &lt;a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle"&gt;Invert the dependency&lt;/a&gt; of the modules at the necessary places. And write classes that do only one thing and have &lt;a href="https://8thlight.com/blog/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html"&gt;only one reason to change&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Good code should explain what it's doing. It should be boring to read. Everything is perfectly obvious. That is good code. — Robert Martin&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Also published in my &lt;a href="https://medium.com/thoughts-overflow/how-do-you-know-your-code-is-bad-c76359be9c3b"&gt;Medium publication&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;


</description>
      <category>coding</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>Use Optional / Nullable Reference Types Judiciously</title>
      <dc:creator>Bob</dc:creator>
      <pubDate>Sun, 16 Apr 2017 09:17:41 +0000</pubDate>
      <link>https://dev.to/bob/use-optional--nullable-reference-types-judiciously</link>
      <guid>https://dev.to/bob/use-optional--nullable-reference-types-judiciously</guid>
      <description>&lt;p&gt;Languages such as Swift, Kotlin has a distinction in the reference types — &lt;a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309"&gt;Optional and Non-Optional references&lt;/a&gt; or &lt;a href="https://kotlinlang.org/docs/reference/null-safety.html"&gt;Nullable and Non-Null types&lt;/a&gt; — where only an Optional or Nullable type can hold a nil or null reference.&lt;/p&gt;

&lt;p&gt;Java SE 8 also has support for &lt;a href="http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html"&gt;Optional&lt;/a&gt;, but the difference is that in Swift and Kotlin a normal field cannot hold a null reference. It has to be declared an Optional type to hold a null. Java is not that strict that it allows null reference to normal fields also, but it is possible to write better APIs in Java also using Optional.&lt;/p&gt;

&lt;p&gt;All these type systems try their best to avoid the null references — &lt;a href="https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions"&gt;the billion dollar mistake&lt;/a&gt; as the inventor of null reference Tony Hoare called it.&lt;/p&gt;

&lt;p&gt;If you are switching from a language that doesn’t have Optional — like Java 7 or Objective-C — to a language that supports Optional, chances are that you merely translate all your fields to Optional. But that’s not a very good practice.&lt;/p&gt;

&lt;p&gt;It is very important to understand what problem this Optional type solves. &lt;strong&gt;The advantage compared to the normal null references is that the Optional types forces you — or the client of your API — to think about the case when the value is not present.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That’s it. By declaring a field as an Optional you declare that it is possible that the field can be empty and it makes the clients of the API handle the empty value case.&lt;/p&gt;

&lt;p&gt;Let’s assume there is a class, Customer and it has two fields: customerId and customerName. And for any customer, these two fields cannot be empty, logically.&lt;/p&gt;

&lt;p&gt;If you declare these two fields as Optional, you declare that these fields can be empty. And &lt;strong&gt;this creates a potential for having a customer object in an invalid state in the runtime&lt;/strong&gt; — that is a customer object that has empty id or name but which cannot be empty based on your business logics.&lt;/p&gt;

&lt;p&gt;Having an object in an invalid state is more dangerous than a Null Pointer Exception as the invalid object will not crash your app but make your app behave in a weird manner and it is very hard to debug the cause.&lt;/p&gt;

&lt;p&gt;So think hard before declaring a field as an Optional whether that field can be empty logically. If some fields cannot be null based on your business logics, don’t declare it as an Optional. If you need to declare some field that cannot be null logically as an Optional for some special use-cases, you may first have to think of other possibilities or restructuring your code rather than using Optional as the first choice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Having every field as Optional references and using safe calls or optional chaining ( bob?.department?.head?.name ) to access the values might avoid you Null Pointer Exceptions in the beginning. But it will bite you in the long run as you might get null values even for the fields that cannot have null value logically and leave the objects in invalid states and worse you have no easy way to figure that out.&lt;/strong&gt;&lt;/p&gt;

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