<?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: wrong-about-everything</title>
    <description>The latest articles on DEV Community by wrong-about-everything (@wrongabouteverything).</description>
    <link>https://dev.to/wrongabouteverything</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%2F49704%2F62eaa98c-b0fb-4f55-93ac-0694c3f01014.jpeg</url>
      <title>DEV Community: wrong-about-everything</title>
      <link>https://dev.to/wrongabouteverything</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wrongabouteverything"/>
    <language>en</language>
    <item>
      <title>Static classes are evil</title>
      <dc:creator>wrong-about-everything</dc:creator>
      <pubDate>Wed, 20 Dec 2017 10:48:18 +0000</pubDate>
      <link>https://dev.to/wrongabouteverything/static-classes-are-evil-44jg</link>
      <guid>https://dev.to/wrongabouteverything/static-classes-are-evil-44jg</guid>
      <description>&lt;p&gt;In spite of some languages, e.g., PHP, don’t have such thing as static classes, the concept is still present there. Class consisting entirely of static methods is effectively the same thing as static class.&lt;/p&gt;

&lt;p&gt;Besides static classes are &lt;a href="http://www.yegor256.com/2015/02/20/utility-classes-vs-functional-programming.html"&gt;procedural&lt;/a&gt; and their clients are &lt;a href="http://michalorman.com/2012/04/stop-writing-static-methods/"&gt;untestable&lt;/a&gt; (well, there are some hacks in Java and PHP, but I don’t even want to mention them), they have couple of more issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classes tend to be big to huge
&lt;/h2&gt;

&lt;p&gt;Since classes with static methods have nothing to do with objects, they don’t know who they are, what they should do and what they should not do. The boundaries are blurred, so we just write one instruction after another. It’s hard to stop until we’re done with our task. It is inevitably imperative and non-OOP process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies are hidden
&lt;/h2&gt;

&lt;p&gt;Code is way less readable. What’s in the class? Database query, some intense calculations, email sending? You just don’t control how many of them are in the class. One static method here, one there — and here it is, our new God object. And when you realize that, it’s already too late.&lt;/p&gt;

&lt;h2&gt;
  
  
  Low cohesion
&lt;/h2&gt;

&lt;p&gt;Hence the class is getting less and less cohesive. If the class has a lot of dependencies, chances are that it does more than it should. For the sake of justice I should say that the possible reason of large number of dependencies is that they are at lower abstraction levels. So composing dependencies in higher-level abstractions could be the way to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tight coupling
&lt;/h2&gt;

&lt;p&gt;Static methods mean that they can be called from anywhere. They can be called from a lot of contexts. They have a lot of clients. So if one class needs some little special behavior to be implemented in static method, you need to make sure that none of the other clients got broken. So such reuse simply doesn’t work. I can compare it with noble (and failed) attempt to compose and &lt;a href="https://medium.com/@wrong.about/wrong-ways-of-defining-service-boundaries-d9e313007bcc"&gt;reuse microservices&lt;/a&gt;. The resulting classes are too generic and completely unmaintainable. This results in the whole system being tightly coupled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;As an example let’s consider client’s financial balance (taken from &lt;a href="https://medium.com/@wrong.about/example-of-service-boundaries-identification-e9077c513560"&gt;Payment Service Provider example&lt;/a&gt;). It has all mentioned drawbacks and looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FinancialBalanceUtils&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;reserveBalance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;shouldReserve&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;assertAllConditionsAreMet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;queryFinancialBalanceForCurrentClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;checkFinancialBalance&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;reserveFinancialBalanceForCurrentClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, let’s inspect it from the beginning.&lt;/p&gt;

