Writing Better Code a few suggestions 1 Code

Writing Better Code a few suggestions

1. Code should be easy to read Write code that is easy to read Write comments! Use meaningful variable names Follow a formatting convention public Coin[] withdraw(int x) { int t=Math. min(x/10, ten); x -= t*10; int f=Math. min(x/5, five); x -= f*5; int o=Math. min(o, one); x -= o*1; if (x==0) return get. Coins(t, f, o); else return null; }

2. Localize variables Use the smallest possible scope use attributes only for things that are part of object's state minimize sharing of information between methods public class Coin. Machine { List<Coin> items; int balance; public int get. Balance( ) { balance = 0; for(Coin c: items) balance += c. get. Value(); return balance; }

Localize variables balance is only needed in get. Balance, so it should be local. won't fail if 2 threads call get. Balance() simultaneously public class Coin. Machine { List<Coin> items; public int get. Balance( ) { int balance = 0; for(Coin c: items) balance += c. get. Value(); return balance; }

3. Strive to reduce coupling Classes are coupled if: a class depends on objects from another class a class invokes methods from another class a class's method has a parameter of type other class Bank ATM Manager ATM Keypad ATM Display Database

4. Use interfaces to specify behavior "Program to an interface, not an implementation" reduces coupling and promotes re-usability // a course contains a list of students class Purse { List is an interface private List items; . . . Array. List is a kind of list items = new Array. List( );

5. Avoid Duplicate Code or Logic Don't Repeat Yourself! (DRY) eliminate duplicate code – introduce a method, interface, or redesign code avoid duplicate logic, too

Example of Duplicate Logic The insert( ) method can express its intention and avoid duplicate logic by calling is. Full() instead. // simple example of duplicate logic class Coin. Machine { final int capacity = 100; public boolean is. Full() { return items. size() >= capacity; } /** insert coin into machine */ public boolean insert(Coin coin) { if (items. size() >= capacity) return false; return list. append(coin);

5. Limit visibility of members Limit the visibility of methods and attributes only required behavior and constants are public attributes are private, with public accessor methods write mutator ("set") methods only when necessary be suspicious of protected attributes, especially in the default package

Limiting Visibility public class Purse { private List<Coin> items; private int capacity; // withdraw is part of public interface public Coin[] withdraw( amount ) {. . . } public int get. Capacity( ) { return capacity; } // a method used only by the Purse private void sort. Coins( ) {. . . }

6. Re-use Code Know the Java API. Use API methods instead of writing them yourself. Apache Jakarta Commons project - reusable java code http: //jakarta. apache. org Google Guava - reusable Java code Example: a "List" class is more reusable than a "List of Bank Accounts".

7. Learn to Use Frameworks slf 4 j or Log 4 J - logging JUnit - unit testing JFree. Chart - many kinds of 2 D charts and graphs Web Frameworks - learn one. Play, Struts 2, Spring, Grails (uses Groovy)

References Steve Mc. Connell, Code Complete, 2 nd Edition. Robert Martin, Clean Code. http: //www. unclebob. com - Robert Martin's blog Joshua Bloch, Effective Java. Doesn't use some new Java features, but still lots of good advice.
- Slides: 13