• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Stoopid code
 

Stoopid code

on

  • 693 views

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

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

Statistics

Views

Total Views
693
Views on SlideShare
687
Embed Views
6

Actions

Likes
0
Downloads
0
Comments
1

3 Embeds 6

http://us-w1.rockmelt.com 2
http://www.linkedin.com 2
https://twitter.com 2

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • 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!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Welcome!\n
  • \n\n
  • \n\n
  • All Greek to me\n
  • Spaghetti code\n
  • Make it fit\n
  • \n\n
  • Take chaos\n
  • Bring order\n
  • When simplifying:\n1 one thing\n2 length\n3 remove duplicates\n
  • Jackson pollock as a coder\n
  • Zen!?\nMake sure you're adding value\n
  • Too simple\n
  • \n\n
  • Fragile\n
  • Strong\n
  • Pre release code\n
  • Simplified for example\n
  • Mapping\n
  • Extract\n
  • Call\n
  • Serialize\n
  • Extract\n
  • Call\n
  • Revisit create\n
  • Extract\n
  • Call\n
  • Create is now simple\n
  • Clearer names, easier to add...\n
  • Update\n
  • Duplicate code\n
  • Just reuse update\n
  • Controller just takes calls\n\n\n
  • Why is there biz logic?\n
  • Move business code out\n
  • Contact knows its own data\n
  • Let it own it\n
  • Final\nClear\nReadable\n
  • \n\n
  • \n\n
  • \n\n
  • \n\n
  • \n\n
  • \n\n
  • \n\n
  • \n\n
  • \n\n

Stoopid code Stoopid code Presentation Transcript

  • Code Wants to be Stoopid!
  • Who am I? • Jim Siegienski • 16 years of experience • Fortune 50 to Start-ups • Application Architect at...
  • Dave Ramsey Development Team
  • Dave Ramsey Development Team
  • 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;
  • 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;        }    }}
  • 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
  • "Any fool can write code that a computer can understand. Goodprogrammers write code that humans can understand." Martin Fowler
  • 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 }}
  • 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 }}
  • 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 }}
  • 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 } }}
  • 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 } }}
  • 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 } }}
  • 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 }}
  • 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 }}
  • 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 }}
  • 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 }}
  • 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 }}
  • 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 }}
  • 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 }}
  • 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 }
  • 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 }
  • 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 }
  • 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 }
  • 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 }
  • 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 }}
  • 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 }}
  • 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 }}
  • 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 }}
  • Summary
  • 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
  • Method Length• Over 10 lines... Break it apart• 1,000 lines and beyond - seek help
  • Duplicate Code• Combine• Move it?• Leverage
  • "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)
  • “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
  • Questions?
  • Thank You• Dave Ramsey Team• Luke Stokes - FoxyCart• BarCamp Nashville• Fellow Coders
  • 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