<?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>Forem: Erik Pragt</title>
    <description>The latest articles on Forem by Erik Pragt (@bodiam).</description>
    <link>https://forem.com/bodiam</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%2F174445%2Fd42f0202-1181-4b25-b634-8d1f3a02303e.jpeg</url>
      <title>Forem: Erik Pragt</title>
      <link>https://forem.com/bodiam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bodiam"/>
    <language>en</language>
    <item>
      <title>Python Books for Beginners</title>
      <dc:creator>Erik Pragt</dc:creator>
      <pubDate>Sat, 22 Jun 2019 14:06:24 +0000</pubDate>
      <link>https://forem.com/bodiam/python-books-for-beginners-3il4</link>
      <guid>https://forem.com/bodiam/python-books-for-beginners-3il4</guid>
      <description>&lt;p&gt;I'm starting a new job soon, and for that, it's time to brush up my Python skills again. One of the best way for me to get back into a language is to read a book and try out some of the examples, so I've search for a bit, and composed this list. There's unfortunately quite some books out there with fake reviews, so I thought I'll share the result of my filtering with you. I’ll share a few of the best Python books for beginners, and one slightly more advanced. &lt;/p&gt;

&lt;p&gt;If you're completely new to Python; Python is an interpreted, object-oriented, high-level programming language and it’s relatively easy to learn.  So, where to start when you want to learn Python? Enjoy these books that will guide you through everything you need to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://amzn.to/2XpXsRv"&gt;Learning Python, 5th Edition&lt;/a&gt;&lt;br&gt;&lt;/strong&gt; Learning Python, the 5th Edition, is turning 6 years old this year, but it still offers you a comprehensive learning tool for Python. It covers a wide range of topics for programming in Python, such as Types &amp;amp; Operations, Statements and Syntax, Functions and Generators, Modules and Packages and much more. The first chapter is a great start, it’s a Q&amp;amp;A session about Python and why people use Python. If you’re a beginner, this can help you a lot. A nice feature of this book is that every chapter ends with a quiz, so you can verify yourself to see if you've understood the topics correctly.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/2RkGChA"&gt;&lt;strong&gt;Python Crash Course, second edition: A Hands-On, Project-Based Introduction to Programming&lt;br&gt;&lt;/strong&gt;&lt;/a&gt;An interesting book for the absolute beginner, which has just been updated to a new release! This book gets great reviews but it assumes that this is one of your first coding book. If it's not, then maybe Learning Python is a better book, but if this is your first coding book, the author will take you through a few interesting projects to get your hands dirty and learn coding by doing!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://amzn.to/2XhsGKH"&gt;Think Python. How to Think Like a Computer Scientist&lt;/a&gt;&lt;br&gt;&lt;/strong&gt;&lt;a href="https://amzn.to/2XhsGKH"&gt; &lt;/a&gt;This is a great book if you’re new to programming or coming from another language. Each chapter ends with debugging tips, a glossary and some exercises to keep you going. This really is a hands-on guide that will teach you Python one step at the time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://amzn.to/2Rkvz80"&gt;Beginning Python: From Novice to Professional, 2nd edition &lt;br&gt;&lt;/a&gt;&lt;/strong&gt;This book gives you a great overview and covers the Python fundamentals in-depth. A really like that the first part of the book guides you through a series of tasks including working with files, databases, web programming and testing. The last ten chapters consist of ten programming projects: ‘one must program in order to learn to program’. The projects include: automated text markup, remote text editor, file sharing with XML-RPC, a ‘do-it-yourself’ arcade game and much more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://amzn.to/2Knxubf"&gt;Learn Python 3 the hard way&lt;/a&gt;&lt;br&gt;&lt;/strong&gt;Some people love it, some people dislike it, there seems to be no middle ground. Learn Code the Hard Way is offered as free PDFs. Other books in the series cover Python, Ruby, C, SQL or Regex ‘the hard way’. These books are written by Zed Shaw and will give you more insight in how to get started with any of the above topics. Learn Python the Hard Way is written for beginners who know nothing about programming and it will teach you the basics of programming. You will learn how to use the terminal and the text editor. By trying out the examples, this book is focusing about learning by doing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://amzn.to/2RnA1mD"&gt;Python Pocket Reference (Pocket Reference (O’Reilly))&lt;/a&gt;&lt;br&gt;&lt;/strong&gt;If you’re travelling a lot, this book is right for you! A convenient pocket guide worth keeping close at hand. This reference covers both Python 3.4 and 2.7. While it's getting a bit of an oldie, it’s accurate, easy to understand and very well-written. If you’re on your way and aren’t able to go trough Python’s documentation online, this pocket-book comes in quite handy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://amzn.to/2XhpFtO"&gt;Natural Language Processing in Action: Understanding, analyzing, and generating text with Python&lt;br&gt;&lt;/a&gt;&lt;/strong&gt;If you're a complete beginner with Python, this is probably not the best book to start with, but NLP is a great topic, and the book gets a lot of raving reviews, which for once haven't been written by a bot. Definitely a book to look into to if you're into Natural Language Processing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course there are many more Python books, so if you have suggestions about which books are really worth mentioning, please let me know!&lt;/p&gt;

