Fluent interface in Delphi

后端 未结 5 931
既然无缘
既然无缘 2021-01-30 17:25

What are the pros and cons in using fluent interfaces in Delphi?

Fluent interfaces are supposed to increase the readability, but I\'m a bit skeptical to have one

5条回答
  •  一生所求
    2021-01-30 18:08

    Are there any compiler issues?

    No.

    Are there any debugging issues?

    Yes. Since all the chained method calls are seen as one expression, even if you write them on multiple lines as in the Wikipedia example you linked, it's a problem when debugging because you can't single-step through them.

    Are there any runtime/error handling issues?

    Edited: Here's a test console application I wrote to test the actual Runtime overhead of using Fluent Interfaces. I assigned 6 properties for each iteration (actually the same 2 values 3 times each). The conclusions are:

    • With interfaces: 70% increase in runtime, depends on the number of properties set. Setting only two properties the overhead was smaller.
    • With objects: Using fluent interfaces was faster
    • Didn't test records. It can't work well with records!

    I personally don't mind those "fluent interfaces". Never heard of the name before, but I've been using them, especially in code that populates a list from code. (Somewhat like the XML example you posted). I don't think they're difficult to read, especially if one's familiar with this kind of coding AND the method names are meaningful. As for the one long line of code, look at the Wikipedia example: You don't need to put it all on one line of code.

    I clearly remember using them with Turbo Pascal to initialize Screens, which is probably why I don't mind them and also use them at times. Unfortunately Google fails me, I can't find any code sample for the old TP code.

提交回复
热议问题