Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Your code smells too! Time to deodorize

439 views

Published on

Session about code smells as presented at
- EPAM SEC Spring 2014 in June 2014 in Budapest, Hungary
- ACE in March 2015 in Krakow, Poland

Published in: Software
  • Be the first to comment

  • Be the first to like this

Your code smells too! Time to deodorize

  1. 1. Your Code Smells Too! Time to Deodorize Gino Marckx
  2. 2. cb pyroclastichawk - https://www.flickr.com/photos/18536761@N00/367669179
  3. 3. cb Iwan Gabovitch - https://www.flickr.com/photos/qubodup/4439090296
  4. 4. cb Iwan Gabovitch - https://www.flickr.com/photos/qubodup/4438310359
  5. 5. CAUTION BAD ROAD AHEAD
  6. 6. CAUTION BAD CODE AHEAD
  7. 7. public class Person { public String m_name; public String m_street; public String m_zip; public String m_city; public String m_province; public String m_country; public Person(String name, String country) { ... } public Person(String name, String town, String country) { ... } public Person(String name, String s, String z, String c, String p, String country) { ... } 
 public void add(Person person) { ... } public final List getFriends() { ... } public double between(Person person) { ... } public Person closestFriend() { ... } }
  8. 8. The Wallaroo Prints © 2011
  9. 9. public class Person { public String name; public Address address; public Person(String name, Address address) { ... } public void addFriend(Person person) { ... } public List<Person> getFriends() { ... } /** * Find the friend who lives closest. * * @return the friend who lives closest * @throws NoSuchElementException in case this person has no friends */ public Person getNearestFriend() { ... } }
  10. 10. • Meaningful names • Excessive use of literals • Long parameter list • Excessively short identifiers • Feature envy • Readability • Maintainability • Velocity
  11. 11. public class Address { public Address(String country) { ... } public Address(String city, String country) { ... } public Address(String street, String postalCode, String city, String state, String country) { ... } public String getStreet() { ... } public String getPostalCode() { ... } public String getCity() { ... } public String getState() { ... } public String getCountry() { ... } public Point getGeographicalLocation() { ... } /** * Calculate the distance in kilometres to another address. * * @param anotherAddress the other address to calculate the distance to * @return the distance in kilometres between this address and the other * address * @throws IllegalArgumentException when location of either address is * unknown */ public double distanceTo(Address anotherAddress) { ... } }
  12. 12. http://www.zazzle.ca/comments_a_deodorant_to_mask_code_smells_tshirt-235182622652051339
  13. 13. The Wallaroo Prints © 2011
  14. 14. public class AddressBuilder { public AddressBuilder(AddressLocator addressLocator) { ... } public AddressBuilder() { ... } public AddressBuilder withCountry(String country) { ... } public AddressBuilder withStreet(String street) { ... } public AddressBuilder withCity(String city) { ... } public AddressBuilder withPostalCode(String postalCode) { ... } public AddressBuilder withState(String state) { ... } public Address build() throws UnknownGeographicalLocation { ... } } public class Address { Address(AddressLocator locator, String street, String zipCode, String city, String state, String country) { ... } ... public Distance distanceTo(Address anotherAddress) { ... } }
  15. 15. public class AddressTest { public AddressLocator createMockLocatorWithPolandCentreOfTheUniverse() { ... } @Test public void testDistanceTo() throws Throwable { AddressBuilder builder = new AddressBuilder( createMockLocatorWithPolandCentreOfTheUniverse()); Address polishAddress = builder.withCountry("Poland").build(), canadianAddress = builder.withCountry("Canada").build(); assertEquals(new Distance(1000, Distance.Unit.KILOMETRE), polishAddress.distanceTo(canadianAddress)); } }
  16. 16. • Dependency inversion • Excessive use of literals • Long parameter list • Feature envy • Testability • Maintainability • Velocity
  17. 17. Velocity Testability Maintainability
  18. 18. Build Software
 The Right Way … to be agile
  19. 19. Thank you! Gino Marckx Director, Agile Competency Center

×