Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
[PROGRAMMING] Better value objects and readable code
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Jepzter" data-source="post: 434654" data-attributes="member: 81100"><p>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 <strong>easy </strong>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.</p><p></p><p>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.</p><p></p><p><strong><span style="font-size: 22px">Bad example</span></strong></p><p>[CODE]</p><p>public class User {</p><p></p><p> private UUID userId;</p><p> private String username;</p><p></p><p> public UUID getUserId() {</p><p> return userId;</p><p> }</p><p></p><p> public void setUserId(UUID userId) {</p><p> this.userId = userId;</p><p> }</p><p></p><p> public String getUsername() {</p><p> return username;</p><p> }</p><p></p><p> public void setUsername(String username) {</p><p> this.username = username;</p><p> }</p><p>}</p><p></p><p>[/CODE]</p><p></p><p><span style="font-size: 22px"><strong>Good example</strong></span></p><p><span style="font-size: 15px">[CODE]public class User {</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> private UUID userId;</span></p><p><span style="font-size: 15px"> private String username;</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> public User(UUID userId, String username) {</span></p><p><span style="font-size: 15px"> this.userId = userId;</span></p><p><span style="font-size: 15px"> this.username = username;</span></p><p><span style="font-size: 15px"> }</span></p><p><span style="font-size: 15px"> </span></p><p><span style="font-size: 15px"> public UUID getUserId() {</span></p><p><span style="font-size: 15px"> return userId;</span></p><p><span style="font-size: 15px"> }</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> public String getUsername() {</span></p><p><span style="font-size: 15px"> return username;</span></p><p><span style="font-size: 15px"> }</span></p><p><span style="font-size: 15px"> }[/CODE]</span></p><p></p><p></p><p><span style="font-size: 15px">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, </span></p><p></p><p><span style="font-size: 15px">[CODE]User user = new User(UUID.randomUUID(), "TheOvster"); [/CODE]</span></p><p></p><p><span style="font-size: 15px">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?</span></p><p></p><p><span style="font-size: 18px">[CODE]public class User {</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> private UUID userId;</span></p><p><span style="font-size: 18px"> private String username;</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public static User create(UUID userId, String username) {</span></p><p><span style="font-size: 18px"> return new User(userId, username);</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> private User(UUID userId, String username) {</span></p><p><span style="font-size: 18px"> this.userId = userId;</span></p><p><span style="font-size: 18px"> this.username = username;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public UUID getUserId() {</span></p><p><span style="font-size: 18px"> return userId;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public String getUsername() {</span></p><p><span style="font-size: 18px"> return username;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px">}[/CODE]</span></p><p></p><p><span style="font-size: 15px">now when we create the instance of this object with data, we write the code like this</span></p><p></p><p><span style="font-size: 15px">[CODE]User user = User.create(UUID.randomUUID(), "TheOvster");[/CODE]</span></p><p></p><p><span style="font-size: 15px">Now, we bring more readable code and a meaning to what we do with the object. </span></p><p></p><p><span style="font-size: 15px">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. </span></p><p></p><p><span style="font-size: 18px">[CODE] </span></p><p><span style="font-size: 18px">public static User from(User user) {</span></p><p><span style="font-size: 18px"> return new User(user);</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">private User(User user) {</span></p><p><span style="font-size: 18px"> this.userId = user.getUserId();</span></p><p><span style="font-size: 18px"> this.username = user.getUsername();</span></p><p><span style="font-size: 18px">}[/CODE]</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">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. </span></p><p><span style="font-size: 18px">This method could be used like this, </span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">[CODE]</span></p><p><span style="font-size: 18px">User user = User.from(User.create(UUID.randomUUID(), "TheOvster");</span></p><p><span style="font-size: 18px">[/CODE]</span></p><p></p><p><span style="font-size: 15px">The finished User class would look like this,</span></p><p></p><p><span style="font-size: 18px">[CODE]class User {</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> private UUID userId;</span></p><p><span style="font-size: 18px"> private String username;</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public static User from(User user) {</span></p><p><span style="font-size: 18px"> return new User(user);</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public static User create(UUID userId, String username) {</span></p><p><span style="font-size: 18px"> return new User(userId, username);</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> private User(User user) {</span></p><p><span style="font-size: 18px"> this.userId = user.getUserId();</span></p><p><span style="font-size: 18px"> this.username = getUsername();</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> private User(UUID userId, String username) {</span></p><p><span style="font-size: 18px"> this.userId = userId;</span></p><p><span style="font-size: 18px"> this.username = username;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public UUID getUserId() {</span></p><p><span style="font-size: 18px"> return userId;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"> public String getUsername() {</span></p><p><span style="font-size: 18px"> return username;</span></p><p><span style="font-size: 18px"> }</span></p><p><span style="font-size: 18px">}</span></p><p><span style="font-size: 18px">[/CODE]</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">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! <img src="/styles/default/xenforo/smilies/emojione/smile.png" class="smilie" loading="lazy" alt=":)" title="Smile :)" data-shortname=":)" /></span></p></blockquote><p></p>
[QUOTE="Jepzter, post: 434654, member: 81100"] 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 [B]easy [/B]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. [B][SIZE=6]Bad example[/SIZE][/B] [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; } } [/CODE] [SIZE=6][B]Good example[/B][/SIZE] [SIZE=4][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; } }[/CODE][/SIZE] [SIZE=4]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, [/SIZE] [SIZE=4][CODE]User user = new User(UUID.randomUUID(), "TheOvster"); [/CODE][/SIZE] [SIZE=4]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?[/SIZE] [SIZE=5][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; } }[/CODE][/SIZE] [SIZE=4]now when we create the instance of this object with data, we write the code like this[/SIZE] [SIZE=4][CODE]User user = User.create(UUID.randomUUID(), "TheOvster");[/CODE][/SIZE] [SIZE=4]Now, we bring more readable code and a meaning to what we do with the object. [/SIZE] [SIZE=4]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. [/SIZE] [SIZE=5][CODE] public static User from(User user) { return new User(user); } private User(User user) { this.userId = user.getUserId(); this.username = user.getUsername(); }[/CODE] 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"); [/CODE][/SIZE] [SIZE=4]The finished User class would look like this,[/SIZE] [SIZE=5][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; } } [/CODE] 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! :)[/SIZE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
[PROGRAMMING] Better value objects and readable code
Top