Funky code of the day

Funky code of the day

type
  ICalculator = interface(IInvokable)
    function Add(a, b: Integer): Integer;
  end;

...

var
  calculator: ICalculator;
begin
  calculator := Mock.Create;
  calculator.Add(Arg.IsAny, Arg.IsAny).Returns(
    function(const x: TCallInfo): TValue
    begin
      Result := x[0].AsInteger + x[1].AsInteger;
    end);
  CheckEquals(3, calculator.Add(1, 2));
end;

Comments

  1. My old school team mates would rip my hat of and sh*t into my neck if I'll give them such utterly complex implementations for a simple addition... :-D

    ReplyDelete
  2. Udo Sommer If it just were an addition and not the inline definition of the behavior of a mock which you otherwise would have to write a class for (and implement all the other methods that ICalculator would probably have but are irrelevant for this particular test).

    ReplyDelete
  3. Stefan Glienke
    Don't get me wrong, I have seen this. But they would ask "Why? Simply implement a class.".

    Different generations and colliding worlds.

    I like funky things. Especially if I don't have to write more code than needed.

    ReplyDelete
  4. Udo Sommer I am not arguing with straw men ;)

    ReplyDelete
  5. Perhaps it is the example which bends the mind to think that this is a too complex solution for a simple problem.  It's also appears to be error prone as you do an explicit cast in the argument retrieval.

    ReplyDelete
  6. Lars Fosdal I used the same example as in the NSubstitute wiki
    And what else can the arguments be if the signature takes two integers?

    ReplyDelete
  7. Better type deduction for generic parameters would likely make this a lot neater. Oh one can wish...

    ReplyDelete
  8. That is, I like the idea, but just looks clunky compared to what it could have been.

    ReplyDelete
  9. Asbjørn Heid oh yes, I fully agree with that. But I doubt they will make it any easier to read soon. Which means: deal with it and the compiler bugs that go with it (:

    ReplyDelete

Post a Comment