Board index » delphi » Re: D2005 Code Folding
Michael Anonymous
![]() Delphi Developer |
Michael Anonymous
![]() Delphi Developer |
Re: D2005 Code Folding2005-08-11 06:48:15 AM delphi101 Dave Nottage [TeamB] writes: QuoteBrion L. Webster writes: All he needs to do to make the code more robust is set a boolean value after calling ECS. This way he can check the value before freeing the critical section. |
Michael Anonymous
![]() Delphi Developer |
2005-08-11 06:50:28 AM
Re: D2005 Code Folding
Brian Moelk writes:
Quote
That's all he needed to do IMAO. |
Brian Moelk
![]() Delphi Developer |
2005-08-11 06:55:14 AM
Re: D2005 Code FoldingQuoteHe's not wrong ... QuoteAll he needs to do to make the code more robust is set a boolean value variable declaration. |
Michael Anonymous
![]() Delphi Developer |
2005-08-11 06:56:13 AM
Re: D2005 Code Folding
Jason Southwell writes:
Quote>I didn't miss the point, I understand exactly what he is saying. It ( If an exception was to occur execution would have exit the routine. ) Moreover, I believe it is a good practice of putting routines you think are going to fail in a try finally or a try except block. So, in this case, I think Allen was 100% correct in what he wrote. But I do believe he needs some more code in the try finally block to make sure he entered the critical section before freeing it. |
Brian Moelk
![]() Delphi Developer |
2005-08-11 06:56:24 AM
Re: D2005 Code FoldingQuoteAllen's code was basically correct and more future proof. QuoteI would personally check to make sure if it entered the critical section |
Ivo Bauer
![]() Delphi Developer |
2005-08-11 07:36:51 AM
Re: D2005 Code Folding
Allen Drennan napsal(a):
QuoteIf you ever called the try..finally block you actually protecting this ECS call itself and take a care of situations where this ECS call could raise an exception. But why? Instead, you have to protect the only code that executes *after* the critical section was acquired (i.e. after a successful call to ECS) in order to make sure this code does not throw any exceptions so that a call to LCS in finally clause can be guaranteed to occur in any case. Do you see the difference? QuoteWhen its within a Try..Finally block this can not happen since the -- Ivo Bauer Software Developer OZM Research, s.r.o. ________________________________________________ ModLink - MODBUS Messaging Components for Delphi www.ozm.cz/ivobauer/modlink/ ________________________________________________ |
Ivo Bauer
![]() Delphi Developer |
2005-08-11 07:36:56 AM
Re: D2005 Code Folding
Michael Anonymous napsal(a):
QuoteI would use something like this if it was that important: -- Ivo Bauer Software Developer OZM Research, s.r.o. ________________________________________________ ModLink - MODBUS Messaging Components for Delphi www.ozm.cz/ivobauer/modlink/ ________________________________________________ |
Ivo Bauer
![]() Delphi Developer |
2005-08-11 07:37:00 AM
Re: D2005 Code Folding
Michael Anonymous napsal(a):
QuoteI'm sorry but I do not think Craig's and Ivo's way was too great QuoteMoreover, I believe it is a good practice of putting routines you think trapping exception that the ECS call may throw. What you need here to accomplish is to protect the code executing *after* the critical section was entered/acquired in order to ensure that corresponding LCS call is not bypassed. -- Ivo Bauer Software Developer OZM Research, s.r.o. ________________________________________________ ModLink - MODBUS Messaging Components for Delphi www.ozm.cz/ivobauer/modlink/ ________________________________________________ |
Ivo Bauer
![]() Delphi Developer |
2005-08-11 07:37:03 AM
Re: D2005 Code Folding
Michael Anonymous napsal(a):
QuoteHis practice was a great practice IMAO. QuoteOnly an exception handling wizard would want to put the code outside of Ivo Bauer Software Developer OZM Research, s.r.o. ________________________________________________ ModLink - MODBUS Messaging Components for Delphi www.ozm.cz/ivobauer/modlink/ ________________________________________________ |
Michael Anonymous
![]() Delphi Developer |
2005-08-11 07:41:38 AM
Re: D2005 Code Folding
Brian Moelk writes:
Quote>Allen's code was basically correct and more future proof. Source: en.wikipedia.org/wiki/Future_proof Quote
if the ECS raises an exception, one's code in or after the try block might never be executed. |
Michael Anonymous
![]() Delphi Developer |
2005-08-11 07:41:58 AM
Re: D2005 Code Folding
Brian Moelk writes:
QuoteIn your code (in your other post), there is little difference between |
Eryk
![]() Delphi Developer |
2005-08-11 07:44:29 AM
Re: D2005 Code Folding
Michael,
Quote>Exactly. Allen is wrong. subverting by putting code in the wrong place: var Success : Boolean; sl : TStringList; begin Success := False; try sl := TStringList.Create; // Danger of out of memory exception. Success := True; DoStuff; finally if Success then sl.Free; end; end; ...is that code robust? Yes, for the most part. Is it 'good code'? No, it is a pointless workaround for the fact that: var sl : TStringList; begin try sl := TStringList.Create; // Danger of out of memory exception. DoStuff; finally sl.Free; end; end; ...is simply ASKING for an access violation. Is the problem the lack of a boolean variable? No. The problem is that the programmer should have written: var sl : TStringList; begin sl := TStringList.Create; // Danger of out of memory exception. try DoStuff; finally sl.Free; end; end; ...in the first place. Eryk PS: This reminds me of threads back in D1 days when 'exceptions' were a 'new idea' to many. "Twilight Zone" !! |
Michael Anonymous
![]() Delphi Developer |
2005-08-11 07:44:34 AM
Re: D2005 Code Folding
Ivo Bauer writes:
QuoteMichael Anonymous napsal(a): the code in or after the try block may never be executed. |
Ivo Bauer
![]() Delphi Developer |
2005-08-11 07:59:35 AM
Re: D2005 Code Folding
Michael Anonymous napsal(a):
Quote>Which is the same as putting ECS call in front of try..finally block, ECS call inside try..finally block and this ECS call raises an exception, the LCS inside the finally clause will get called, which is wrong (it should be called *if* and *only*if* the preceding ECS call succeeded). -- Ivo Bauer Software Developer OZM Research, s.r.o. ________________________________________________ ModLink - MODBUS Messaging Components for Delphi www.ozm.cz/ivobauer/modlink/ ________________________________________________ |
Michael Anonymous
![]() Delphi Developer |
2005-08-11 07:59:58 AM
Re: D2005 Code Folding
Eryk writes:
Quotevar it's worth take a few extra measure to insure that your program continues to run smoothly and not enter into some sort of deadlock. |