Ruby and duck typing: design by contract impossible?

后端 未结 8 1522
别那么骄傲
别那么骄傲 2020-12-14 09:16

Method signature in Java:

public List getFilesIn(List directories)

similar one in ruby

def get_fi         


        
相关标签:
8条回答
  • 2020-12-14 09:41

    I would argue that although the Java method gives you more information, it doesn't give you enough information to comfortably program against.
    For example, is that List of Strings just filenames or fully-qualified paths?

    Given that, your argument that Ruby doesn't give you enough information also applies to Java.
    You're still relying on reading documentation, looking at the source code, or calling the method and looking at its output (and decent testing of course).

    0 讨论(0)
  • 2020-12-14 09:41

    It's by no means a maintenance nightmare, just another way of working, that calls for consistence in the API and good documentation.

    Your concern seems related to the fact that any dynamic language is a dangerous tool, that cannot enforce API input/output contracts. The fact is, while chosing static may seem safer, the better thing you can do in both worlds is to keep a good set of tests that verify not only the type of the data returned (which is the only thing the Java compiler can verify and enforce), but also it's correctness and inner workings(Black box/white box testing).

    As a side note, I don't know about Ruby, but in PHP you can use @phpdoc tags to hint the IDE (Eclipse PDT) about the data types returned by a certain method.

    0 讨论(0)
提交回复
热议问题