DEV Community

Cover image for Are you using Void correctly?

Are you using Void correctly?

Chris Bertrand on November 05, 2019

Vb.Net got something right. Its separation of the trusty function and the subroutine makes perfect sense. Unfortunately, that transition has not ma...
Collapse
 
aleksikauppila profile image
Aleksi Kauppila • Edited

Void is perfectly good return type. In OO your methods either return state or change state.

Those that return state, don't alter the state, and will have some reasonable return type.Those that change state, don't return anything.

Tbh i'm not a fan of the description of "doing something" presented in this post. Changing the state of object describes this better in my opinion.

Martin Fowler has a post about this subject.

Edit: Failures to do that "void" operation is communicated through exception.

Collapse
 
chris_bertrand profile image
Chris Bertrand

The CQRS pattern has been around for a while and is gaining a lot of traction in our new microservices, event sourcing, multi tenancy environments. It's definitely a valid use case. Equally it adds a whole level of complexity.

Collapse
 
aleksikauppila profile image
Aleksi Kauppila

Many seem to mix CQRS (the architectural pattern) to this lot simpler practice that Martin describes. Check the post again 😊

Thread Thread
 
chris_bertrand profile image
Chris Bertrand

They're very much related though. Interestingly if you look at what he says, He in fact determines that changing state and returning values should be deemed as two separate operations.

"It would be nice if the language itself would support this notion. I could imagine a language that would detect state changing methods, or at least allow the programmer to mark them. "

This one could argue is Martin himself notioning that using a void function should be called a "command", and a standard function that returns an object is called a "query". I'd go along with that, and a way of the compiler enforcing that rule would be even better!

"The really valuable idea in this principle is that it's extremely handy if you can clearly separate methods that change state from those that don't. This is because you can use queries in many situations with much more confidence, introducing them anywhere, changing their order. You have to be more careful with modifiers."

This is where the problem lies, your return type functions can still change global state, and this is where I feel we are not enforcing enough rules around what a function "should/is allowed" to do.

Thanks for sharing your views, I appreciate the healthy debate!

Collapse
 
somedood profile image
Basti Ortiz

I think this is a very interesting take on something I never really thought about. I consider myself to be quite strict about the semantics of code, so this comes as a surprise that this has never crossed my mind.

If I may ask, what may be the other valid use cases for void calls besides logging?

Collapse
 
chris_bertrand profile image
Chris Bertrand

Thanks, It drives me up the wall.

There really aren't many valid use cases. Any IO operations which may include logging.

Then any entry points to an app. Eg the main() func, setting global config, automapper, DI or task runners such as Grunt, Gulp etc and Test Frameworks. In principle only anything super high level, and things that could affect the global state.

Collapse
 
somedood profile image
Basti Ortiz • Edited

By "entry points to an app", am I correct to assume that these include event listeners?

Also, just a tiny nitpick: doesn't the main function call ideally return an integer denoting whether or not the program was successful?

Thread Thread
 
chris_bertrand profile image
Chris Bertrand

Yes that would be right! The main function call can be either, and it's down to the user to decide which to use. I would recommend it returning an int, but then I think you would have guessed that already 😊

Collapse
 
zyzmoz profile image
Daniel Cunha (he/him)

I got your point. I do agree that we (developers) should think exactly what the function will perform to assign the right return. Although, I also think it's a kind of extremism say that void isn't recommended at all! I came from a Java background, so there are principles I learned that justify the use of void functions like simple setters! Mostly if we don't get the right idea of some structure or practice, it lead us to a misconception of it!

Btw, well-elaborated article!

Collapse
 
chris_bertrand profile image
Chris Bertrand • Edited

Setters are also valid. I'd lump that with global config/state. The exceptions to the rule (there are no rules) 😅are minimal and the ability to misuse void are much higher in my experience. Thanks for adding to the discussion.

Collapse
 
chris_bertrand profile image
Chris Bertrand

I think functional programming follows this practice to an extent so that connection makes sense. I really think they should be labelled differently, they do different things. What that label would be though would be harder to reach a consensus on.

Collapse
 
aleksikauppila profile image
Aleksi Kauppila

Wow, nice touch attacking some junk i wrote few years ago while trying to learn some basic stuff 😂👍

Thanks for taking part to the discussion. Internet’s so cool.

Collapse
 
theskaf profile image
Θεόδωρος Σκαφιδάς ♫

Delphi is still alive you know. Like procedures and functions?

Collapse
 
chris_bertrand profile image
Chris Bertrand

And Pascal! And Yes, they separate this concern too!

I love this description from the DelphiBasics site

"Delphi provides 2 types of subroutine - Procedures and Functions. Functions are the same as procedures except that they return a value in addition to executing statements. A Function, as its name suggests, is like a little program that calculates something, returning the value to the caller. On the other hand, a procedure is like a little routine that performs something, and then just finishes."

I'll update the post, thanks for the reminder!

Collapse
 
theskaf profile image
Θεόδωρος Σκαφιδάς ♫

You are welcome. Good read btw :)

Collapse
 
syn profile image
norman

why would you like to advoid me?

Collapse
 
chris_bertrand profile image
Chris Bertrand

Haha, how could anyone avoid that charm!