</description>
      <category>python</category>
      <category>books</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Immutable data structures in Java</title>
      <dc:creator>Erik Pragt</dc:creator>
      <pubDate>Sat, 15 Jun 2019 07:35:18 +0000</pubDate>
      <link>https://forem.com/bodiam/immutable-data-structures-in-java-34a1</link>
      <guid>https://forem.com/bodiam/immutable-data-structures-in-java-34a1</guid>
      <description>&lt;p&gt;As part of some of the coding interviews I’ve been conducting recently, the topic of immutable data structures comes up regularly. I’m not overly dogmatic in it myself, but whenever there’s no need for mutable state, I try to get rid of code which makes code mutable, which is often most visible in data structures. However, there seems to be a bit of a misunderstanding on the concept of immutability, where developers often believe that having a &lt;code&gt;final&lt;/code&gt; reference, or &lt;code&gt;val&lt;/code&gt; in Kotlin or Scala, is enough to make an object immutable. This blogpost dives a bit deeper in immutable references and immutable data structures. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of immutable data structures&lt;/strong&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Immutable data structures have some significant benefits, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No invalid state&lt;/li&gt;
&lt;li&gt;Thread safety&lt;/li&gt;
&lt;li&gt;Easier to understand code&lt;/li&gt;
&lt;li&gt;Easier to test&lt;/li&gt;
&lt;li&gt;Can be used for value types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;No invalid state&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When an object is immutable, it’s hard to have the object in an invalid state. The object can only be instantiated through its constructor, which will enforce the validity of objects. This way, the required parameters for a valid state can be enforced. An example:&lt;br&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;Address address = new Address();
address.setCity("Sydney");
// address is in invalid state now, since the country hasn’t been set.

