7

I'm reading 97 Things Every Programmer Should Know, now I'm positioned in "Apply Functional Programming Principles", and there is a paragraph that says:

...A leading cause of defects in imperative code is attributable to mutable variables. Everyone reading this will have investigated why some value is not as expected in a particular situation. Visibility semantics can help to mitigate these insidious defects, or at least to drastically narrow down their location, but their true culprit may in fact be the providence of designs that employ inordinate mutability...

What is the objective of Semantics Visibility in this context? How to apply this approach to solve mutable variables' insidious defects?

  • Hello and Welcome to Programmers. While, in this particular instance, there is a freely available version of the content available via O'Reilly, please be very careful when posting links to material. – World Engineer Apr 13 '13 at 22:42
4

Not 100% sure, but it seems they talk about how a language handles the logical scope of variables. The worst case would be a language with only global variables. Mutability allows you to "accidently" change a variable. So if you can limit the variables visibility (or scope) to the level of a function, a class, namespace, module or similar smaller units the risk to change something that's used in different places too would be far lower.

2

This is referring to the visibility and scope of variables. If you are familiar with Java, you may recognize the usage of terms like Public, Private, Protected, and so forth. Proper usage of these terms is part of encapsulation, and is core to good programming practices of Object Oriented Programming.

Making variables private means that they cannot be accessed from code outside of the code in question. The benefit is that you can be assured that it is not changed or otherwise disturbed in any way that you are not familiar with. In other words, you would only have to look at the code within that single file in order to see what could be done to that variable, instead of worrying about a 1,000,000+ line codebase and knowing that it could be changed anywhere. The latter case is a very real problem for languages such a COBOL, where all variables are global, and any code can effect any variable. Nightmares are generated this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.