Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

vb.net code

Hello,
Is there a better way of writing this code.
 If IsNothing(SQLobj.Connection) = True Then
                TrakSQL.OpenConnection()
            ElseIf (SQLobj.Connection.State) = ConnectionState.Closed Then
                TrakSQL.OpenConnection()
            End If

Open in new window


Cheers
Avatar of Allan Nisbet
Allan Nisbet
Flag of United Kingdom of Great Britain and Northern Ireland image

HI

Try this

If SQLobj.State <> ConnectionState.Open Then
     TrakSQL.OpenConnection()
End If

Open in new window


hope this helps
Avatar of RIAS

ASKER

Thanks Allan,
But I need to check if there is no connection at all i.e Nothing .
The code is correct just need a better way of writing it.

Cheers
Avatar of Dorababu M
I think you can use OrElse condition in this case

If (IsNothing(SQLobj.Connection) = True OrElse (SQLobj.Connection.State) = ConnectionState.Closed) Then
TrakSQL.OpenConnection()
End If

Open in new window


Correct accordingly
hmm, you should ask the author of TrakSQL.

Cause the magic assignment of an connection to an arbitrary object via TrakSQL.OpenConnection() violates the law of Demeter and the principle of least astonishment.

Cause even an unconditional TrakSQL.OpenConnection() maybe used here.. and may perform well enough.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Thanks
Well, you're aware that you have a hidden, strong coupling here? Which is bad? Really, really bad?
Avatar of RIAS

ASKER

What code you recommend?
The problem is that hidden coupling. Without knowing what TrakSQL.OpenConnection() does and how it magically assigns SQLobj an active connection, any hint is imho useless and/or dangerous.

One case I can imagine: The usage of TrakSQL.OpenConnection() is to ensure that all connections point to the same origin. Then using a conditional approach would break the guard-function of that call.

To be precise: The semantics implied by that short sample tell me, that there should be never an explicit test. Just call TrakSQL.OpenConnection() a let it do its magic.

Otherwise we would have a call like TrakSQL.OpenConnection(SQLObj) to optionally assign a connection and then open the connection.

It has just a terrible strong code smell..