DEV Community

CodingBlocks

Episode 48 – Clean Code – How to Write Amazing Functions

We continue talking our way through Clean Code, taking a deep look at the building blocks of programming in the quest to write the best functions. Oh, and everybody sings.

The original version of the show notes can be found at:
http://www.codingblocks.net/episode48

#yop-poll-container-24_yp5804352f3398e { width: 360; background:#fff; padding:10px; color:#555; overflow:hidden; font-size:12px; } #yop-poll-container-24_yp5804352f3398e input[type='text'] { margin:0px 0px 5px 0px; padding:2%; width:96%; text-indent:2%; font-size:12px; } .yop-poll-name-24_yp5804352f3398e { font-weight:bold; background:#327BD6; color:#fff; padding:5px; text-align:center; font-size:12px; } #yop-poll-questions-container-24_yp5804352f3398e { font-size:14px; margin:5px 0px; } .yop-poll-question-container-24_yp5804352f3398e { padding: 2px; } .yop-poll-question-24_yp5804352f3398e { background:#327BD6; color:#fff; margin-bottom: 21px; margin-top: -10px; font-style: italic; text-align: center; width: 100%; padding:5px; } .yop-poll-answers-24_yp5804352f3398e { } .yop-poll-answers-24_yp5804352f3398e ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-answer-24_yp5804352f3398e { font-style:normal; margin:0px 0px 10px 0px; padding:0px; font-size:12px; margin-bottom:20px; } .yop-poll-li-answer-24_yp5804352f3398e input { margin:0px; float:none; } .yop-poll-li-answer-24_yp5804352f3398e label { margin:0px; font-style:normal; font-weight:normal; font-size:12px; float:none; } .yop-poll-results-24_yp5804352f3398e { font-size: 12px; font-style: italic; font-weight: normal; margin-left: 15px; } .yop-poll-customs-24_yp5804352f3398e { } .yop-poll-customs-24_yp5804352f3398e ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-custom-24_yp5804352f3398e { padding:0px; margin:0px; font-size:14px; } /* Start CAPTCHA div style*/ #yop-poll-captcha-input-div-24_yp5804352f3398e { margin-top:5px; } #yop-poll-captcha-helpers-div-24_yp5804352f3398e { width:30px; float:left; margin-left:5px; height:0px; } #yop-poll-captcha-helpers-div-24_yp5804352f3398e img { margin-bottom:2px; } #yop-poll-captcha-image-div-24_yp5804352f3398e { margin-bottom:5px; } #yop_poll_captcha_image_24_yp5804352f3398e { float:left; } /* End CAPTCHA div style*/ .yop-poll-clear-24_yp5804352f3398e { clear:both; } #yop-poll-vote-24_yp5804352f3398e { } /* Start Result bar*/ .yop-poll-results-bar-24_yp5804352f3398e { background:#f5f5f5; height:10px; } .yop-poll-results-bar-24_yp5804352f3398e div { background:#555; height:10px; } /* End Result bar*/ /* Start Vote Button*/ #yop-poll-vote-24_yp5804352f3398e div#yop-poll-vote-24_yp5804352f3398e button { float:left; } #yop-poll-vote-24_yp5804352f3398e div#yop-poll-results-24_yp5804352f3398e { float: right; margin-bottom: 20px; margin-top: -20px; width: auto; } #yop-poll-vote-24_yp5804352f3398e div#yop-poll-results-24_yp5804352f3398e a { color:#fff; text-decoration:underline; font-size:12px; } #yop-poll-vote-24_yp5804352f3398e div#yop-poll-back-24_yp5804352f3398e a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-24_yp5804352f3398e div#yop-poll-archive-24_yp5804352f3398e a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-24_yp5804352f3398e div { float:left; width:100%; } /* End Vote Button*/ /* Start Messages*/ #yop-poll-container-error-24_yp5804352f3398e { font-size:12px; font-style:italic; color:red; text-transform:lowercase; margin-bottom:20px; text-align:center; } #yop-poll-container-success-24_yp5804352f3398e { font-size:12px; font-style:italic; color:green; margin-bottom:20px; text-align:center; } /* End Messages*/#yop-poll-container-24_yp5804352f3398e img { max-width: 360; } .yop-poll-forms-display{}
What is your preferred work environment?
  • Love me some cubicles
  • Open Floor Plan, collaboration!
  • Home office (pants optional)
  • A work office, with a door
  • Coffee / Tea Shoppe
Live Coding w/ Joe

Live Coding w/ Joe

Podcast News

Clean Code

Clean Code

Functions

  • 1st rule – Functions should be small
  • 2nd rule – Functions should be smaller than that!
  • Functions should barely be 20 lines long…
  • Each function should lead you to the next function in a compelling order – like telling a story
  • They should do ONE thing
  • Code within if’s or block statements should be one line long, meaning they should call another function…overkill?
  • The indent levels of a function probably shouldn’t be more than two tabs! (tabs vs spaces)
  • Prepend “TO” to the name of your function and see if you can describe what it should be doing…it may seem like more than one thing
  • “Another way to know that a function is doing more than one thing is if you can extract another function from it with a name that is not a restatement of it’s implementation”
  • Functions that do just one thing can’t be divided into sections…they’re not long enough
  • Keep functionality at the same level of abstraction – interesting…“getHtml()” vs “getPath” vs path.append(“newline”)
  • The StepDown Rule – the code should read like a paragraph of “To do …” – and those should be at the same basic level of abstraction
  • Switch statements – should be created only once and should be used to create polymorphic objects – objects with the same methods but different implementations
  • Use descriptive names – Ward’s Principle “you know you’re working on clean code when each routine turns out to be pretty much what you expected”
  • The ugliest functions I “write” are refactors – they take too many arguments, because the code isn’t separated by concern – mixing data gathering, logic and presentation!
  • The smaller and more focused a function, the easier it is to name
  • Don’t be afraid to make a long name – a long descriptive name is better than a short enigmatic one
  • A long name is better than a long comment
  • Be consistent in your naming – again, it should be like reading a paragraph – easy to follow

Function Arguments

  • You should basically never use more than 2 arguments in a function – if so, there’d better be a great reason
  • You do this by making values instance variables and then use the instance methods to work with those arguments
  • Testing with multiple arguments becomes very difficult
  • Niladic – no arguments – absolute best case
  • Monadic – single argument – asking a question about the argument, or transforming and returning
  • Other monadic uses are events
  • output variables can be confusing and probably avoided where possible
  • Immediately tells you that the function is going to do something different based on the value of the flag
  • Dyadic Functions – harder to understand just by looking at them…easy to mix up the order of the params
  • Triadic functions – significantly harder than dyadic – avoid if possible – make arguments class members so niladic methods could be called
  • If a method needs more than 2 or 3 arguments, pass in an object
  • When using argument lists, still stick to the same rule – no more than 3 – the list of objects would be your third…n parameter

Verbs and Keywords

  • Naming your functions with good verbs and using the arguments as keywords can be a good thing assertExpectedEqualsActual(expected, actual);
  • Your functions should not have side-effects – if it says it does one thing, it should ONLY do that thing
  • Avoid making methods that require you to inspect the declaration – that means the method isn’t named something that identifies its true intention well
  • Generally speaking, output variables should be avoided

Command Query Separation

  • Functions should do something or answer something, not both
  • Prefer exceptions as opposed to error codes
  • Extract the try/catches out into functions that do the body of work – Throw liberally, catch sparingly
  • Error handling is “one thing” – so it should be the beginning and end of your function

Don’t Duplicate Code

  • One of the biggest problems in programming – duplication leads to multiple places to change and more places where bugs can occur
  • Don’t necessarily stick to the single entry / single exit rule – just make sure the functions are small enough to understand

How do you make functions like this?

  • Revise revise revise. Write your code first and start chopping away at it until you have something that follows the rules mentioned

Tip of the Week

  • Conditional break points with actions. And labels.
  • “Show names of files currently staged: git diff –name-only –cached”
  • MsDevShow Episode on Refactoring with Katrina Owen: https://www.youtube.com/watch?v=LyU3Pk6Wt4

Episode source