I read on StackOverflow that the Compiler doesn't care about the underscore
That's not quite true. Implicit line continuations are a fairly recent addition to the Visual Basic language1. They cover many, but not all, scenarios where the _
line continuation character used to be needed.
Here's the complete list from linked documentation:
- After a comma (
,
).
- After an open parenthesis (
(
) or before a closing parenthesis ()
).
- After an open curly brace (
{
) or before a closing curly brace (}
).
- After an open embedded expression (
<%=
) or before the close of an embedded expression (%>
) within an XML literal.
- After the concatenation operator (
&
). For example:
- After assignment operators (
=
, &=
, :=
, +=
, -=
, *=
, /=
, \=
, ^=
, <<=
, >>=
).
- After binary operators (
+
, -
, /
, *
, Mod
, <>
, <
, >
, <=
, >=
, ^
, >>
, <<
, And
, AndAlso
2, Or
, OrElse
, Like
, Xor
) within an expression.
- After the
Is
and IsNot
operators.
- After a member qualifier character (
.
) and before the member name.
This shows we can safely add lines after AndAlso
rather than before:
If Not variable Is Nothing AndAlso
Not Results.Tables.Count = 0 AndAlso
Not Results.Tables(0).Rows.Count = 0 Then
I also suggest rewriting as follows, because VB.Net does odd things with Not
, using it as a bitwise rather than logical operator3:
If variable IsNot Nothing AndAlso
Results.Tables.Count > 0 AndAlso
Results.Tables(0).Rows.Count > 0 Then
1. Okay, 2010, but I still find a lot of VB developers who don't know about them, or know about them but never check the rules and either avoid them or let the compiler tell them when to add one.
2. Emphasis mine.
3. Plus, I just find it easier to read without the negations.