Bug of the week #1 — return void

Chahat Gupta
2 min readAug 5, 2023

--

Bug fixing stories are as entertaining as the Harry Potter series — the canvas is dark, the characters are confusing, and there’s a lot of witchcraft. 🪄

I have been thinking about this for a long while. Why not share stories of bug fixing as blog posts? As much as it can be entertaining, I believe many of us will benefit from this. I’m starting this series with a very notorious one — ‘return void’ in Dart.

Take a look at this innocent piece of Dart code:

bool shouldInitializeSdk = true;

void checkAndInitializeSdk() {
if (!shouldInitializeSdk) return;
initializeSdk();
}

void initializeSdk() {
// some code
}

Void functions have a problem — you may or may not return something that is void. So, if you happen to forget adding the tiny semicolon (;) after the return statement on line #4, the code will still compile (and not work, of course) because you are returning another void instead of executing it.

This is the buggy version of the same Dart code:

bool shouldInitializeSdk = true;

void checkAndInitializeSdk() {
if (!shouldInitializeSdk) return
initializeSdk();
}

void initializeSdk() {
// some code
}

Both the versions are syntactically correct and will compile happily but only the first one would work as expected in this case. Even adding a debugger to the scene will only confuse you more.

How to spot these void errors?

One way is to look for any weird indentation that your IDE is showing. Another way is to not be a noob (like me).

I have many more interesting short stories to share. Stay connected to read more of these. Let’s meet in another blog. Happy coding!

(just another meme for the feature image)

--

--

Chahat Gupta
Chahat Gupta

Written by Chahat Gupta

Mobile Tech Lead specialising in Android, iOS, and Flutter. Sharing insights and learnings on mobile development to inspire and elevate tech professionals.

Responses (3)