Address address = new Address("Sydney", "Australia");
// Address is valid and doesn’t have setters, so the address object is always valid.&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Thread safety&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since the object cannot be changed, it can be shared between threads without having race conditions or data mutation issues.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Easier to understand code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Similar to code example in the invalid state, it’s generally easier to use a constructor than using initialisation methods. This is because the constructor enforces the required arguments, while setter or initialiser methods are not enforced at compile time. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Easier to test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since objects are more predictable, it’s not necessary to test all permutations of the initialiser methods; i.e. when calling the constructor of a class, the object is either valid or invalid. Other parts of the code which are using these classes become more predictable, having fewer chances of NullPointerExceptions. Sometimes, when passing objects around, there are methods which potentially mutate the state of the object. For example:&lt;br&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;public boolean isOverseas(Address address) {
    if(address.getCountry().equals("Australia") == false) {
        address.setOverseas(true); // address has now been mutated!
        return true;
    } else {
        return false;
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above code, in general, is bad practice. It returns a boolean as well as potentially changing the state of the object. This makes the code harder to understand and to test. A better solution would be to remove the setter from the Address class, and return a boolean by testing for the country name. An even better way would be to move this logic to the Address class itself (address.isOverseas()). When state really needs to be set, make a copy of the original object without mutating the input. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can be used for value types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a money amount, say 10 dollars. 10 Dollars will always be 10 dollars. In code, this could look like public Money(final BigInteger amount, final Currency currency). As you can see in this code, it’s not possible to change the value of 10 dollars to anything other than that, and thus the above can be used safely for value types.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final references don't make objects immutable&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As mentioned before, one of the issues I regularly encounter with developers is that a large portion of these developers don’t fully understand the difference between final references and immutable objects. It seems that the common understanding of these developers is that the moment a variable becomes final, the data structure becomes immutable. Unfortunately, it’s not that simple, and I’d like to get this misunderstanding out of the world once and for all:&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt; A final reference does not make your objects immutable!&lt;/em&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;In other words, the following code does &lt;strong&gt;not&lt;/strong&gt; make your objects immutable:&lt;br&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;final Person person = new Person("John");&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Why not? Well, while `person` is a final field, and cannot be reassigned, the Person class might have a setter method or other mutator methods, making an action like:&lt;br&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;person.setName("Cindy");&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Quite an easy thing to do, regardless of the final modifier. Alternatively, the Person class might expose a list of addresses like this. Accessing this list allows you to add an address to it, and therefore mutates the person object like this:&lt;br&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;person.getAddresses().add(new Address("Sydney"));&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Our final reference again didn’t help us in stopping us from mutating the person object.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Okay, now we’ve got that out the way, let’s dive a little bit into how we can make a class immutable. There are a couple of things we need to keep in mind while designing our classes:&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don’t expose internal state in an mutable way&lt;/li&gt;
&lt;li&gt;Don’t change the state internally&lt;/li&gt;
&lt;li&gt;Make sure subclasses don’t override the above behaviour&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the following guidelines in place, let’s design a better version of our Person class. &lt;br&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;public final class Person {  // final class, can’t be overridden by subclasses
    private final String name;     // final for safe publication in multithreaded applications
    private final List&amp;lt;Address&amp;gt; addresses;

    public Person(String name, List&amp;lt;Address&amp;gt; addresses) {
        this.name = name;
        this.addresses = List.copyOf(addresses);   // makes a copy of the list to protect from outside mutations (Java 10+). 
                // Otherwise, use Collections.unmodifiableList(new ArrayList&amp;lt;&amp;gt;(addresses));

    }

    public String getName() {
        return this.name;   // String is immutable, okay to expose
    }

    public List&amp;lt;Address&amp;gt; getAddresses() {
        return addresses; // Address list is immutable
    }
}

public final class Address {    // final class, can’t be overridden by subclasses
    private final String city;          // only immutable classes
    private final String country;

    public Address(String city, String country) {
        this.city = city;
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public String getCountry() {
        return country;
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, the following code can be used like this:&lt;br&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;import java.util.List;
final Person person = new Person("John", List.of(new Address(“Sydney”, "Australia"));&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, the above code is but immutable due to the design of the Person and Address class, while also having a final reference, making it impossible to reassign the person variable to anything else. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update: &lt;/strong&gt;&lt;em&gt;As &lt;a href="https://www.reddit.com/r/java/comments/azryu6/final_vs_immutable_data_structures_in_java/?st=jt74o32w&amp;amp;sh=40d418d3"&gt;some people&lt;/a&gt; mentioned, the above code was still mutable because I didn't make a copy of the list of Addresses in the constructor. So, without calling the new ArrayList() in the constructor, it's still possible  to do the following:&lt;/em&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code class="language-java"&gt;final List&amp;lt;Address&amp;gt; addresses = new ArrayList&amp;lt;&amp;gt;();
addresses.add(new Address("Sydney", "Australia"));
final Person person = new Person("John", addressList);
addresses.clear();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;However, since a now a copy is made in the constructor, the above code will no longer affect the copied address list reference in the Person class, making the code safe. Thanks all for spotting!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I hope the above helps in understanding the differences between final and immutability. If you have any comments or feedback, please let me know in the comments below.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Again, much thanks to my colleague Winston for taking the time to proofread and review this blogpost!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>immutable</category>
      <category>datastructures</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
