What makes a great software developer? Simple, anyone who has the ability to create and maintain quality software in a team environment are in high demand in today's technology-driven economy. The number one challenge facing developers working in a team environment is reading and understanding software written by another developer. Java.Net has identified 5 habits that can be used by developers to make them more efficient at producing and maintaining quality software:
Habit 1: Constructor Performs Minimal Work
The first habit is for an object's constructor to do as little work as possible. A constructor should only load data into its instance variables using the constructor's parameters. This makes the object easier to use and understand because the constructor performs only the simple task of loading data into the object's instance variables, as displayed below:
public class CustomerAccount implements ICustomerAccount{
//Instance variables.
private String username;
private String password;
protected String accountStatus;
//Constructor that performs minimal work.
public CustomerAccount(String username, String password) {
this.password = password;
this.username = username;
}
}
Objects with constructors that do more than just load instance variables are harder to understand and more likely to be misused. For example, creating a constructor that also calls a method called configureObject() that configures the object may initially seem like a good idea. However, another developer could use this constructor to create an object and since they are not aware that by default it calls configureObject() to configure the object, they call configureObject() after initializing it, essentially making an unnecessary call.
Habit 2: Methods Clearly Convey Their Intent
The second habit is for all methods to clearly convey their intent through the names they are given. For example, isRequestedUsernameValid() lets the developer know that this method determines whether or not the requested username is valid. In contrast, isGoodUser() can have any number of uses: it can determine if the user's account is active, determine if the requested username and/or password are valid, or determine if the user is a nice person.
public class CustomerAccountTest extends TestCase{
public void testRequestedPasswordIsNotValid
BecauseItMustBeDifferentThanTheUsername(){
String username = "robertmiller";
String password = "robertmiller";
ICustomerAccount ca = new CustomerAccount(username, password);
assertFalse(ca.isRequestedPasswordValid());
}
}
Since this method is less descriptive, it is more difficult for a
developer to figure out what its purpose is. In short, it is better for
the method names to be long and descriptive than to be short and
meaningless, as displayed above.
Habit 3: An Object Performs a Focused Set of Services
The third habit is for each object in the software to be focused on performing a small and unique set of services. Objects that perform a small amount of work are easier to read and more likely to be used correctly because there is less code to digest. Moreover, each object in the software should perform a unique set of services because duplicating logic wastes development time and increases maintenance costs.
public interface ICustomerAccount {
//State-changing methods
public void createNewActiveAccount()
throws CustomerAccountsSystemOutageException;
public void loadAccountStatus()
throws CustomerAccountsSystemOutageException;
public void createPurchaseRecordForProduct(Long productId)
throws CustomerAccountsSystemOutageException;
public void loadAllPurchaseRecords()
throws CustomerAccountsSystemOutageException;
//Behavior methods
public boolean isRequestedUsernameValid();
public boolean isRequestedPasswordValid();
public boolean isActiveForPurchasing();
public String getPostLogonMessage();
public void isCustomerEligibleForDiscount();
}
Habit 4: State-Changing Methods Contain Minimal Behavior Logic
Intermixing state-changing logic with behavior logic makes the software more difficult to understand because it increases the amount of work happening in one place.
Habit 5: Behavior Methods Can Be Called in Any Order
Ensure that each behavior method provides value independent of any other behavior method, meaning that an object's behavior methods can be called repeatedly and in any order. This habit allows the object to deliver consistent behavior. For example, CustomerAccount's isActiveForPurchasing() and getPostLogonMessage() behavior methods displayed above both use the accountStatus's value in their logic. Each of these methods should be able to function independently of the other.
Software design methodologies do play an important part in software reuse. Some methodologies enhance software reuse while others discourage it, but software should be written so that it can easily be modified. Regardless of which methodology is adopted by a development team, following these five habits will help create software that everyone on the team can read, understand, and modify.