Are you using Void correctly?
Tags: webdev, rant, beginners, productivity
Published: 2019-11-05 14:51:39

---
title: Are you using Void correctly?
published: true
description: I don't like void functions. Let's look at the purpose of them and try to comprehend the utter confusion they bring.
tags: webdev, rant, beginners, productivity
cover_image: https://thepracticaldev.s3.amazonaws.com/i/mttfzg3v8pr9mmsjdr2k.png
canonical_url: https://blog.designpuddle.com/web-development/using-void-correctly/
---

<!-- wp:paragraph -->
<p><a href="https://www.guru99.com/vb-net-introduction-features.html">Vb.Net</a> got something right. Its separation of the trusty <strong>function</strong> and the <strong>sub</strong>routine makes perfect sense. Unfortunately, that transition has not made its way to any other recent language. Delphi and Pascal also use this idiom, labelling them Procedures and Functions respectively.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>All other languages that omit a value from a method call, return a void type. By default, you will create your function and then decide whether to return something.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's look at the purpose of a <strong>void</strong> function or as I like to refer to them, a <strong>subroutine</strong> and try to comprehend the utter confusion it can bring.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://americanreligioninamerica.files.wordpress.com/2014/03/direction_confusion.jpg" alt="Related image"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Is there a correct usage of Void?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Isn't the purpose of a function to return something?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>9 times out of 10</strong> a function will return something. Otherwise, it will do something. These two acts should be tangibly different, yet we group them together. This leads to horrific methods that are not pure, do multiple things and mutate global state, all under the watchful eye of Master Void.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://www.thoughtworks.com/insights/blog/how-programming-languages-have-evolved">Programming has come a long way</a>, yet we stick to this archaic practice that confounds me to this day. Now I'm not against the notion of functions doing something; This is what we want; The issue is the misuse of the void mechanism specifically.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When saving or deleting a record, is it better to return nothing, or should we return the object, boolean or an identifier of the record changed? Is explicitly checking for some value better than wrapping all that code in a try-catch block.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>One <strong>valid</strong> use case is <strong><a href="https://en.wikipedia.org/wiki/Log_file">Logging</a></strong>; where information is captured about the application. Another is an entry point like the main function or task runner whose only job is to run a set of functions synchronously.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Why are you using Void?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's start with global properties. My ultimate bugbear with void functions is changing global state. It happens far too often. <strong>Don't do it!</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The key design decision should be whether the function returns something? When creating a function ask yourself:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Does it throw an exception if there is an error? </li><li>Does it alter an object or some properties?</li><li>Should we wrap code in a try-catch block?</li><li>What happens when the function succeeds?</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Do we care?&nbsp;Is&nbsp;more&nbsp;information&nbsp;better&nbsp;or&nbsp;worse?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That's down to you, I guess what I'm trying to say is that <strong>using void functions can cause confusion. </strong>It requires the developer to <strong>dig deeper</strong> into code because externally the function doesn't disclose any information about its effects.</p>
<!-- /wp:paragraph -->

<!-- wp:quote {"className":"is-style-large"} -->
<blockquote class="wp-block-quote is-style-large"><p>I believe the only valid use case of a void function is a fire and forget call. So long as it didn't error, you don't care what it did.</p><cite>Me, hater of the misused void.</cite></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>A void call shouldn't stop the rest of the code running successfully. It's not required for the rest of the function to do its job.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Are you sure it doesn't do something you want to know about?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is the first question I as myself when looking at a void function. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>It should be yours too!</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>My aim is not to stop people from using void functions. They are an intrinsic part of how software is developed and consumed, I just urge you to use them when necessary. <strong>You wouldn't save a number into a string field.</strong> </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Don't use void without first thinking about the consequences of doing so.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://i.imgflip.com/3fbwft.jpg" alt="Roll Safe Think About It Meme | THINK... IS THIS REALLY A VOID FUNCTION? | image tagged in memes,roll safe think about it | made w/ Imgflip meme maker"/></figure>
<!-- /wp:image -->

I'd love to hear your views on this, counter arguments are most welcome!