Click here to Skip to main content
15,888,984 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionHow To Get Time of " When User Closing The Browser"..? Pin
Member 813497918-Oct-13 0:31
Member 813497918-Oct-13 0:31 
AnswerRe: How To Get Time of " When User Closing The Browser"..? Pin
Eddy Vluggen18-Oct-13 3:00
professionalEddy Vluggen18-Oct-13 3:00 
AnswerRe: How To Get Time of " When User Closing The Browser"..? Pin
Marco Bertschi18-Oct-13 3:36
protectorMarco Bertschi18-Oct-13 3:36 
AnswerRe: How To Get Time of " When User Closing The Browser"..? Pin
Abhinav S18-Oct-13 22:43
Abhinav S18-Oct-13 22:43 
Question[VB.NET 2008] SerialPort and UART framing error Pin
steve_949661317-Oct-13 23:29
professionalsteve_949661317-Oct-13 23:29 
AnswerRe: [VB.NET 2008] SerialPort and UART framing error Pin
Marco Bertschi18-Oct-13 0:05
protectorMarco Bertschi18-Oct-13 0:05 
GeneralRe: [VB.NET 2008] SerialPort and UART framing error Pin
steve_949661318-Oct-13 2:25
professionalsteve_949661318-Oct-13 2:25 
QuestionDiscussion C# / VB.NET: Short-Circuit Evaluation Is Required Pin
Suamere Scalar17-Oct-13 10:06
Suamere Scalar17-Oct-13 10:06 
Disclaimer: I forsee a lot of disagreement in terms of legacy code. This is only pertinent to Green or Refactoring Projects.

Assuming we're working with S.O.L.I.D. Object-Oriented Design, when would NON Short-Circuited Eval be acceptable? I assert that it is not acceptable, but need dicsussion points of agreement or disagreement.

There are obvious times that Lacking Short-Circuit Eval is Bad and should be fixed no matter what:
VB
'VB Example:
If FirstName.Length > 5 And FirstName(5) = "f"c Then
    'This will break for lack of short circuiting
End If

C#
//C# Example:
if (FirstName.Length > 5 & FirstName(5) == 'f') {
    //This will break for lack of short circuiting
}

The above conditional evaluation should instead be AndAlso to short-circuit the checking of the sixth (index: 5) character. But what about this example?
VB
'VB Example:
If CaseClass.ParseCase(Data) And ClientClass.ParseClient(Data) And CourtClass.ParseCourt(Data) Then
    'Do Something
End If

C#
//C# Example:
If (CaseClass.ParseCase(Data) & ClientClass.ParseClient(Data) & CourtClass.ParseCourt(Data)){
    //Do Something
}

The above code isn't automatically clear to anybody that: if all three methods don't run, code later on down the line will not be able to do its job.

However, we have some code which we only want to evaluate on the condition that the result of those Parse methods all return TRUE. So the author of this code removed the Short-Circuiting.

In the above code, it's logical to remove the short-circuiting so all three methods evaluate, but the lack of clear objectives makes maintainence a nightmare. In fact, it then requires Comments to say "MUST EVALUATE ALL THREE, Don't Short-Circuit!", and requiring Comments means code is getting smelly.

So, if there is a Requirement to ALWAYS Short-Circuit, we are forced to do this:
VB
'VB Example:
Call CaseClass.ParseCase(Data)
Call ClientClass.ParseClient(Data)
Call CourtClass.ParseCourt(Data)

If CaseClass.Success AndAlso ClientClass.Success AndAlso CourtClass.Success Then
    'Do Something
End If

C#
//C# Example:
CaseClass.ParseCase(System.Data);
ClientClass.ParseClient(System.Data);
CourtClass.ParseCourt(System.Data);

if (CaseClass.Success && ClientClass.Success && CourtClass.Success) {
    //Do Something
}


Above, our objective is very clear that we want to run all three, and that we want to perform conditional work based on the success of the three runs. Thus, requiring Short-Circuiting also forced us to write code with clear objective.

Also, this requires us to write classes with single responsibility and clear objectives also, as well as follow other SOLID Practices.

