[PROGRAMMING] Better value objects and readable code

Jepzter

Member
May 25, 2018
38
37
I want to bring my experience to the forum and talk about how we write clean and descriptive code. The code should not be a mess with comments that doesn't really tell anything more than a method name could do, we do instead write meaningful method names. We should also focus on making the code we write easy to use, this can also be done by using custom instance creator of a class. I will go through some examples of bad code and good and bad examples. This is my opinion but I really think this will make you think about how you should write it.

Objects with data is often used with getters and setters, we want to eliminate setters as much as we can, the only reason for have a setter will be for updating the current data of a property. We instead want to make it so that you only can set values through the constructor, this is because we want to limit the possibility to modify the instance of that object.

Bad example
Code:
public class User {

    private UUID userId;
    private String username;

    public UUID getUserId() {
        return userId;
    }

    public void setUserId(UUID userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

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

Good example
Code:
public class User {


        private UUID userId;
        private String username;

        public User(UUID userId, String username) {
            this.userId = userId;
            this.username = username;
        }
    
        public UUID getUserId() {
            return userId;
        }

        public String getUsername() {
            return username;
        }
    }


Now we have a secure way of setting data, and while this is a good way to use a object with only values, we want a more readable way of creating the object. With this code we create an instance like this,

Code:
User user = new User(UUID.randomUUID(), "TheOvster");

this is a bad example when you want readability, we don't say what we want to do with this object other than putting our data in, so what do we add to our object to make the creation of this object easier and more readable?

Code:
public class User {

    private UUID userId;
    private String username;

    public static User create(UUID userId, String username) {
        return new User(userId, username);
    }

    private User(UUID userId, String username) {
        this.userId = userId;
        this.username = username;
    }

    public UUID getUserId() {
        return userId;
    }

    public String getUsername() {
        return username;
    }
}

now when we create the instance of this object with data, we write the code like this

Code:
User user = User.create(UUID.randomUUID(), "TheOvster");

Now, we bring more readable code and a meaning to what we do with the object.

Add at least one more method to the object that is pretty equal to the above one for example this method is very useful aswell for creating a new instance of a current User instance.

Code:
public static User from(User user) {
    return new User(user);
}

private User(User user) {
    this.userId = user.getUserId();
    this.username = user.getUsername();
}

The method takes an already existing instance and create a new one, this is useful in some cases, most of the time SQL depending on how you application map properties to an object.
This method could be used like this,

Code:
User user = User.from(User.create(UUID.randomUUID(), "TheOvster");

The finished User class would look like this,

Code:
class User {

    private UUID userId;
    private String username;

    public static User from(User user) {
        return new User(user);
    }

    public static User create(UUID userId, String username) {
        return new User(userId, username);
    }

    private User(User user) {
        this.userId = user.getUserId();
        this.username = getUsername();
    }

    private User(UUID userId, String username) {
        this.userId = userId;
        this.username = username;
    }

    public UUID getUserId() {
        return userId;
    }

    public String getUsername() {
        return username;
    }
}

I hope you found this meaningful for you in the creation of your awesome application. I will go through other things about this topic that will help you creating better application for you and others that work in the same project. Comment for any concerns or your thoughts that might be even better! :)
 
Last edited:

Users who are viewing this thread

Top