What is a Data Class
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.
Why do I love Data Classes?
I love data classes because they allow me to make pure data objects.
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.
Using Rich Data Models
The example below is for the authentication of a user in an app using rich data models.
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");
}
}
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");
}
This is classic, has both the functionality and the data in one class. I don't like this.
Using Pure Functions
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
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 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");
}
}
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");
}
I can authenticate at will, just need to pass in the user and I can authenticate.
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
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");
}
To add the functionalities above to the rich data models, it would make for insanely bloated code.
VERDICT???
LOVE!! LOVE!!! LOVE!!!
I hope I have convinced you to love data classes.
As I sign off I say:
My Opinions are my own as of today (might change tomorrow) and not the views of my employer (or mine by tomorrow).
Have a contrary opinion or you wanna give me a high five, you can @Trussdamola
Top comments (0)