There are many ways to implement functionality in Java. The most approved way in many environments is the OOP one. Because of that some part of your code will be taken as a pure evil. Today I would like to break some myths about Java and Object-Oriented Programming. Let's begin.
![]() |
| Image from: http://www.h3dwallpapers.com/ |
1. Java == OOP
Many developers seem to believe that when you code in Java, you must write an Object-Oriented code. Any other solutions are rejected at the beginning. That's true - Java is a strongly OOP language, but Object-Oriented Programming is just one of the paradigms. Currently Java supports, for example functional programming (by Lambdas), as many others object-oriented languages did some time ago. There are no good and bad programming paradigms. Choosing the right one, depends on your current and future needs. No more, no less.
2. Utility classes are evil
Because of their procedural character in many communities they are a pure evil. I saw many "alternative" object-oriented solutions, but if someone (like Yegor Bugayenko in this post) believes that this:
Integer max = new MaxValue(10, 5).intValue();
Is somehow better than:
Integer max = Math.max(10, 5);
Then I would say: "You're going too far in your object-oriented madness my friend". Java allows a procedural code, and if you're coding in Java - code in Java, OOP is not a language (I just don't believe I'm saying that, however madness makes chaos, and chaos is the last thing we want in our code).
3. Setters and getters are evil
I found these opinions many times in network, the last one here. In my opinion JavaBeans shouldn't be grouped to objects. They are simple data structures with some API convention. I don't agree with an idea that JavaBeans are against YAGNI (You aren't going to need it - a principle from Extreme Programming). Data structures, like any other part of some API are the kind of templates. If someone tells you that this setter is not needed, then it's a good moment to think if the field corresponding to this setter is needed at all. In most cases it's something similar to this:
As you can see editable value is unneeded, because it's based on the first field. More correct version:
Considering a change name of the getEditable() method it's also a good practice. In any other case, setters and getters should be provided by our API. If they are not, then for the other programmer it's like buying a new car without brake pedal (you don't need it now - it's far away from the nearest traffic lights). So once again: don't take a JavaBean like an Object. Take it as some kind of bridge between data structure and object-oriented world. JavaBeans in most cases are just an alternative for a standard public fields.
Conclusion:
A good code, no matter if it's procedural, functional or object-oriented will stay a good code. It will be readable, clear and easy to use. Java gives us many opportunities to solve problems in many different way. In most cases simple solution is the best one. Be kind, and make sure, that you are not going the most painful way because of the ideology.
And what about your code? Isn't it too serious...?
public class SomeBean {
private String someTextField;
private Boolean editable;
public SomeBean() {
// Nothing to do, you're JB.
}
public SomeBean(final String someTextField) {
this.someTextField = someTextField;
editable = someTextField.startsWith("E_");
}
public String getSomeTextField() {
return someTextField;
}
public void setSomeTextField(final String someTextField) {
this.someTextField = someTextField;
}
public Boolean getEditable() {
return editable;
}
public void setEditable(final Boolean editable) {
this.editable = editable;
}
}
As you can see editable value is unneeded, because it's based on the first field. More correct version:
public class SomeBean {
private String someTextField;
public SomeBean() {
// Nothing to do, you're JB.
}
public SomeBean(final String someTextField) {
this.someTextField = someTextField;
}
public String getSomeTextField() {
return someTextField;
}
public void setSomeTextField(final String someTextField) {
this.someTextField = someTextField;
}
public Boolean getEditable() {
return someTextField != null && someTextField.startsWith("E_");
}
}
Considering a change name of the getEditable() method it's also a good practice. In any other case, setters and getters should be provided by our API. If they are not, then for the other programmer it's like buying a new car without brake pedal (you don't need it now - it's far away from the nearest traffic lights). So once again: don't take a JavaBean like an Object. Take it as some kind of bridge between data structure and object-oriented world. JavaBeans in most cases are just an alternative for a standard public fields.
Conclusion:
A good code, no matter if it's procedural, functional or object-oriented will stay a good code. It will be readable, clear and easy to use. Java gives us many opportunities to solve problems in many different way. In most cases simple solution is the best one. Be kind, and make sure, that you are not going the most painful way because of the ideology.
And what about your code? Isn't it too serious...?







0 komentarze:
Post a Comment