I can't personally think of a time when NON Short-Circuiting conditionals were the right choice. As a matter of fact, most of the time I've seen NON Short-Circuiting evaluation, it ended up breaking or was obvious to break soon.

Last Note... one of my biggest Pet Peeves in VB is the use of the Non-Ternary IIF Function. Partially because it breaks this proposed rule of requiring Short-Circuiting, but mostly because I've never seen an IIF() That requires the use of its NON short-circuiting Behavior.
VB
'VB-Only Example:
Dim intTest As Integer = 0
Dim blnLMAO As Boolean = IIF(2=2, Integer.TryParse("3", intTest), Integer.TryParse("4", intTest))


What's the value of intTest? Well, since 2=2, we'd assume it's 3. Lawl, wrong. Just delete one of the I's and use the IF() Ternary Expression (not a function), it actually does what you expect it to. Also, IIF() Isn't Type-Safe, it returns Object, and is even less explicit in its objective. C# doesn't even have an equivilent.

In support of my reference, I site a 200+k super user on StackOverflow who had this answer. I didn't take part in this question, but found it while researching. However, while it advocates Short-Circuiting, it does not provide me with the discussion necessary to say that we REQUIRE Short-Circuiting in all future code: Short-Circuiting Advocation

So, anybody have any discussion points on why Requiring Short-Circuit Evaluation would/wouldn't be a bad Requirement?
There are only 10 kinds of people in the world: Those who don't get this, and those who have heard it too many times.

~Suamere

QuestionFind Out Installed Browser List in .net Pin
Member 813497916-Oct-13 20:50
Member 813497916-Oct-13 20:50 
AnswerRe: Find Out Installed Browser List in .net Pin
Eddy Vluggen17-Oct-13 0:28
professionalEddy Vluggen17-Oct-13 0:28 
GeneralRe: Find Out Installed Browser List in .net Pin
Member 813497917-Oct-13 1:20
Member 813497917-Oct-13 1:20 
GeneralRe: Find Out Installed Browser List in .net Pin
Pete O'Hanlon17-Oct-13 2:36
mvePete O'Hanlon17-Oct-13 2:36 
GeneralRe: Find Out Installed Browser List in .net Pin
Member 813497918-Oct-13 0:19
Member 813497918-Oct-13 0:19 
GeneralRe: Find Out Installed Browser List in .net Pin
Pete O'Hanlon18-Oct-13 0:38
mvePete O'Hanlon18-Oct-13 0:38 
GeneralRe: Find Out Installed Browser List in .net Pin
Dave Kreskowiak18-Oct-13 2:22
mveDave Kreskowiak18-Oct-13 2:22 
AnswerRe: Find Out Installed Browser List in .net Pin
Abhinav S17-Oct-13 2:03
Abhinav S17-Oct-13 2:03 
GeneralRe: Find Out Installed Browser List in .net Pin
Eddy Vluggen17-Oct-13 3:00
professionalEddy Vluggen17-Oct-13 3:00 
GeneralRe: Find Out Installed Browser List in .net Pin
Member 813497918-Oct-13 1:10
Member 813497918-Oct-13 1:10 
AnswerRe: Find Out Installed Browser List in .net Pin
Eddy Vluggen18-Oct-13 2:59
professionalEddy Vluggen18-Oct-13 2:59 
GeneralRe: Find Out Installed Browser List in .net Pin
Member 813497919-Oct-13 1:30
Member 813497919-Oct-13 1:30 
AnswerRe: Find Out Installed Browser List in .net Pin
Marco Bertschi18-Oct-13 0:09
protectorMarco Bertschi18-Oct-13 0:09 
QuestionBinding naviator Pin
Member 1026351915-Oct-13 19:12
Member 1026351915-Oct-13 19:12 
AnswerRe: Binding naviator Pin
Abhinav S15-Oct-13 19:54
Abhinav S15-Oct-13 19:54 
GeneralRe: Binding naviator Pin
Member 1026351931-Oct-13 2:18
Member 1026351931-Oct-13 2:18 
Questionhow to convert any web page into pdf by passing the url of the page on button click event Pin
Harishankar Maurya13-Oct-13 22:16
Harishankar Maurya13-Oct-13 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.