avatarNicklas Millard

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2294

Abstract

very property has valid values.</p><figure id="a54f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KKBpIrM1mZVH1SpGqR_quQ.png"><figcaption>refactored guard statement</figcaption></figure><p id="cd9f">This refactoring technique is especially useful when you find yourself repeating the same guard clauses in multiple methods. A specification object, for instance, allows you to capture business rules in one place.</p><h2 id="282f">Assertions inside methods</h2><p id="ac8a">You’re quite familiar with assertions. Those statements at the end of unit tests. In defensive coding, they’re not restricted to testing.</p><p id="e5f7">You’ll likely be calling other class methods and even methods provided by external libraries. In such cases, we’d like to check if our assumptions about what those methods do or return are true, before continuing with our method execution.</p><figure id="e193"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*J9s_2x9rGneUVcivA5l6Lg.png"><figcaption>execution assertion</figcaption></figure><p id="0f44">We anticipate errors. Adding an<code>if</code> after invoking the database’s <code>Save()</code> method, we effectively assert what happened, and act accordingly. No surprises.</p><figure id="0204"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*a8birzn18M_tdm0l.png"><figcaption></figcaption></figure><div id="6db5" class="link-block"> <a href="https://readmedium.com/replacing-logical-statements-with-table-driven-methods-da1114512134"> <div> <div> <h2>Replacing Logical Statements with Table Driven Methods</h2> <div><h3>Let’s build better software with decision tables in place of if-else</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*EOYXlhVuCekdKNJbuUXlHg.png)"></div> </div> </div> </a> </div><div id="eddb" class="link-block"> <a href="https://readmedium.com/5-ways-to-replace-if-else-statements-857c0ff19357"> <div> <div> <h2>Better Software Without If-Else</h2> <div><

Options

h3>5 Ways to Replace If-Else. Beginner to advanced examples</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*3Ju0VYbFi0w0rVYt9RT_vw.png)"></div> </div> </div> </a> </div><div id="bd4e" class="link-block"> <a href="https://readmedium.com/stop-using-if-else-statements-f4d2323e6e4"> <div> <div> <h2>Stop Using If-Else Statements</h2> <div><h3>Write clean, maintainable code without if-else.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*xUWTgHxBR4P_lYWzqGgImQ.png)"></div> </div> </div> </a> </div><div id="7366" class="link-block"> <a href="https://readmedium.com/dont-use-database-generated-ids-d703d35e9cc4"> <div> <div> <h2>Don’t Use Database Generated IDs</h2> <div><h3>Stop letting the database be in charge of your application</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*mfjRjgnZNGUwVSeJwYmQvQ.png)"></div> </div> </div> </a> </div><figure id="b109"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*LZ0-rAawOB4iv6uI.png"><figcaption></figcaption></figure><p id="cc49"><b>Nicklas Millard</b> is a software development engineer in one of the fastest-growing banks, building mission-critical financial services infrastructure.</p><p id="7bfa">Previously, he was a Big4 Senior Tech Consultant developing software for commercial clients and government institutions.</p><blockquote id="71d7"><p><a href="https://www.youtube.com/channel/UCaUy83EAkVdXsZjF3xGSvMw">New YouTube Channel (@Nicklas Millard)</a></p></blockquote><blockquote id="a707"><p><i>Connect on <a href="https://www.linkedin.com/in/nicklasmillard/">LinkedIn</a></i></p></blockquote></article></body>

PRACTICAL PROGRAMMING ADVICE

2 Defensive Coding Techniques You Should Use Today

Simple but powerful techniques for safe, maintainable software

I’ll keep this one short and sweet.

We’ll be going over two very simple and practical defensive techniques you can start applying to your project today.

As a professional developer, chances are you’re already applying these techniques, so you might want to sit this one out.

However, for anyone who’s yet to reach a professional level, these techniques will improve your quality and make your code safer.

If you’ve never heard of defensive coding before, it’s about time. Here’s the gist of it.

Defensive coding allows our software to behave in a correct manner, despite incorrect input.

Right, let’s look into how you can make your software behave nicely, even when it’s provided with wrong inputs.

Guard Clauses — checking preconditions

These one-liners are one of the absolute cornerstones of defensive coding. They sit at the top of your methods making sure the methods only continue executing when valid input is provided.

They’re precondition checks. Here’s a very simple example, but nonetheless a real one.

simple guard statement

We’re simply checking to see if the caller has provided us with a non-null or empty value. We’d typically do this if proceeding with such values might cause unexpected results.

You’ll need to refactor to another guard clause approach when your methods start to take a lot of arguments, or, one argument calls for multiple guard clauses. Such refactoring may involve creating an object that holds all the properties needed, as well as a IsValid() method. Here, the IsValid()` method is checking its state to validate if every property has valid values.

refactored guard statement

This refactoring technique is especially useful when you find yourself repeating the same guard clauses in multiple methods. A specification object, for instance, allows you to capture business rules in one place.

Assertions inside methods

You’re quite familiar with assertions. Those statements at the end of unit tests. In defensive coding, they’re not restricted to testing.

You’ll likely be calling other class methods and even methods provided by external libraries. In such cases, we’d like to check if our assumptions about what those methods do or return are true, before continuing with our method execution.

execution assertion

We anticipate errors. Adding anif after invoking the database’s Save() method, we effectively assert what happened, and act accordingly. No surprises.

Nicklas Millard is a software development engineer in one of the fastest-growing banks, building mission-critical financial services infrastructure.

Previously, he was a Big4 Senior Tech Consultant developing software for commercial clients and government institutions.

New YouTube Channel (@Nicklas Millard)

Connect on LinkedIn

Programming
Technology
Software Development
Software Engineering
Best Practices
Recommended from ReadMedium