i just started learning c# and finding it quite difficult to deal with data hiding.even though i understand what public ,protected etc means when i'm coding i'm confused as to what to use, i feel when i use public i make alteration very easy and with protected very restrictive. i just understand their definitions please help with understanding data hiding on the practical side.
data hiding
Page 1 of 16 Replies - 7016 Views - Last Post: 08 August 2007 - 05:14 PM
Replies To: data hiding
#3
Re: data hiding
Posted 28 July 2007 - 01:07 PM
Scope is one of the most tricky things for most programmers. How specific do you get? How much do you hide? One rule I have always followed that seems to work really well is make everything private and "promote" it if you definitely need to access it "directly".
How does this work? If I define a variable in a class' method, I make it private. If I find I need that variable to hold a value beyond a method, I "promote" it to a class variable. Do I need to access the variable directly? I then "promote it again to public.
When passing variables I ask the question "Is it necessary or can I figure out the information in the function itself?"
If you have one big problem with this whole scoping process, I recommend one book called "Code Complete" by Steve McConnell (Microsoft Press). The book is aged a bit, but the idea of scoping and variable lifetime chapters it includes is still good even today. He provides examples in Java, C++, VB, and even PHP/Python etc. It will tell you about variable scope, lifespan, localization etc. Great book.
Below is a link to the book. Enjoy!
Code Complete by Steve McConnell (Amazon.com)
How does this work? If I define a variable in a class' method, I make it private. If I find I need that variable to hold a value beyond a method, I "promote" it to a class variable. Do I need to access the variable directly? I then "promote it again to public.
When passing variables I ask the question "Is it necessary or can I figure out the information in the function itself?"
If you have one big problem with this whole scoping process, I recommend one book called "Code Complete" by Steve McConnell (Microsoft Press). The book is aged a bit, but the idea of scoping and variable lifetime chapters it includes is still good even today. He provides examples in Java, C++, VB, and even PHP/Python etc. It will tell you about variable scope, lifespan, localization etc. Great book.
Below is a link to the book. Enjoy!
Code Complete by Steve McConnell (Amazon.com)
#4
Re: data hiding
Posted 28 July 2007 - 01:10 PM
Great info Martyr2! Thanks for sharing!
#7
Re: data hiding
Posted 08 August 2007 - 05:14 PM
Martyr2 thats some excellent advice! Thats almost how I do it, with the exception of a couple changes.
/me bookmarks that post
/me bookmarks that post
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|