4 ms·
High cohesion, loose coupling. That summarizes it. But to understand what it means usually takes at least 10 years and most people will never understand it. M
by cryptica 5y ago
High cohesion, loose coupling.
That summarizes it. But to understand what it means usually takes at least 10 years and most people will never understand it.
My best interpretation of high cohesion is that it means that every component/module/class in a system can be easily explained to a non-technical person who has business/domain knowledge... In as few words as possible.
Loose coupling requires simple interfaces. Simple functions/methods which accept simple (raw primitives or plain objects) as arguments... You want to avoid methods which accept 'live instances' as much as possible. Instances which have their own internal state should not cross module boundaries ideally (not passed around as arguments). That is also a sign of low cohesion. High cohesion and loose coupling often go together naturally. For loose coupling, I often think of a power plug/power point or a USB port... Simpler is better. Imagine how difficult it would be to plug an elaborate 10-prong power plug into a socket with 10 holes... Imagine how much work that would be for the electronic equipment manufacturers. The interface must be as simple as necessary to get the job done.
If you can do these two things, it will constrain how you think about and write code and you will produce the most maintainable code you can imagine. The language won't even matter at all. If your method interfaces are simple (no complex type parameters), it becomes trivial to figure out which types a specific methods accepts as arguments.
IMO TypeScript makes it easier to work with bad code, but isn't a better solution simply to avoid bad code? It's not that difficult once you have the right approach.
In a way, I feel like TypeScript is a symptom of the entire industry surrendering to bad code... A surprising number of people in the corporate sphere actually believe that there is no such thing as good code and even that architecture, software design and planning doesn't matter. The only solution they will accept is to throw more people at the problem and implement security through obscurity.
- TheCoelacanth 5y agoI don't agree that types make it easier to write bad code. If anything, they make it easier to spot bad code by making interfaces more explicit.
- branko_d 5y ago> Imagine how difficult it would be to plug an elaborate 10-prong power plug into a socket with 10 holes... Imagine how difficult it would be to plug an elaborate 10-prong power plug into a socket with unknown number of holes. With static typing, you at least know how many holes are there. Now, I agree that "the interface must be as simple as necessary to get the job done", but non-trivial problems will have non-trivial essential complexity even after you have removed all incidental complexity.
- cryptica 5y agoYes but you forget incentives. If a tool makes something easier. People get lazier. E.g. People today have terrible handwriting because of computer keyboards. Many people today can't do long division on paper because of calculators. People today struggle to find their way around new places when they don't have Google Maps. But in this case, the skill which is being atrophied is an actual useful skill... Though it's not obvious at a glance. A tool which makes it easier to do the wrong thing will lead more people to do the wrong thing. The friction which dynamic languages create is a positive and promotes good code and professional development.
- branko_d 5y agoAre you arguing that we should know which roots contain water and which parts of a horse are best for making bow strings? Circumstances change, skills evolve with the circumstances. Yes, there is a danger of sudden reversal of circumstances, but we still have our ability to relearn and rediscover the "old" skills if need be. If friction was "positive and promoted good code", the best code would be on punch cards.
- cryptica 5y agoI understand that we are not used to factor in psychology when it comes to software development and that this argument may seem contrived but I'm speaking from my own professional experience when I say that I've observed both in myself and others that learning to use dynamically typed languages correctly has had a significant positive impact on the general code style. I've gone back and forth between the two paradigms multiple times. It forces developers to pay more attention to software design and structure or else it becomes overwhelming. The fact that code becomes overwhelming more quickly with dynamically typed languages is a positive thing. It's an effective deterrent against complexity and incorrect abstractions.
- throwanem 5y agoTypeScript also makes it easier to work with good code.
- deckard1 5y ago> symptom of the entire industry surrendering to bad code Exactly. That's why TypeScript is structural typing with inference. The industry has given up on teaching developers how to organize code. The only way to force everyone to play nice together is through tools: prettier, VSCode, TypeScript, lint, etc. etc. Part of me thinks this is a social problem. Too many unmoored egos in the industry and no one in the social position of being able to lay down ground rules for a codebase. Or possibly a result of devs chasing higher salaries and becoming mercenaries always on the move. It's rare to have teammates that have been with a company longer than a year anymore.