One more thing.
If you are using code like the CheckForAlphaInArray
that you posted then I believe you have a problem
if you run into an error situation.
In your example, you simply show an error message and then exit the procedure.
With this technique the next line of code in the calling procedure will continue with unpredictable results
given the failure.
You should be raising an exception so that the whole process stops.
As you said you would like to find some Delphi classes, I thought you would not mind if I point out
a few other guide lines that your posted procedure brought to mind.
For anyone else following this, I did say guide lines not rules. There are plenty of situations
where we would all ignore them for very valid reasons.
Here are my suggestions with comments
- Delphi Code
//check if non-numerical (alphabets) are in the data
procedure THillSlopeRegr.CheckForAlphaInArray(I: integer; S: string);
for J := 1 to Length(S) do
if not CharInSet(S[J],['0'..'9', '.']) then
Raise Exception.CreateFmt('Error! Your file has non-numerical data in row #%d!', [I]);
1. In such an easy to follow procedure there is no need to define the variables
Chr and Numeric that are used only once.
2. By using the negated boolean if not
you avoid the need to use continue
Jumping to the top of a loop half way through (especially as the normal) is both confusing to follow
and a source of possible bugs when something further down the loop, like an incrementation, should still have happened.
3. Raising an exception ensures that code jumps immediately to the first exception clause and no other code in between executes.
4. By using Exception.CreateFmt (instead of Exception.Create)
you avoid having to convert I
into a string and do all the concatenation.
I hope this helps.