&lt;p&gt;We are not sure where it is called from as it can be called from absolutely anywhere. So we need to make sure that this method really should be executed, hence &lt;em&gt;shouldReserve()&lt;/em&gt; method.&lt;br&gt;
We are not sure where it is called from, once again! Are all preconditions satisfied? No one knows, so we absolutely must verify this —&lt;em&gt;assertAllConditionsAreMet()&lt;/em&gt; method.&lt;br&gt;
Then we get financial balance by tons of parameters from database with &lt;em&gt;queryFinancialBalanceForCurrentClient()&lt;/em&gt; method, as the query and database table serve the needs of every client who needs financial balance.&lt;br&gt;
OK, we are still not sure if the financial balance we got is fine. We need to check what’s in there — &lt;em&gt;checkFinancialBalance()&lt;/em&gt;.&lt;br&gt;
Aaand finally we really do reserve the balance with &lt;em&gt;reserveFinancialBalanceForCurrentClient()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I omitted logging, error handling and minor code quirks and kept only the essentials. And it’s already too big.&lt;br&gt;
This class is a hidden dependency itself, and it consists of hidden dependencies. How much database queries are executed in this method? I don’t know.&lt;br&gt;
Do you still have any doubts that this class does more than it should?&lt;br&gt;
Was it worth it to allegedly avoid copy-paste which resulted in completely unmaintainable, super-generic code that is really scary to edit?&lt;/p&gt;

&lt;h2&gt;
  
  
  Try OOP instead
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://medium.com/@wrong.about/how-to-avoid-anemic-domain-model-5e1c3e6fe4d0"&gt;Identify your objects&lt;/a&gt;. Focus on “what”, not “how”. Focus on objects, not procedures.&lt;/li&gt;
&lt;li&gt;When objects are identified, make all your dependencies explicit first, and limit their number. Limit it to five per method. Why five? I don’t know, it just sounds reasonable. Six feels too much already. When there are more than five probably your class wants to do more than it should. Or, probably, the abstraction level of those dependencies is too low. Anyway, explicit dependencies give you a chance to make your design more solid.&lt;/li&gt;
&lt;li&gt;Don’t generalize too early, remember &lt;a href="https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)"&gt;the Rule of Three&lt;/a&gt;. First implement only some specific scenarios.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Here is a cross-posted version from my &lt;a href="https://medium.com/@wrong.about/static-classes-are-evil-or-make-your-dependencies-explicit-af3e73bd29dd"&gt;medium post&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>oop</category>
      <category>solid</category>
      <category>programming</category>
    </item>
    <item>
      <title>Implementation Inheritance Is Evil</title>
      <dc:creator>wrong-about-everything</dc:creator>
      <pubDate>Sun, 17 Dec 2017 12:44:35 +0000</pubDate>
      <link>https://dev.to/wrongabouteverything/implementation-inheritance-is-evil-3bb</link>
      <guid>https://dev.to/wrongabouteverything/implementation-inheritance-is-evil-3bb</guid>
      <description>

&lt;p&gt;A lot has been said about why &lt;a href="https://www.google.com/search?q=inheritance+is+evil"&gt;inheritance is bad&lt;/a&gt;. I’ll share my take on this subject and show that inheritance is not really needed most of the time.&lt;/p&gt;

&lt;h1&gt;
  
  
  Structural inheritance
&lt;/h1&gt;

&lt;h2&gt;
  
  
  It breaks encapsulation
&lt;/h2&gt;

&lt;p&gt;Inheritance for the sake of properties reuse is not the way that was meant by inheritance in OOP, since the early days of &lt;a href="https://en.wikipedia.org/wiki/Simula"&gt;Simula&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Smalltalk"&gt;Smalltalk&lt;/a&gt;. Such inheritance approach breaks the human metaphor. Nobody should have an access to my internals: stomach, brain, or lungs. It breaks object’s encapsulation, its fundamental feature. It breaks OOP.&lt;/p&gt;

&lt;h2&gt;
  
  
  It is &lt;a href="https://en.wikipedia.org/wiki/Fragile_base_class"&gt;fragile&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Behavior and internal structure are orthogonal concepts. They change independently. If some object extends internal structure of another one today, it doesn’t mean things will stay the same tomorrow.&lt;/p&gt;

&lt;h2&gt;
  
  
  It is &lt;a href="http://www.yegor256.com/2016/09/13/inheritance-is-procedural.html"&gt;procedural&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Inheritance based on internal structure inevitably promotes procedural outlook. Good objects just have no chance to survive in such an ill environment. They become nothing but a data structures.&lt;br&gt;
My personal experience with huge hierarchies reflects exactly this point. One of the projects I was engaged to had a huge amount of classes representing each request that the application could handle. The terrible thing was that those classes were full of business-logic. Since there were no real objects, reflecting domain, business-logic in those request-classes was more like procedures operating upon some data. And since that inheritance was motivated solely by request-fields reuse, it was very unnatural and unmaintainable.&lt;/p&gt;

