Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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.

share|improve this question

marked as duplicate by MichaelT, gnat, Bart van Ingen Schenau, maple_shaft Jan 14 at 13:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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 Oliveira Jan 14 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. – Abhinav Gauniyal Jan 14 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 Jan 14 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 Jan 14 at 14:37
up vote 37 down vote accepted

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.

share|improve this answer
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 at 3:05
18  
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 Grinberg Jan 14 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 Jan 14 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. – Steve Jessop Jan 14 at 11:24
2  
@Mawg (Parts of) the german anthem have been over the years been illegal to sing. en.wikipedia.org/wiki/Deutschlandlied – Angelo Fuchs Jan 14 at 12:23

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.

share|improve this answer
1  
I think the question is asking about a different type of scope – Jezzamon Jan 14 at 20:59

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