Tuesday, July 22, 2008

Decide the Accessibility of Class Members in C Sharp

Another article in wikiHow ...


How to Decide the Accessibility of Class Members in C Sharp


from wikiHow - The How to Manual That You Can Edit

One of the choices you always make as a C# programmer is to decide the accessibility of each member (field, event, property and method) you declare in your classes. For advanced C# programmers, this is not actually really hard, but it can be pretty destructive for new to intermediate C# programmers, and seldom, even to advanced one. This article outlines the process of deciding the accessibility of your members in a systematic, easy to remember and straightforward approach.

Steps

  1. Design and implement your members as if you have already decided their accessibility level (usually this is done by implementing all members as public)
  2. Start deciding the accessibility of each member by itself repeating the following steps on all members:
    1. Start by giving the member the lowest accessibility possible: (private)
    2. Answer the following questions:
      • Will any existing code in your application or solution be affected by declaring the member on this accessibility level? For example, some other class might have code that accesses this member or uses it and because of the current level of accessibility chosen, it won't be able to access it anymore.
      • Is your member designed to be used by other developers, and if so, will they be able to access it in its current accessibility level?
      • In your future code, will changing the accessibility of this member be near impossible?
      • Is your class intended to be inherited, and if so, will the inheriting class face troubles accessing this member (if it is allowed to)?

    3. Check if the answers to all the previous questions were "no" or not.
    4. If the answers were all "no", leave the accessibility level as it is and start deciding the accessibility of another member
    5. If one or more of the answers was "yes", increase the level of accessibility one degree and repeat the steps for the same member, from step 2

Tips

  • Stick to this approach until you find that more than 90% of your decisions without using this approach match the results obtained by applying this approach, then you can start counting more on your own initial judgment.
  • When you become more and more familiar with programming in C#, many standard choices become obvious and they do not need a method to be decided. Hence, you start applying this approach only to those member you doubt or not included in any standard.
  • Even for advanced programmers, this approach can be helpful when used while skimming through your code in the final stages. It might catch some member declared using a higher accessibility than actually required even if that happened by chance or as a result of a typing error.
  • Some of the standard accessibility levels that became well established amongst C# programmers are:
    • Fields are usually declared private in structs and sealed classes and as protected in most other classes (unless you do not intend to inherit the class neither allow anyone who does to access the field, then private it is)
    • Class fields that are static readonly or const are usually public as they usually hold constant data available to other users of the class
    • Methods can be declared of any accessibility but usually are either:
      • private for internal implementation methods.
      • protected for virtual methods such as event firing methods.
      • public for user methods (methods that will be used by users of the class)

    • Events are usually public. In rare cases we need private events and even less protected events.
    • Properties are usually public or private. In rare situations we need protected or internal properties.

Warnings

  • It is considered a very bad programming practice to declare members with higher accessibility levels than they really require. This puts holes in your code.
  • Do not take the last tip in the "Tips" section as a starting point unless you are really familiar with the steps before it and have applied them many times. They are not always correct and they depend mainly on the situation.

Related wikiHows

Article provided by wikiHow, a collaborative writing project to build the world's largest, highest quality how-to manual. Please edit this article and find author credits at the original wikiHow article on How to Decide the Accessibility of Class Members in C Sharp. All content on wikiHow can be shared under a Creative Commons license.

No comments: