This week, we continue our Clean Code discussion as we dive into the joys and pains of error handing.
Seriously, read this book!
Join us for more discussion by becoming a part of our Slack community by signing up at http://www.codingblocks.net/slack.
For the full show notes, visit:
http://www.codingblocks.net/episode52
Watch and Listen
Survey
#yop-poll-container-28_yp593f59cff3986 {
width: 600px;
background:#fff;
padding:10px;
color:#555;
overflow:hidden;
font-size:12px;
}
#yop-poll-container-28_yp593f59cff3986 input[type='text'] {
margin:0px 0px 5px 0px;
padding:2%;
width:96%;
text-indent:2%;
font-size:12px;
}
.yop-poll-name-28_yp593f59cff3986 {
font-size:14px;
font-weight:bold;
}
#yop-poll-questions-container-28_yp593f59cff3986 {
font-size:14px;
margin:5px 0px;
}
.yop-poll-question-container-28_yp593f59cff3986 {
padding: 2px;
}
.yop-poll-question-28_yp593f59cff3986 {
font-style: italic;
text-align: center;
margin-bottom: 21px;
margin-top: -10px;
width: 100%;
}
.yop-poll-answers-28_yp593f59cff3986 { }
.yop-poll-answers-28_yp593f59cff3986 ul {
list-style: none outside none;
margin: 0;
padding: 0;
}
.yop-poll-li-answer-28_yp593f59cff3986 {
font-style:normal;
margin:0px 0px 10px 0px;
padding:0px;
font-size:12px;
margin-bottom:20px;
margin-bottom:20px;
}
.yop-poll-li-answer-28_yp593f59cff3986 input {
margin:0px;
float:none;
}
.yop-poll-li-answer-28_yp593f59cff3986 label {
margin:0px;
font-style:normal;
font-weight:normal;
font-size:12px;
float:none;
}
.yop-poll-results-28_yp593f59cff3986 {
font-size: 12px;
font-style: italic;
font-weight: normal;
margin-left: 15px;
}
.yop-poll-customs-28_yp593f59cff3986 { }
.yop-poll-customs-28_yp593f59cff3986 ul {
list-style: none outside none;
margin: 0;
padding: 0;
}
.yop-poll-li-custom-28_yp593f59cff3986 {
padding:0px;
margin:0px;
font-size:14px;
}
/* Start CAPTCHA div style*/
#yop-poll-captcha-input-div-28_yp593f59cff3986 {
margin-top:5px;
}
#yop-poll-captcha-helpers-div-28_yp593f59cff3986 {
width:30px;
float:left;
margin-left:5px;
height:0px;
}
#yop-poll-captcha-helpers-div-28_yp593f59cff3986 img {
margin-bottom:2px;
}
#yop-poll-captcha-image-div-28_yp593f59cff3986 {
margin-bottom:5px;
}
#yop_poll_captcha_image_28_yp593f59cff3986 {
float:left;
}
/* End CAPTCHA div style*/
.yop-poll-clear-28_yp593f59cff3986 {
clear:both;
}
#yop-poll-vote-28_yp593f59cff3986 {
}
/* Start Result bar*/
.yop-poll-results-bar-28_yp593f59cff3986 {
background:#f5f5f5;
height:10px;
}
.yop-poll-results-bar-28_yp593f59cff3986 div {
background:#555;
height:10px;
}
/* End Result bar*/
/* Start Vote Button*/
#yop-poll-vote-28_yp593f59cff3986 div#yop-poll-vote-28_yp593f59cff3986 button {
float:left;
}
#yop-poll-vote-28_yp593f59cff3986 div#yop-poll-results-28_yp593f59cff3986 {
float: right;
margin-bottom: 20px;
margin-top: -20px;
width: auto;
}
#yop-poll-vote-28_yp593f59cff3986 div#yop-poll-results-28_yp593f59cff3986 a {
color:#555;
text-decoration:underline;
font-size:12px;
}
#yop-poll-vote-28_yp593f59cff3986 div#yop-poll-back-28_yp593f59cff3986 a {
color:#555;
text-decoration:underline;
font-size:12px;
}#yop-poll-vote-28_yp593f59cff3986 div#yop-poll-archive-28_yp593f59cff3986 a {
color:#555;
text-decoration:underline;
font-size:12px;
}
#yop-poll-vote-28_yp593f59cff3986 div {
float:left;
width:100%;
}
/* End Vote Button*/
/* Start Messages*/
#yop-poll-container-error-28_yp593f59cff3986 {
font-size:12px;
font-style:italic;
color:red;
text-transform:lowercase;
margin-bottom:20px;
text-align:center;
}
#yop-poll-container-success-28_yp593f59cff3986 {
font-size:12px;
font-style:italic;
color:green;
margin-bottom:20px;
text-align:center;
}
/* End Messages*/
.yop-poll-clear-28_yp593f59cff3986 {
clear: both;
}#yop-poll-container-28_yp593f59cff3986 img {
max-width: 600px;
} .yop-poll-forms-display{}
Do you listen to anything while you code?
-
Music, because I gotta get my jam on.
-
Podcasts, how else do you think I'm listening to this?
-
Audio books, because the book is always better than the movie.
-
TV, how else am I going to stay up to date on Westworld?
-
The office background, because I'm actually listening to my cubicle neighbors' gossip.
-
White noise, rain, the ocean, any repetitive noise to drown out reality
-
Who has time to listen to anything when I've got all of these fires to put out?
News
- Thanks to those that left reviews.
- OMG, we hit the 1 *MILLION* download milestone!
- Congratulations to Veselin for episode 50’s Clean Code giveaway.
- It’s 2016, get out there an VOTE! (For the 2016 Software Developer Podcast Awards that is.)
- Need stickers? Of course you do! Send us a SASE and we’ll make sure you get some. Click here for more information.
- Special thanks to azeteg for audio template.
Error Handling
- This chapter was written by Michael Feathers, known for “Working Effectively with Legacy Code” http://amzn.to/2hWUVtt
- Error handling is important and needed, but if it obscures logic, it’s being done wrong
- Use Exceptions rather than Return Codes
- Return codes force the caller to check the codes and act accordingly, which obscures logic
- Throwing an exception is cleaner as the logic isn’t obscured by the error handling
- Keeps the logic away from the errors – separation of concerns
- Error handing and Go – https://blog.golang.org/error-handling-and-go
- Start with your try catch finally statements
- Doing so forces you to think about the state of your application – you should be left in a good state if there is an error and that would resume in the catch
- Try to write tests that FORCE exceptions
- Then add behaviors to your handler to handle the exceptions
- This helps you maintain the scope of your try/catch’s
- C# “using” has spoiled us!
Checked Exceptions
- A thing in Java that would force a developer to list all the types of exceptions that could be returned from a called method
- Violates the open/closed principle
- Every method between the method being called and the calling method would have to include those exception lists. If you change what the called method can throw, you need to revisit every single method
- Typically the costs / maintenance outweigh the benefits
Context with Exceptions
- Provide enough context with your exception to determine the source and location of the error – No more “Null Reference”
- Stack trace isn’t enough – doesn’t identify the intention
- Include messages along with the exceptions to help identify what the intention was, along with any stack / location info
Define Exceptions in terms of How they are Caught
- Think in terms of how the exception will be used
- Don’t create redundant exception classes if they’ll simply be used to do the same thing, such as logging the error
- Think in terms of actionable exceptions
- In the case of utilizing another libraries exceptions, if you’ll be doing the same thing for each type, write your own wrapper to simplify / reduce copies
- Wrapping 3rd party API’s are a “best practice” according to the book
Special Case Pattern
- Martin Fowler’s Special Case pattern
- Visual Studio magazine’s take on The Special Case Pattern
- The Null Object pattern
- Don’t use catch’s to handle additional business logic flow
- The book uses the notion of a meal expense. Get the meals.getTotal() catch that error if it didn’t exist and then add perdiem
- So, rather than return the expenses, return a MealExpense object that has a getTotal() method – if there were meals, then the MealExpense object getTotal will total the meals cost, otherwise it’ll return a PerDiemMealExpense that extends MealExpense
Don’t Return NULL
- Says to use the Special Case Pattern – I think we’ve referred to it as the NULL object pattern?
- NULL checks junk up your code and deter from the algorithm / application logic
- If not doing a special case pattern, consider throwing an exception rather than returning null
- If dealing with a 3rd party that returns nulls, consider the facade pattern and intercept and return an object
- Poetry reading of Michael’s favorite poem: http://www.codingblocks.net/podcast/episode-10-csharp-6-and-roslyn/
Don’t Pass NULL
- Worse than returning null!
- Just don’t allow methods to take in null parameters – will make your code much cleaner / more maintainable
Resources We Like
Clean Code (Amazon)
Tip of the Week
- “Filter messages like these”
Easily filter emails in gmail!
in Gmail
- Azure Functions
- Use CTRL+, to navigate to a file in VS2013 and VS2015
Like this Show? Vote for us!
We’ve been included in the 2016 Software Developer Podcast Awards. We’d _super_ appreciate you throwing a vote our way.