>The code performs a redundant test. Like I wrote in my previous message, >the above function works identically to this:
>procedure Foo(F: TForm); >begin > if F is TMyForm then begin > if F is TMyForm then > TMyForm(F).Test := 0 > else > raise EInvalidCast.Create(...); > end; >end; >procedure Foo(F: TForm); >begin > if F is TMyForm then > (F as TMyForm).Test := 0; >end; >This function will never fail with an exception. If its input is not an >instance of TMyForm, the function will simply return to its caller as >though nothing happened. I consider that a bad thing. The premise of >this exercise is that it is an error in the program if this function >gets called on non-TMyForm objects. But the way this function is >writte Rob is right about two tests. And for the record, I stated not once but TWICE that an assert should be used in an else stmt to help the maintainers of the code. If I thought that such an error might escape development and function\regression testing then I would include exception handling or other error handling. But my goal in this case is to write code that if it becomes broken, that same code can help the person who broke it to quickly locate his or her error. And I do so without setting up unnecessary exception handling which is, I expect, much more expensive than several IS tests. If it wasn't for the compile time benefit of the AS statement and the fact that it is easier to read (due to its more explicit nature) I would never use it. It adds little or no benefit at runtime. Like Rob said, Hard casts and AS gives us two exceptions that we don't have much reason to catch. Just trading one exception for another. But I will gladly give up a couple ms time if it makes the code easier to read. The AS statement is easier to read, especially in large complicated statements. It EXPLICITLY says "I am doing a cast". If TMyForm(FThisIsALongVarName) gets buried in a large enough statement, the fact that a cast is occurring might not be easy to see. It can require careful study of the parens. Especially if there are two or three more paren groupings in the stmt. (one might argue that we shouldn't write such stmts, but we've all seen them) Furthermore, the compile time testing AS gives us also makes AS very useful. And it continues to be useful as the code evolves. I rarely do casts like TMyObject(obj) any more. There is a reason that IS and AS operators are being added to today's languages. It is also a mistake to design code to save a couple ms if saving that time will not matter to the user and the code becomes less readable as a result. Time takes a high priority where it matters. It should take little priority where it doesn't. In event driven windows programming, adding a couple ms to some user event is not usually an issue. Most operations occur such that an additional couple ms won't matter. But if I can write a stmt in 10 seconds that might save some other programmer 20 minutes a year from now, that matters. That is why I posted to this thread in the first place. I am not likely to do that again. Thanks Tom ____________________________________________________________________________________ TV dinner still cooling? Check out "Tonight's Picks" on Yahoo! TV. http://tv.yahoo.com/ _______________________________________________ Delphi mailing list -> [email protected] http://www.elists.org/mailman/listinfo/delphi

