Welcome to the SRP Forum! Please refer to the SRP Forum FAQ post if you have any questions regarding how the forum works.
Can you detect this error?
I wrote something like the following code, and it resulted in a bunch (thankfully only several dozen) of records being corrupted. This kind of error can't happen in other language I have used. I was wondering if SRP or the OI compiler could check for this? I have VNAV checking on.
In case it's not obvious, the problem is that I am assigning a variable after the return statement. It should be before.
validate:
if 1 then
return
valid = 0
end
return
In case it's not obvious, the problem is that I am assigning a variable after the return statement. It should be before.
Comments
I'm curious, what specifically couldn't happen in another language?
@josh, as an expert in language compilers, I can tell you that BASIC+ makes detecting things like this a chore. Revelation doesn't provide any services to break down the language into a formal syntax tree, and writing one for a language like this is not easy. I've tried, for fun, a couple times. Once we can break down the language into a formal syntax tree, we can start to do analysis like this, but we are talking months of work. Given our work load, I'm just not sure when such a feature can happen.
Don't get me wrong, I would love to have something like this, along with other things like inline errors, better code complete, smart autofill dropdowns... the list goes on.
@donbakke
" I'm not a fan of using the return statement to force the end of a gosub like this, but it is valid."
Really, so you must have very nested code, which is hard to read and understand.
Not necessarily. There are ways of avoiding "very nested code". The best way is to modularize code so that it isn't one big wall.
That said, I would rather read nested code that has a singular exit point than code that just prematurely aborts. For me, the latter this is much harder to follow and debug. I admit that I probably am in the minority in this view.
Alas, if only I had more hours in the day. :)
As for the return statement... this is a long standing debate in the programming world. One camp insists on one Return per block, others insist that you should return as soon as possible for performance reasons. I straddle both fences. In BASIC+, I don't like multiple returns, but in C#, I'll often have several in a routine.
I'll back you up on that, Don ;). Having one point of exit allows you to define and have better control of the exit conditions. Especially important as the code evolves in future development, with more variables and exit condition to cater for. I'll admit to on rare occasions using a GoTo straight to end of the routine where exit conditions can be dealt with.
It's that two logically connected pieces of the code (the then and the else clauses) are written far away from each other. In other words, it's hard to know what then clause an else clause is connected to if the else clause is separated from the then clause by lines and lines of nested code.
I think you'll find everyone is in agreeance on this point. That's the meaning of Don's earlier comment
That said, usually when I find examples of Return statements being littered through out code, it isn't to avoid the "nested code" dilemma, but rather to avoid the need to add a few more lines of code to maintain a single exit point. That is, it is purely for convenience.