Why is tightly coupled bad but strongly typed good?

后端 未结 9 531
日久生厌
日久生厌 2021-02-04 13:30

I am struggling to see the real-world benefits of loosely coupled code. Why spend so much effort making something flexible to work with a variety of other objects? If you know w

9条回答
  •  遇见更好的自我
    2021-02-04 13:37

    First of all, you're comparing apples to oranges, so let me try to explain this from two perspectives. Typing refers to how operations on values/variables are performed and if they are allowed. Coupling, as opposed to cohesion, refers to the architecture of a piece (or several pieces) of software. The two aren't directly related at all.

    Strong vs Weak Typing

    A strongly typed language is (usually) a good thing because behavior is well defined. Take these two examples, from Wikipedia:

    Weak typing:

    a = 2
    b = '2'
    
    concatenate(a, b) # Returns '22'
    add(a, b)         # Returns 4
    

    The above can be slightly confusing and not-so-well-defined because some languages may use the ASCII (maybe hex, maybe octal, etc) numerical values for addition or concatenation, so there's a lot of room open for mistakes. Also, it's hard to see if a is originally an integer or a string (this may be important, but the language doesn't really care).

    Strongly typed:

    a = 2
    b = '2'
    
    #concatenate(a, b)     # Type Error
    #add(a, b)             # Type Error
    concatenate(str(a), b) # Returns '22'
    add(a, int(b))         # Returns 4
    

    As you can see here, everything is more explicit, you know what variables are and also when you're changing the types of any variables.

    Wikipedia says:

    The advantage claimed of weak typing is that it requires less effort on the part of the programmer than, because the compiler or interpreter implicitly performs certain kinds of conversions. However, one claimed disadvantage is that weakly typed programming systems catch fewer errors at compile time and some of these might still remain after testing has been completed. Two commonly used languages that support many kinds of implicit conversion are C and C++, and it is sometimes claimed that these are weakly typed languages. However, others argue that these languages place enough restrictions on how operands of different types can be mixed, that the two should be regarded as strongly typed languages.

    Strong vs weak typing both have their advantages and disadvantages and neither is good or bad. It's important to understand the differences and similarities.

    Loose vs Tight Coupling

    Straight from Wikipedia:

    In computer science, coupling or dependency is the degree to which each program module relies on each one of the other modules.

    Coupling is usually contrasted with cohesion. Low coupling often correlates with high cohesion, and vice versa. The software quality metrics of coupling and cohesion were invented by Larry Constantine, an original developer of Structured Design who was also an early proponent of these concepts (see also SSADM). Low coupling is often a sign of a well-structured computer system and a good design, and when combined with high cohesion, supports the general goals of high readability and maintainability.

    In short, low coupling is a sign of very tight, readable and maintainable code. High coupling is preferred when dealing with massive APIs or large projects where different parts interact to form a whole. Neither is good or bad. Some projects should be tightly coupled, i.e. an embedded operating system. Others should be loosely coupled, i.e. a website CMS.

    Hopefully I've shed some light here :)

提交回复
热议问题