&lt;h2&gt;
  
  
  It often feels artificial
&lt;/h2&gt;

&lt;p&gt;Entities resulted from applying inheritance in this procedural sense always seem artificial. They rarely reflect ubiquitous language, if ever. The reason is that any domain has natural &lt;a href="https://medium.com/@wrong.about/how-to-decompose-a-system-into-modules-796bd941f036"&gt;seams&lt;/a&gt;, marking off different responsibilities, behaviors — it is exactly what makes one object different from another. Objects internal data inherently can not be used to distinguish objects — just because it is internal.&lt;/p&gt;

&lt;h1&gt;
  
  
  Behavioral inheritance
&lt;/h1&gt;

&lt;p&gt;Inheritance for the sake of behavioral extension is the way to go. It’s like biology species taxonomy. It was built relying upon some mutual traits that all of that species share. For example, all mammals are vertebrates, endothermic and produce milk to feed their babies. Primate is &lt;em&gt;a kind of&lt;/em&gt; mammal, but have well developed hands and feet, with fingers and toes. Humans are &lt;em&gt;a kind of&lt;/em&gt; primate, but we have the ability to self-reflect.&lt;/p&gt;

&lt;p&gt;This is a metaphor used in &lt;a href="https://www.amazon.com/Object-Thinking-Developer-Reference-David/dp/0735619654"&gt;object thinking&lt;/a&gt; and it has an interesting consequence. It implies that &lt;strong&gt;concrete classes can not be inherited&lt;/strong&gt;. Keeping in mind biological taxonomy metaphor, there are no concrete mammal instances. There are no concrete primates. There are cats, dogs, humans. The real entities are the leafs of the hierarchy. All the rest is an abstract classes or interfaces. Hence what I’m in favor of is way closer to the concept of &lt;a href="https://en.wikipedia.org/wiki/Subtyping"&gt;subtyping&lt;/a&gt;, not inheritance.&lt;/p&gt;

&lt;h1&gt;
  
  
  Inheritance is rare
&lt;/h1&gt;

&lt;p&gt;Say we have a Merchant entity. It can be approved and expired. Typical approach is to make a “status” property. But very soon we realize that behavior differs a lot. Approved merchant can do pretty much anything that our system allows, unlike an expired one. Hence we come up with two separate classes — one for an approved merchant and one for an expired merchant. The first impel would be to make a base merchant class with all the internal data that they share — name, address, etc. But, firstly, as I wrote at the beginning, this is wrong motive, and secondly, none of object internal data has to be modeled (&lt;a href="https://medium.com/@wrong.about/you-dont-need-an-orm-7ef83bd1b37d"&gt;and often isn’t&lt;/a&gt;) as object’s properties. So this inheritance is wrong in both ways.&lt;/p&gt;

&lt;p&gt;Following behavioral object decomposition approach, it turns out that inheritance is not necessary in most of the time. Things that we used to consider to be related very closely — approved merchant and expired merchant, both being merchants — in reality do not share any behavior, so do not have mutual parent.&lt;/p&gt;

&lt;h1&gt;
  
  
  Inheritance hierarchies are small
&lt;/h1&gt;

&lt;p&gt;So following this decomposition approach there would not be huge inheritance cascades unless we design biology taxonomy. The number of decorators, on the contrary, is big.&lt;/p&gt;

&lt;p&gt;It’s all about decomposing your domain. When &lt;a href="https://hackernoon.com/on-good-domain-decomposition-385ee8ce5a3"&gt;done right&lt;/a&gt;, it comes out naturally. It’s far from being easy though.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrapping it up
&lt;/h1&gt;

&lt;p&gt;So do not extend concrete classes—only abstract. Decompose your problem space first, this results in small and rare inheritance hierarchies, with plenty of decorators.&lt;/p&gt;




&lt;p&gt;This is a cross-posted version of &lt;a href="https://medium.com/@wrong.about/inheritance-based-on-internal-structure-is-evil-7474cc8e64dc"&gt;this&lt;/a&gt; original post.&lt;/p&gt;

</description>
      <category>oop</category>
      <category>solid</category>
      <category>inheritance</category>
    </item>
  </channel>
</rss>
