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.

moooeeeep
  • 133
  • 4
overki11
  • 187
  • 1
  • 5
  • 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. – Abhinav Gauniyal Jan 14 '16 at 09: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 Jan 14 '16 at 10:13
  • 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. – Marcius Araujo Jan 14 '16 at 02:28
  • I don't think this is a duplicate of [the question about global state](http://programmers.stackexchange.com/questions/148108/why-is-global-state-so-evil) at all. Global state and variable visibility are very clearly two different topics. – COME FROM Jan 14 '16 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 Oct 09 '17 at 21:03

2 Answers2

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.

Dan
  • 654
  • 1
  • 7
  • 18
Tom Squires
  • 17,695
  • 11
  • 67
  • 88
  • 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 Jan 14 '16 at 03: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. – David says Reinstate Monica Jan 14 '16 at 04: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 Jan 14 '16 at 09:17
  • How does the above scenario work in Spain, as I believe their national anthem is instrumental? – TMH Jan 14 '16 at 10:02
  • @gnasher729 +1, but can you give us some links for that "illegal to sing your own national anthem" statement? – Mawg says reinstate Monica Jan 14 '16 at 10:44
  • 3
    @KRyan: nah, it's easy to sing. It's difficult to sing *well*, but frankly we're not expecting professional-quality hopping either. – Steve Jessop Jan 14 '16 at 11:24
  • 1
    @Mawg: La Marsaillaise has been banned in France (by Louis XVIII, thanks Wikipedia). I'm not sure whether it was explicitly banned in occupied France during the Second World War, but singing it certainly was not always to the liking of the occupiers (see the movie Casablanca for the kind of thing). But presumably at the times it was banned it was legally not the national anthem, since the people with legal power to ban it are the same people who get to choose the official national anthem. – Steve Jessop Jan 14 '16 at 11:29
  • 2
    @Mawg (Parts of) the german anthem have been over the years been illegal to sing. https://en.wikipedia.org/wiki/Deutschlandlied – Angelo Fuchs Jan 14 '16 at 12:23
  • Das haette ich wissen sollen :-( – Mawg says reinstate Monica Jan 14 '16 at 15:34
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.

Adam Zuckerman
  • 3,715
  • 1
  • 19
  • 27