![]() ![]() ![]() To profit from the benefits of high cohesion, keep the variables as close as possible to the code block that uses them.įor instance, if a variable solely exists to form the logic of block scope, then declare and make the variable exist only within that block (using const or let declarations). High cohesion accompanied by loose coupling is the characteristic of a well-designed system.Ī code block can be considered a small module. Testable: it's easier to test a module that's focused on a single task.Reusable: being focused on a single task, makes the module easier to reuse.Maintainable and easier to refactor: the change in the module affects fewer modules.Focused and understandable: easier to understand what the module does.The cohesion can be high or low.Ī high cohesion module is preferable because the elements of such a module focus solely on a single task. Let's check the function that verifies whether a word is a palindrome:Įncouraging the usage of const for immutable bindings or let otherwise ensures a practice that reduces the appearance of the uninitialized variable.Ĭohesion characterizes the degree to which the elements of a module (namespace, class, method, block of code) belong together. The variable is not exposed to the uninitialized state and accessing undefined is impossible. One of the nice features of const is that you must assign an initial value to the variable const myVariable = 'initial'. If you want to define a variable, always start with const, which creates an immutable binding. ![]() Tip 1: Favor const, otherwise use let, but say goodbye to varĬonst and let are block scoped (contrary to older function scoped var) and exist in a temporal dead zone until the declaration line. Ideally, you would assign a value right away after declaration const myVariable = 'Initial value'. The less the variable exists in an uninitialized state, the better. Accessing the variable evaluates to undefined.Īn efficient approach to solve the troubles of uninitialized variables is whenever possible to assign an initial value. ![]() MyVariable is declared and not yet assigned with a value. ![]()
0 Comments
|