Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

The Perils of Debugging with Return Statements in Languages Without Semi-colon Statement Terminators, i.e., Swift

0.00/5 (No votes)
26 Jan 2016CPOL1 min read 3K  
The perils of debugging with return statements in languages without semi-colon statement terminators, i.e. Swift

This is a pretty obvious post but perhaps writing it will stop me falling prey to this issue.

When I'm debugging and I know that some code executed in a function is not to blame but is noisy in terms of what it causes to happen, etc. I'll often just prevent it from being executed in order to simplify the system, e.g.

func foo()
{
/*
f()
g()
h()
 
// Do lots of other things...
 
*/
 
}

Sometimes, I like to be quicker so I just put in an early return statement, i.e.,

func foo()
{
return
f()
g()
h()
 
// Do lots of other things...
}

I must also go temporarily warning blind and ignore the following:

Image 1

The effect of this is that rather than prevent everything after the return statement from executing it as per the warning, the return statement takes f() as its argument and explicitly calls it returning its value, though not executing the remaining functions. In this case, as foo() (and f() though it's not shown) is void, that is nothing. In fact if foo() or f() had non-void return types, this wouldn't compile.

The fix is easy. Just put a semi-colon after the return.

func foo()
{
return;
f()
g()
h()
 
// Do lots of other things...

}

I use this 'technique' when I'm debugging C++ where this works fine. This is slightly interesting as C++ has the same semantics. The following C++ code has the same problem as the Swift, in that this code also invokes f() as its return value.

void foo()
{
return
f();
g();
g();
 
}

I guess the reason it's not an issue with C++ (as much or at all) is that my muscle memory or something else is always wanting to terminate lines with semi-colons so the natural way to write the return would be 'return;' whereas in Swift without the semi-colon requirement, it's natural not to hence this issue becomes slightly more prevalent.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)