Question

I have seen vb.net conditionals with and without parenthesis, If (foo = bar) Then or If foo = bar Then

So far I have been coding without them, as that seemed to be the convention in most examples and tutorials I've looked at (even on MSDN). However I realised that parenthesis would be needed to set precedence in more complex expressions If (foo = bar Or (bar < barMax And bar > barMin) Then

So is it recommended to use them consistently or only when needed?

Was it helpful?

Solution

It is down to your own preference.

Personally I would not use them in a single expression like this:

If foo = bar Then

But I would use them in something like this (for clarity/readability)

Dim boolValue = (foo = bar)

and this (to ensure that booleans are evaluated in the order I intended):

If (foo = bar) Or (foo2 = bar2) Then

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top