开发者

Design guidelines for .net functions

开发者 https://www.devze.com 2023-02-19 04:15 出处:网络
What de开发者_Python百科sign guidelines do you follow while designing/adding new functions in a class? The guidelines can be varied ranging from naming convention, size of a function, input parameters

What de开发者_Python百科sign guidelines do you follow while designing/adding new functions in a class? The guidelines can be varied ranging from naming convention, size of a function, input parameters, output parameters etc.

This question is specifically targeted to .net environment but equally applies to other programming environments.


Here you can find some Design Guidlines for Developing Class Libraries


When encountering multiple environments, languages and platforms designing conventions and guidelines will take a huge effort. In my experience it is best to stick to the suppliers' or communities' standards. The reasons for doing this:

  1. Less work
  2. New team members can easily pick it up; it should be familiar already
  3. Future additions to the language/environment will probably be easy to add

So look up the standards from the vendors and communities and add the stuff that isn't specified (most of the time internal things like naming local variables that do not influence interfaces)


I try to follow the following guidelines for any new functions that I add to a class:

  • Function name should follow Pascal naming convention.
  • Function name should reveal what it does – no need to look at code. If you are trying to understand a piece of code, the function name should tell exactly what it does and there should not be any need to go and look at the code of that function.
  • Function should do one thing (Single Responsibility Principle) and do that thing right.
  • Functions should be as small as possible.
  • Function name should be verb. It should not include the name of the class. For e.g. User.Authenticate is better than User.AuthenticateUser.
  • Non-static function should either mutate the state of an object or return the state of an object but not both. If the function does both, it becomes confusing and often could not be named appropriately.
  • Function input should be a more generalized base type that is appropriate.
  • Function return value should be a more specialized derived type that is appropriate.
  • Function should document any assumptions (if any) before and after the logic of the function by making use of Debug.Assert.
  • Public Functions within a class should be order dependent. If the function expects a certain state of an object then it should throw an appropriate exception to the consumer.
  • Public Functions should check that the input supplied is valid. If the input is not valid it should throw an appropriate exception. There are exception classes in .net for the same purpose: ArgumentNullException, ArgumentOutOfRangeException, ArgumentException etc. See if there is already an exception class that servers the purpose before creating a custom one.
  • Public functions should have the xml comments documenting: purpose, input parameters, output, exceptions that the function throws special consideration etc about the function.
  • Private functions should not check for validity of input as they are invoked from within the class.
  • Private functions that do not access the state of an object should be declared as static.
  • Function should access any base class members by prefixing it with ‘base’. For e.g. base.ToString() should be used for invoking the base class method.
0

精彩评论

暂无评论...
验证码 换一张
取 消