<?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: Adedoja Adedamola</title>
    <description>The latest articles on DEV Community by Adedoja Adedamola (@trussdamola).</description>
    <link>https://dev.to/trussdamola</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%2F228883%2F3026b064-c946-4b35-86ec-8f32ab7f43dc.jpg</url>
      <title>DEV Community: Adedoja Adedamola</title>
      <link>https://dev.to/trussdamola</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/trussdamola"/>
    <language>en</language>
    <item>
      <title>HateItOrLoveIt: Data classes?</title>
      <dc:creator>Adedoja Adedamola</dc:creator>
      <pubDate>Mon, 27 Jul 2020 07:06:30 +0000</pubDate>
      <link>https://dev.to/trussdamola/hateitorloveit-data-classes-5bob</link>
      <guid>https://dev.to/trussdamola/hateitorloveit-data-classes-5bob</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;What is a Data Class&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;A data class is a class that is developed basically to hold data for use by other classes. They cannot operate independently without other classes. They differ across programming languages, but the concept stays the same. Examples here are written in Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why do I love Data Classes?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I love data classes because they allow me to make pure data objects.&lt;/p&gt;

&lt;p&gt;This is contrary to the age-old Object-Oriented wisdom that advocates the use of rich data models, data models that combines both data and functionality.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Using Rich Data Models&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The example below is for the authentication of a user in an app using rich data models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class User {
    private String username;
    private String password;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserame() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    public boolean login() {
        //whatever logic you wanna use here
        //all this checks if username is "nightking91"
        return (this.username == "nightking91");
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 public static void main(String[] args) throws Exception {
        User user = new User();
        user.setPassword("password");
        user.setUsername("username");

        boolean isLoggedIn  = user.login();

        System.out.println(isLoggedIn ? "We logged In" : "We out here");
    }

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is classic, has both the functionality and the data in one class. I don't like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/d10dMmzqCYqQ0/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/d10dMmzqCYqQ0/source.gif" alt="Noooo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Using Pure Functions&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;I prefer pure functions, combined with immutable data-objects from a data class. I feel its a cleaner way to code and aligns a lot more with the SOLID principles we all love and love so much. Take a look at the example below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class User {
    private String username;
    private String password;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserame() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Authenticator
{
    public boolean login(User user) {
        //whatever logic you wanna do with the user and you return true or false
        //all this checks if username is "nightking91"
        return (user.getUsername() == "nightking91");
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public static void main(String[] args) throws Exception {
        User user = new User();
        user.setPassword("password");
        user.setUsername("username");

        Authenticator authenticator = new Authenticator();
        boolean isLoggedIn  = authenticator.login(user);

        System.out.println(isLoggedIn ? "We logged In" : "We out here");
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/l3q2LH45XElELRzRm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l3q2LH45XElELRzRm/giphy.gif" alt="Yesss"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I can authenticate at will, just need to pass in the user and I can authenticate.&lt;/p&gt;

&lt;p&gt;Having pure data objects allows adding functionalities like Modeling relationships, Validation, and Controlling Access to the user in an easy and straight forward manner that would not make the code smell&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public static void main(String[] args) throws Exception {
        User user = new User();
        user.setPassword("password");
        user.setUsername("username");

        Validator validator = new Validator();
        boolean isValid = validator.validate(user);

        System.out.println(isValid ? "We valid" : "We not valid");
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To add the functionalities above to the rich data models, it would make for insanely bloated code. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;VERDICT???&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;LOVE!! LOVE!!! LOVE!!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/S5nrWUivHLujPk94Ix/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/S5nrWUivHLujPk94Ix/giphy.gif" alt="Verdict"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope I have convinced you to love data classes.&lt;/p&gt;

&lt;p&gt;As I sign off I say:&lt;/p&gt;

&lt;p&gt;My Opinions are my own as of today (&lt;strong&gt;might change tomorrow&lt;/strong&gt;) and not the views of my employer (&lt;strong&gt;or mine by tomorrow&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Have a contrary opinion or you wanna give me a high five, you can &lt;a href="https://twitter.com/trussdamola"&gt;@Trussdamola&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>programming</category>
    </item>
    <item>
      <title>HateItOrLoveIt: Traits?</title>
      <dc:creator>Adedoja Adedamola</dc:creator>
      <pubDate>Thu, 13 Feb 2020 12:05:00 +0000</pubDate>
      <link>https://dev.to/trussdamola/hateitorloveit-traits-dpd</link>
      <guid>https://dev.to/trussdamola/hateitorloveit-traits-dpd</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;What are traits?&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/MBf9sZw727SJzsN9z5/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/MBf9sZw727SJzsN9z5/giphy.gif" alt="What are traits?"&gt;&lt;/a&gt;&lt;br&gt;
My Team Leader, the SALK once described it as a Horizontal Inheritance with this simple analogy, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All Birds have feathers, but not all birds can fly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this analogy, flight is a trait. All birds inherit the characteristic to have feathers, but only some can actually fly&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://wiki.php.net/rfc/horizontalreuse"&gt;Horizontal Reuse for PHP RFC&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In English, Traits simply allow for the reuse of methods, If there is a need to share functionality across multiple classes, you use traits. Using traits, methods can be reused freely across several different independent classes regardless of their class hierarchies.&lt;/p&gt;

&lt;p&gt;Traits are similar to mixins in other languages like Ruby and JS.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Traits Use Case&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Just like the analogy up top. We have classes Parrot and Ostrich that both inherit from the Class Bird. Both Class Parrot and Ostrich have feathers inherited from the Class Bird, but only Parrot can fly because it has the trait Flight.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Bird
{
    private $type;

    public function __construct(string $type)
    {
        $this-&amp;gt;type = $type;
    }

    public function feathers(): string
    {
        return "I am a " . $this-&amp;gt;type . " and i have feathers";
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trait Flight
{
    public function fly(): string
    {
        return " - I can fly too";
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Parrot extends Bird
{
    use Flight;

    public function __construct()
    {
        parent::__construct('Parrot');
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Ostrich extends Bird
{
    public function __construct()
    {
        parent::__construct('Ostrich');
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
$skiTheOstrich = new Ostrich();
echo $skiTheOstrich-&amp;gt;feathers(); //returns: I am a Ostrich and i have feathers

$johnTheParrot = new Parrot();
echo $johnTheParrot-&amp;gt;feathers();
echo $johnTheParrot-&amp;gt;fly(); //returns: I am a Parrot and i have feathers - I can fly too

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Advantages of Traits&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Allows for code reuse&lt;/li&gt;
&lt;li&gt;Unrestricted by inheritance hierarchies&lt;/li&gt;
&lt;li&gt;A class can have multiple traits&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Disadvantages of Traits&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Encourages bloated classes&lt;/li&gt;
&lt;li&gt;Simply a “copy and paste” for code between classes.&lt;/li&gt;
&lt;li&gt;A crutch for lazy programming&lt;/li&gt;
&lt;li&gt;An entire class’ abilities are NOT in one location visually.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;VERDICT???&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Personally, I do not like traits, why? you ask.&lt;br&gt;
I am a stickler for traditional software design principles and Trait allows for multiple Inheritance and I personally favor &lt;strong&gt;Composition over Inheritance&lt;/strong&gt;. For me, a lot of trait in code is a sign of code smell.&lt;/p&gt;

&lt;p&gt;Using traits, it is difficult to follow the code, because it has this functionality somewhere that you don't know how the class implements it and then you spy a "use XYZ" somewhere up and you are like haha!&lt;/p&gt;

&lt;p&gt;I also believe its a bit weird to test. So for these reasons, even though I believe they have their own use cases!!! &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;I HATE&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/kc0Ahsk9bUDzrVP73i/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kc0Ahsk9bUDzrVP73i/giphy.gif" alt="Verdict"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I sign off I say:&lt;/p&gt;

&lt;p&gt;My Opinions are my own as of today (&lt;strong&gt;might change tomorrow&lt;/strong&gt;) and not the views of my employer (&lt;strong&gt;or mine by tomorrow&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Have a contrary opinion or you wanna give me a high five, you can &lt;a href="https://twitter.com/trussdamola"&gt;@Trussdamola&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>oop</category>
      <category>traits</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
