1 / 13

Writing Better Code

Learn a few tips for writing better code, including easy-to-read code, variable localization, reducing coupling, using interfaces, avoiding duplicate logic, limiting visibility, reusing code, and learning frameworks.

econdon
Download Presentation

Writing Better Code

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. a few suggestions Writing Better Code

  2. 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 getCoins(t,f,o); else return null; }

  3. 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 CoinMachine { List<Coin> items; int balance; public int getBalance( ) { balance = 0; for(Coin c: items) balance += c.getValue(); return balance; }

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

  5. 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

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

  7. 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

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

  9. 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

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

  11. 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".

  12. 7. Learn to Use Frameworks slf4j or Log4J - logging JUnit - unit testing JFreeChart - many kinds of 2D charts and graphs Web Frameworks - learn one. Play, Struts2, Spring, Grails (uses Groovy)

  13. References Steve McConnell, Code Complete, 2nd 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.

More Related