public
class
Employee {
private
String firstName;
private
String lastName;
private
Employee(String firstName, String lastName) {
this
.firstName = firstName;
this
.lastName = lastName;
}
public
static
Employee valueOf (String firstName, String lastName) {
return
new
Employee(firstName, lastName);
}
Quote 4: Try to prefer Interfaces instead of Abstract classes
First
you can not inherit multiple classes in Java but you can definitely
implements multiple interfaces. Its very easy to change the
implementation of an existing class and add implementation of one more
interface rather then changing full hierarchy of class.
Again if
you are 100% sure what methods an interface will have, then only start
coding that interface. As it is very difficult to add a new method in an
existing interface without breaking the code that has already
implemented it. On contrary a new method can be easily added in Abstract
class without breaking existing functionality.
Quote 5: Always try to limit the scope of Local variable
Local
variables are great. But sometimes we may insert some bugs due to copy
paste of old code. Minimizing the scope of a local variable makes code
more readable, less error prone and also improves the maintainability of
the code.
Thus, declare a variable only when needed just before its use.
Always initialize a local variable upon its declaration. If not possible at least make the local instance assigned null
value.
0 comments:
Post a Comment