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.

Stoopid code

863 views

Published on

’Code wants to be Stoopid’ - how to keep your code simple as your system grows in complexity

Published in: Technology, Business
  • Which I could of made the talk. Is there a way you can update this with annotation of your actually talk part so I can make sense of the slides? Thanks!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Stoopid code

  1. 1. Code Wants to be Stoopid!
  2. 2. Who am I? • Jim Siegienski • 16 years of experience • Fortune 50 to Start-ups • Application Architect at...
  3. 3. Dave Ramsey Development Team
  4. 4. Dave Ramsey Development Team
  5. 5. BEGIN  domainlength := LENGTH(domainin);  firstplace := INSTR(domainin, |, 1, 1);  IF firstplace != 0 THEN    secondplace := INSTR (domainin, |, 1, 2);  END IF;  IF secondplace != 0 THEN     thirdplace := INSTR (domainin, |, 1, 3);  END IF;  IF thirdplace != 0 THEN     fourthplace := INSTR (domainin, |, 1, 4);  END IF;  IF fourthplace != 0 THEN     fifthplace := INSTR (domainin, |, 1, 5);  END IF;  IF fifthplace != 0 THEN     sixthplace := INSTR (domainin, |, 1, 6);  END IF;  IF sixthplace != 0 THEN     seventhplace := INSTR (domainin, |, 1, 7);  END IF;  IF firstplace != 0 AND firstplace != domainlength THEN     firststring := SUBSTR(domainin, 1, (firstplace-1));  ELSIF firstplace = domainlength THEN      firststring := SUBSTR (domainin, 1, (firstplace -1  ));  ELSIF firstplace = 0 AND domainlength !=0 THEN      firststring := domainin;        GOTO get_domain_id;  END IF;  IF secondplace != 0 AND secondplace != domainlength THEN     secondstring := SUBSTR(domainin, (firstplace+1), (secondplace - firstplace-1));  ELSIF secondplace = domainlength THEN      secondstring := SUBSTR (domainin, (firstplace+1), (secondplace-firstplace -1  ));  ELSIF secondplace = 0 AND firstplace != 0 THEN      secondstring := SUBSTR (domainin, (firstplace+1), (domainlength - firstplace));      GOTO get_domain_id;  END IF;  IF thirdplace != 0 AND thirdplace != domainlength THEN     thirdstring := SUBSTR(domainin, (secondplace+1), (thirdplace - secondplace -1));  ELSIF thirdplace = domainlength THEN      thirdstring := SUBSTR (domainin, (secondplace+1), (thirdplace-secondplace -1  ));  ELSIF thirdplace = 0 AND secondplace != 0 THEN      thirdstring := SUBSTR (domainin, (secondplace + 1), (domainlength - secondplace));      GOTO get_domain_id;  END IF;
  6. 6. private void SetAccount(RequisitionData.RequisitionItem requisitionItem,                        AccountData.Account account, bool automation){    bool allowSetAccount = false;     if(account != null)    {        // if the account entry is being set by automation, ensure that        // the user hasnt already set a value        if (automation)        {            if (!requisitionItem.IsAccountCodeNull())            {                if (requisitionItem.AccountCode == string.Empty)                    allowSetAccount = true;                else                    allowSetAccount = true;            }            else                allowSetAccount = true;        }        else            allowSetAccount = true;         if (allowSetAccount)        {            requisitionItem.AccountID = account.ID;            requisitionItem.AccountCode = account.Code;        }    }}
  7. 7. Public Function makeBoolean(ByVal b As Boolean) Return bEnd FunctionPublic Function makeByte(ByVal b As Byte) Return bEnd FunctionPublic Function makeChar(ByVal c As Char) Return cEnd FunctionPublic Function makeDate(ByVal d As Date) Return dEnd FunctionPublic Function makeDecimal(ByVal d As Decimal) Return dEnd FunctionPublic Function makeUShort(ByVal u As UShort) Return uEnd Function
  8. 8. "Any fool can write code that a computer can understand. Goodprogrammers write code that humans can understand." Martin Fowler
  9. 9. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() //map json to object if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } //checking attributes HashSet set = new HashSet() json.each { attribute -> ContactAttributeType type = new ContactAttributeType() type.value = attribute.key if (attribute.key == email) { attribute.value.each { val -> ContactAttribute a = new ContactAttribute() a.key = val.key a.value = val.value a.type = type set.add(a) } } if (attribute.key == address) { attribute.value.each { val -> ContactAttribute a = new ContactAttribute() a.key = val.key a.value = val.value a.type = type set.add(a) } } } contact.attributes = set //save if (!contact.save()) { contact.errors.each { println it } } //convert to a map to output Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) contact.attributes.each { att -> Map m = map.get(att.type.value) if (!m) { m = new HashMap() map.put(att.type.value, m) } m.put(att.key, att.value) } render map as JSON }}
  10. 10. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } if (!contact.save()) { contact.errors.each { println it } } Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) render map as JSON }}
  11. 11. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } if (!contact.save()) { contact.errors.each { println it } } Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) render map as JSON }}
  12. 12. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) render map as JSON } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } }}
  13. 13. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) render map as JSON } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } }}
  14. 14. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) render map as JSON } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } }}
  15. 15. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } render serialize(contact) as JSON } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } } def serialize = { contact -> Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) return map }}
  16. 16. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } render serialize(contact) as JSON } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } } def serialize = { contact -> Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) return map }}
  17. 17. class ContactController { def create = { def json = request.JSON Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } render serialize(contact) as JSON } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } } def serialize = { contact -> Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) return map }}
  18. 18. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } } def serialize = { contact -> Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) return map }}
  19. 19. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } } def serialize = { contact -> Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) return map }}
  20. 20. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } } def serialize = { contact -> Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) return map }}
  21. 21. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact } def applyJson = { contact, json -> if (json.first_name) { contact.firstName = json.first_name } if (json.last_name) { contact.lastName = json.last_name } } def serialize = { contact -> Map map = new HashMap() map.put("id", contact.id) map.put("first_name", contact.firstName) map.put("last_name", contact.lastName) return map }}
  22. 22. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def update = { contact -> contact = updateContact(contact, request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact } def updateContact = { contact, json -> applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact }
  23. 23. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def update = { contact -> contact = updateContact(contact, request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact } def updateContact = { contact, json -> applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact }
  24. 24. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def update = { contact -> contact = updateContact(contact, request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() return updateJson(contact, json) } def updateContact = { contact, json -> applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact }
  25. 25. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def update = { contact -> contact = updateContact(contact, request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() return updateJson(contact, json) } def updateContact = { contact, json -> applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact }
  26. 26. class ContactController { def create = { def contact = createContact(request.JSON) render serialize(contact) as JSON } def update = { contact -> contact = updateContact(contact, request.JSON) render serialize(contact) as JSON } def createContact = { json -> Contact contact = new Contact() return updateJson(contact, json) } def updateContact = { contact, json -> applyJson(contact, json) if (!contact.save()) { contact.errors.each { println it } } return contact }
  27. 27. class ContactController { def ContactService contactService def create = { def contact = contactService.create(request.JSON) render contactService.serialize(contact) as JSON } def update = { contact -> contact = contactService.updateContact(contact, request.JSON) render contactService.serialize(contact) as JSON }}
  28. 28. class ContactController { def ContactService contactService def create = { def contact = contactService.create(request.JSON) render contactService.serialize(contact) as JSON } def update = { contact -> contact = contactService.updateContact(contact, request.JSON) render contactService.serialize(contact) as JSON }}
  29. 29. class ContactController { def ContactService contactService def create = { def contact = contactService.create(request.JSON) render contact.serialize() as JSON } def update = { contact -> contact = contactService.updateContact(contact, request.JSON) render contact.serialize() as JSON }}
  30. 30. class ContactController { def ContactService contactService def create = { def contact = contactService.create(request.JSON) render contact.serialize() as JSON } def update = { contact -> contact = contactService.updateContact(contact, request.JSON) render contact.serialize() as JSON }}
  31. 31. Summary
  32. 32. One Thing• A method should only do one thing• It should do what its name says it does• Comments are a hint youre doing too much
  33. 33. Method Length• Over 10 lines... Break it apart• 1,000 lines and beyond - seek help
  34. 34. Duplicate Code• Combine• Move it?• Leverage
  35. 35. "No matter who. No matter what. No matter when. Short term. Long term. Any term. Writing good code isALWAYS faster than writing bad code." Robert Martin (AKA Uncle Bob)
  36. 36. “When you’re a carpenter making a beautiful chest of drawers, you’renot going to use a piece of plywood on the back, even though it facesthe wall and nobody will ever see it.” Steve Jobs
  37. 37. Questions?
  38. 38. Thank You• Dave Ramsey Team• Luke Stokes - FoxyCart• BarCamp Nashville• Fellow Coders
  39. 39. Resources• @unclebobmartin• http://www.objectmentor.com/omTeam/martin_r.html• @martinfowlern• http://martinfowler.com/• Refactoring: Improving the Design of Existing Code by Martin Fowler• @JimSiegienski• Jim.Siegienski@gmail.com

×