Should I define interfaces in Duck Typed languages?

前端 未结 4 1922
清酒与你
清酒与你 2021-02-05 13:31

I\'m just about to write my first application in a duck typed language (Groovy).

If I was to write the same application in a static typed language then I would need to d

4条回答
  •  情书的邮戳
    2021-02-05 13:58

    I'm not familiar with groovy, but in general, no, you don't need to define interfaces in loosely typed languages.

    1. You would be repeating yourself, if you need to change a methods signature, then you need to do it in two places, not one.

    2. Although interfaces do have some use as documentation, in a loosely typed language, most coders will not expect an interface, and therefore will not go searching for an interface if they need documentation.

    3. Most dynamic languages have good IDE's available for them, with method completion, which further diminishes the need for a separate interface.

    4. Methods can be bound and unbound in dynamic languages. Therefore, you can, and probably will, end up with objects that do not adhere to the interface. Having a separate interface could end up confusing people reading your code.

提交回复
热议问题