Put it in an if-else and it executes both blocks.
Fun fact I learned today - you know how when there’s a compound conditional, the interpreter stops once the result is known? (Eg, if the left side of an and is false, it’s false so it doesn’t bother checking the second condition)
Apparently, visual basic doesn’t do this thing every other language I know of does… It might be a debug only thing for the convenience of the depreciated ide I’m forced to use, but I did a null check && called a function on it if it’s not null, and it blew up
I pride myself on my ability to change to a new programming language and make progress on day one, but vb is truly the most disgusting POS language I’ve ever seen. From syntax to jarring inconsistencies in language design, it’s just gross
-
That’s behaviour that’s just part of language design. If you rely on it you should probably check how the language you’re using handles it.
-
relying on that behaviour sounds a lot like “clever” (read unnecessarily unreadable) code
Are you serious? It’s one of the most basic and common if statements that exist.
If( foo != null && foo.isBar() )
That’s what we’re talking about. Looking before you leap.
I take issue with the whole “too clever” argument fundamentally (for a number of reasons), but this isn’t some fancy quality of life feature. This is as simple as it gets
Screw else statements, people who use if-return have 180% more readable code.
Because everyone knows a function stops at the if-else. Nothing ever happens afterward.
You’re writing extremely bad code if that’s the case and you need to refactor. The point of a function is to return a value. Anything else is just there to waste cycles and make the code less readable. You should also never use else statements for arithmetic due to their massive relative overhead. Your processor can do multiple arithmetic operations in the time it takes to process one if statement, and don’t get me started on people who demand you use nested if even though switch statements are way faster and leagues more readable.