Rob Cameron wrote: > Questions: I want to warn myself of potential errors in my own code.
Unit tests. > Have I > missed a feature in the Delphi IDE? If not, is there a better of way of > flagging > up possible problems? > ------------------------------------------------------------------------------ > > Detail: > > Obviously I have collected over time a bunch of home-made methods that solve > an > immediate problem and will likely be useful again in the future; I put all > these > in a set of units which I cite in the uses clause of a unit as needed. > > However, from time to time these methods are not as complete or rigorous as I > would like - but hell, you know, the wolves are at the door, the clients are > getting uppity. But I do worry that a couple of years down the line I'm > going > to forget the limitations and trip myself up by using the code inapproriately. > > This has come to the fore again as I start to migrate projects to D2009 and > notice possibilities, e.g. in the change from 1 byte to 2byte chars, ASCII to > Unicode and all that: I've got a lot of useful string handling routines that > may > not work if the full range of Unicode strings was used. Right now I think > that > I'll not need to use the full range - but who knows what I'll need in a few > years? I don't have the time or inclination at the moment to thoroughly test > all > the possibilities - and in fact I'm not sure I know what all the > possibilities > may be. To paraphrase Rumsfeld: "I don't know if there are things I know or > don't know". > > So, I want to build-in to some of these methods a warning to myself to be > careful how I use them. I have started adding unneccesary string parameters > to > the method definitions, something like this: > > function MyStrTest(TextIn: string; Hey_ASCII_Chars_Only: string = ''): > boolean; If it doesn't work with Unicode, then declare the parameter as an AnsiString. In Delphi 2009, you'll get a warning when you pass a UnicodeString for that parameter. ASCII is another matter. There's no way to get a warning for that one. It's a subset of a lot of other encodings Delphi supports. If you can show that the function works with UTF-8, then ou can declare the string as a UTF8String. In Delphi 2009, you should get an automatic, lossless conversion between that and UnicodeString. (Also, unless you have to modify the string parameters within the function, declare those parameters as const. It reduces the amount of prologue and epilogue code the compiler inserts to manage the strings' reference counts. This is a general piece of advice, not specific to the question at hand.) > This is ugly and inefficient but it has three advantages: > > - the declaration and implementation both carry a visible warning; > - the warning shows up when the code completion hint displays, so I am > reminded > every time I use the method; > - the unneccessary parameter is optional and so doesn't need to show up in > the > main body of code, as in: > > if MyStrTest(SomeString) then ... > > although I can use it if I want to, as in: > > if MyStrTest(SomeString, '!!% Hey - be careful with this method') then ... > > and if I am disciplined about using key sub-strings such as 'Hey_' or '!!%' I > can quickly search my projects for the warnings. You can also just tell the IDE to find all the places you call that function. Delphi 2005 and later have the "find references" command. Another thing you can do is simply remove the second parameter from the function declaration and then let the compiler errors tell you all the places that try to pass an extra parameter. > The To-do-list could partly solve the problem, but it doesn't show up when I > am > actually using the methods. > > Does anyone have a better solution, or do you all write perfect code all the > time? :-) > > Rob > _______________________________________________ > Delphi mailing list -> [email protected] > http://lists.elists.org/cgi-bin/mailman/listinfo/delphi -- Rob _______________________________________________ Delphi mailing list -> [email protected] http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

