RIAS
asked on
vb.net code
Hello,
Is there a better way of writing this code.
Cheers
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
Cheers
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
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
I think you can use OrElse condition in this case
Correct accordingly
If (IsNothing(SQLobj.Connection) = True OrElse (SQLobj.Connection.State) = ConnectionState.Closed) Then
TrakSQL.OpenConnection()
End If
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
Well, you're aware that you have a hidden, strong coupling here? Which is bad? Really, really bad?
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(SQL Obj) to optionally assign a connection and then open the connection.
It has just a terrible strong code smell..
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(SQL
It has just a terrible strong code smell..
Try this
Open in new window
hope this helps