Write all these if statements

I have a problem when I move on to adding more "names" to if statements. It's hard for me to see if everything is ready there. So there is a cleaner way to write this where I can easily see what names are all read there?

    function TfDB.GetW(name: string) :integer;
  begin
     result := 0;
    if (name = 'Destacker') or (name='Router') or (name = 'Turn Table') Then
        result := 57;
    if (name = 'Laser Marker') then
        result := 66;
    if (name = 'SP28')OR(name='Chamber') OR (name = 'CM402') OR (name = 'SP60') then
        result := 65;
    if (name = 'Conveyor') OR (name = 'Slide Gate') OR (name = 'Washer') then
        result := 51;
    if (name = 'BTU') OR (name = 'Furukawa') OR (name = 'Radial') OR (name = 'HDP') or (name = 'MSR') OR (name = 'Koki') Or (name = 'MSF') then
        result := 98;
    if (name = 'Buffer')OR (name = 'Reclaimer') OR (name = 'ECO Roller') then
        result := 49;
    if (name = 'Inverter') or (name = 'CNC') then
        result := 42;
    if (name = '3-D Check Station') or (name='Screw Machine') or (name='VT-Win') or(name='Component Viewer') then
        result := 58;
    if (name = 'AOI Panel') or (name='AirBlow') then
        result := 42;
    if (name='Mag Loader') or (name='Soltec') then
        result := 73;
    if (name='Tester') then
        result := 33;
    if (name='LoadBox') then
        result := 17;
    if (name = 'DeltaWave') then
        result := 89;
    if (name = 'ScrewFeeder') then
        result := 25;
    if (name='Pump') then
        result := 33;

    //if result is 0 show message error.

  end;

      

+3


source to share


3 answers


You can create a dictionary TDictionary<string, Integer>

and store it in a global variable. Load it with name width mapped at initialization time. And then your function becomes a one-liner.



var
  WidthMapping: TDictionary<string, Integer>;
....
function TfDB.GetW(name: string) :integer;   
begin
  if not WidthMapping.TryGetValue(name, Result) then
    ... handle error condition
end;
....
initialization
  WidthMapping := TDictionary<string, Integer>.Create;
  WidthMapping.Add('Destacker', 57);
  WidthMapping.Add('Router', 57);
  ... etc.
....
finalization
  WidthMapping.Free;

      

+10


source


Yes, don't use a statement if

, but an array and a loop:

const
  NAME_RESULT: array [1..2] of record
    Name: string;
    Value: Integer;
  end = (
    (Name: 'whatever'; Value: 57)
  , (Name: 'Something else'; Value: 57)
  );
var
  i: Integer;
begin
  Result := 0; // or whatever you need your default to be
  for i := Low(NAME_RESULT) to High(NAME_RESULT) do
    if SameText(name, NAME_RESULT[i].Name) then
    begin
      Result := NAME_RESULT[i].Value;
      Break;
    end;
end;

      



Added benefit: you don't have to store names that return the same values ​​together, but can sort the list alphabetically.

+5


source


Create an array (or a dynamic array if you want to add more names without any problems with the array sizing) of strings containing all the names you want to check (here I assumed a fixed size string array):

    var test = array [1..50] of string;
    a[1]:='Destacker';
    a[2]:='Router';

      

and etc.

In your test procedure, you can use the case keyword like this:

    function TfDB.GetW(index: integer) :integer
    begin
     result:=0;
     case index of
      1,2,3: result:=57;
      4: result:=66
     end
    end;

      

It seems to me that it is easier.

+1


source







All Articles