Jump to content
Sign in to follow this  
Mike Torrettinni

Simple list processing example

Recommended Posts

I have often simple cases where I need to process a short list. So, I'm trying to come up with an example method that is simple, new values are easy to add and is just overall clean method.

Here I have 3 examples of FixOddIssues: replace OldValue with NewValue in string. Starts with replacing 'FACTORXYZ' with 'REFACTORING XYZ', and I will probably add 3-5 more cases.

 

// USING ARRAY
procedure FixOddIssues(var aSQL: string);
type
  TIssue = record
    OldValue: string;
    NewValue: string;
  end;
var
  vIssue: TIssue;
  vIssues: TArray<TIssue>;
begin
  // change all issues
  vIssue.OldValue := 'FACTORXYZ'; vIssue.NewValue := 'REFACTORING XYZ';
  vIssues := vIssues + [vIssue];

  for vIssue in vIssues do
    aSql := StringReplace(aSQL, vIssue.OldValue, vIssue.NewValue, [rfReplaceAll]);
end;

// USING 2 CONSTANTS
procedure FixOddIssues2(var aSQL: string);
const
  cElements = 1;
  cOldValues: array [1..cElements] of string = ('FACTORXYZ');
  cNewValues: array [1..cElements] of string = ('REFACTORING XYZ');
var
    i: integer;
begin
  // change all issues
  for i := Low(cOldValues) to High(cOldValues) do
    aSql := StringReplace(aSQL, cOldValues[i], cNewValues[i], [rfReplaceAll]);
end;

//USING CONSTANT ARRAY
procedure FixOddIssues3(var aSQL: string);
type
  TIssue = record
    OldValue: string;
    NewValue: string;
  end;
const
  cElements = 1;
  cValues: array [1..cElements] of TIssue = ((OldValue: 'FACTORXYZ'; NewValue: 'REFACTORING XYZ'));
var
    i: integer;
begin
  // change all issues
  for i := Low(cValues) to High(cValues) do
    aSql := StringReplace(aSQL, cValues[i].OldValue, cValues[i].NewValue, [rfReplaceAll]);
end;

Any better suggestions, easier solutions?

 

Edited by Mike Torrettinni

Share this post


Link to post
Guest

I would go with constant array anytime, now on other hand i suggest to go with little refactored version, let FixOddIssues3 accept two parameter instead of one, pass the array of your so called TIssue array along with aSQL, for two reason:

1) having this needed now, so there is chance you will need another one in the future, hence no need to refactor later, on contrary your lists of issue will be hardcoded in two separated methods.

2) when you will find better solution, you will have to change only one small procedure.

 

You can go will dynamic array too, after all this is up to you.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×