Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use a class or a method to wrap a dynamic remote procedure call?

Situation

I'd like to make an RPC interface easier to use. This is a custom interface so there is no readily available wrapper.

I have to write several wrappers around functions which often have many arguments.

Possible solutions

Solution 1 - Using a class for each function:

TDoSomethingFunction = class
public
  property Arg1: Integer;
  property Arg2: string;
  property Arg3: Boolean;
  procedure Run;
end;

The caller has to create an object to call the function:

var
  DoSomething: TDoSomethingFunction;
begin
  DoSomething := TDoSomethingFunction.Create;
  try
    DoSomething.Arg1 := 0;
    ...
    DoSomething.Run;
  finally
  free;
end;

Method 2 - Using a wrapper method for each function:

procedure TRPCInterface.DoSomething(AArg1: Integer; AArg2: string; AArg3: Boolean);

The caller can simply call it:

TRPCInterface.DoSomething(0, ...);

Pro and contra

Method 1 - Class for each function

Contra

  • More code required.
  • An object must be created which takes up memory.

Pro

  • Reading the code is easier, you don't have to look at the declaration to see what the arguments are.

Method 2 - Wrapper method

Contra

  • You can't tell which arguments are used by just looking at the code.

Pro

  • Much less code to write.
  • The wrapper is thinner (no object has to be created).

Which method should I use?

like image 610
Jens Mühlenhoff Avatar asked Oct 09 '22 17:10

Jens Mühlenhoff


1 Answers

There is an intermediate solution that is calling the wrapper methods passing an object argument.

TDoSomethingArgs = class
public
  property Arg1: Integer;
  property Arg2: string;
  property Arg3: Boolean;
end;

procedure TRPCInterface.DoSomething(Args: TDoSomethingArgs);

one advantage of this method is that you still use methods, but still it's more readable. One advantage of using classes (you can also use records) in arguments is that you can later change the arguments (add more, change behavior) and if you choose it well, it does not break backward compatibility - in summary you can change method signature without breaking code.

like image 90
landgraf.dev Avatar answered Oct 13 '22 09:10

landgraf.dev