Improving Code with Small Changes

small changes to code to improve it n.w
1 / 12
Embed
Share

Learn about the importance of small changes in code improvement, including refactoring, drafting, revising, and method composition. Refactoring defined by Martin Fowler, drafting like Thomas Jefferson's Declaration of Independence, and examples of code refactorings are explained. Discover how composing methods can enhance code readability and understandability.

  • Code Improvement
  • Refactoring
  • Drafting
  • Method Composition

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Small changes to code to improve it

  2. Refactoring Defined A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior Refactoring, Martin Fowler. et. al. , page 53 Martin wrote the book: Refactoring Improving the Design of Existing Code Refactoring owes William Opdyke's PhD thesis

  3. Write Drafts and Revise All good writing is based upon revision. Jacques Barzun, Simple & Direct, 4th Edition Revision means "to look at again" Example: Before 4-July, 1776, Thomas Jefferson's draft of the Declaration of Independence had this "We hold these truths to be sacred and undeniable, ..." He let his friend Benjamin Franklin look over this draft to make changes including this: "We hold these truths to be self-evident, ..." Total of 47 revisions, then Congress made 39 more

  4. Refactoring Happens In larger systems, code will be read and modified more frequently than it will be written Refactoring typically involves Removing duplicated or dead code Simplifying complex code Clarifying unclear code Removing code can be a good thing: Easier to develop, maintain, and change Refactoring can be risky, unless you do it in small steps and have automated tests that can be run anytime

  5. A few examples A few refactorings we've seen (they have names) Encapsulate Field (do this virtually always) add getters and/or setters to access a field Rename Method (often) Extract SuperClass (recall Shape) You have two classes with similar features Create a superclass and move common features to the superclass Compose Method A variety of refactoringscan happen at the method level

  6. Composed Method Some refactoring attempts to "compose" methods A composed method is relatively short by using calls to a small number of coherent elements Easier to understand what the method does Some refactorings for composing methods Extract Method: replace complex code with a nice name Inline Method: use code that's as easy to read as method Code demo: Compose insertElement in ArrayPriorityList

  7. Before public void insertElementAt(int index, E el) { if (size() == data.length) { Object[] temp = new Object[data.length + 20]; for (int i = 0; i < size; i++) temp[i] = data[i]; data = temp; } for (int j = size; j > index; j--) { data[j] = data[j - 1]; } data[index] = el; size++; }

  8. After public void insertElementAt(int index, E el) { if (shouldGrowArray()) gowArray(); makeRoomAt(index); addAt(index, el); } Is the new insertElementAt more readable? Vote: Yes: ____ Nay: _____ Are the extra private methods in the way? Vote: Yes: ____ Nay: _____

  9. Private Helpers Needed private void addAt(int index, E el) { data[index] = el; size++; } private void makeRoomAt(int index) { for (int j = size; j > index; j--) { data[j] = data[j - 1]; } } private void gowArray() { Object[] temp = new Object[data.length + 20]; for (int i = 0; i < size; i++) temp[i] = data[i]; data = temp; }

  10. Replace Nested Conditional with Guard Clauses see link private int boggleScoreOf(String word) { int sum = 0; if (next.length() == 3 || next.length() == 4) sum = 1; else if (next.length() == 5) sum = 2; else if (next.length() == 6) sum = 3; else if (next.length() == 7) sum = 5; else if (next.length() >= 8) sum = 11; else sum = 0; return sum; } 10

  11. Structured Programming used to say one return only, opinions can change private int boggleScoreOf(String next) { if (next.length() == 3 || next.length() == 4) return 1; if (next.length() == 5) return 2; if (next.length() == 6) return 3; if (next.length() == 7) return 5; if (next.length() >= 8) return 11; return 0; } 11

  12. Refactoring Catalog Check out Martin's Refactoring Catalog from the refactoring page See page on our webstite for required reading before the test http://www.refactoring.com/catalog/

More Related Content