16

I'm relatively new to programming (July 2015), and I've always wondered why it's good programming practice to hide variables as much as possible.

I've run into this question mainly recently when I looked into events and delegates in C#. I searched around as to why I should use events rather than just a delegate, since they do the same thing it seems. I read that it's better programming practice to hide the delegate fields and use an event.

I decided it was time to learn why it was good programming practice, but I couldn't really find anything other than "Because it's good programming practice".

If you could provide some basic examples and maybe some pseudo-code that would be helpful.

5
  • I realise in my own studies practicing TDD I realize that is the best way to find a good "designer" of code. And when you to start make TDD(Test Driven Development) and unit tests you will go realize too. Is so most easy test a small part of a system. With TDD, unit test and practices, the inside about minimalistic code comes naturally. Commented Jan 14, 2016 at 2:28
  • Also limiting scope helps with performance and debugging. You can be certain that something (some variable or event etc) cannot exist or contain certain property if your program isn't branching into certain path. You've successfully limited the scope for that scenario. Commented Jan 14, 2016 at 9:23
  • Reduces the number of mistakes you can make. If you aren't supposed to use it outside the scope, then make it impossible to do so, and you can't make that mistake.
    – Ben
    Commented Jan 14, 2016 at 10:13
  • I don't think this is a duplicate of the question about global state at all. Global state and variable visibility are very clearly two different topics.
    – COME FROM
    Commented Jan 14, 2016 at 14:37
  • 1
    I don't feel any of these answers really identify the benefit of carefully managing the scope of variables. As a basic metaphor let's imagine I ask you for a favour: go to my flat and throw all the cows milk. When you enter my flat only one door is open and it leads you directly to the fridge with milk. You quickly throw the milk and leave without being able to touch the milk stashed in the living room bar. Scoping variables means limiting access to reduce the chances of unintended changes.
    – Johan
    Commented Oct 9, 2017 at 21:03

2 Answers 2

42

Because the more things you have to deal with in any task the harder it becomes.

For example, try patting your head. Then try patting your head and counting backwards from 1000. Then try patting your head counting backwards from 1000 and hopping on one leg. Then try patting your head counting backwards from 1000 and hopping on one leg and singing the national anthem. Gets a lot harder doesn't it?

Each of those tasks were simple and would be easy on their own. If you keep your code small and granular and limit the amount of variables in scope you're dealing with less things at a time. This means you're less likely to fall over while standing on one leg because you were distracted by singing the national anthem and counting backwards from 1000.

9
  • 3
    Depending on where you live, singing one's national anthem may not be easy. For instance, the "Star-Spangled Banner" is a notoriously difficult song to sing, and I'm sure it's not the most difficult anthem. (and if it weren't obvious from the fact that this nit-pick is the most relevant critique I have, +1)
    – KRyan
    Commented Jan 14, 2016 at 3:05
  • 21
    Don't forget that if you don't minimize scope then every time you do something you basically need to pray that no one else is already patting your head or counting backwards from 1000 or hopping on one leg or singing the national anthem because then you'll throw an exception. Commented Jan 14, 2016 at 4:29
  • 1
    For some people it was even illegal to sing their national anthem in their own home country (because politics changed since the words were written...) Curious if there are current examples. Or if there are any national anthems without words.
    – gnasher729
    Commented Jan 14, 2016 at 9:17
  • 3
    @KRyan: nah, it's easy to sing. It's difficult to sing well, but frankly we're not expecting professional-quality hopping either. Commented Jan 14, 2016 at 11:24
  • 2
    @Mawg (Parts of) the german anthem have been over the years been illegal to sing. en.wikipedia.org/wiki/Deutschlandlied Commented Jan 14, 2016 at 12:23
5

The simpler your application is, the less likely it will be to break.

Adding complexity means adding places where errors can occur. Usually those errors will be in your code, but occasionally you can hit errors from either the compiler or operating system.

An Example

You build a calculator object. It can do all sorts of calculator things (i.e., add numbers, multiply numbers, even determine square roots). You wouldn't need or want to add a method that draws lines on an image. You also wouldn't need or want a method that asks StackExchange for the person with the most reputation on all of its sites.

1
  • 3
    I think the question is asking about a different type of scope
    – Jezzamon
    Commented Jan 14, 2016 at 20:59

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