How to Define a Method

Identify the problem => Define the single purpose => Specify the inputs and outputs => State any assumptions => Consider the error that could occur

Build a Method

Signature

  1. public bool PlaceOrder(Product product, int quantity)

Optional accessibility modifier (Default is private)

Return type (void if no value)

Method name

Parameter list (Empty parenthesis if no parameters)

XML Document Comment

Use ///

Summary for method purpose

param for a description of each parameter

Signature Best Practices

Do:

Use a verb

Define the most restricted accessibility possible

Keep the number of parameters to a minimum

Define an XML document comment

Avoid:

Vague terms

Abbreviations

Conjunctions (and/or)

Inconsistent naming

Method Body

  1. public bool PlaceOrder(Product product, int quantity)
  2. {
  3. // Guard Clause (garbage in,not garbage out)
  4. if (product == null) throw new ArgumentNullException(nameof(product));
  5. if (quantity <= ) throw new ArgumentOutOfRangeException(nameof(quantity));
  6. }

Best Practices

Do:

Keep methods short (less than 20 lines)

Use white space

Use guard clause

Return an expected result (Use an object to return multiple values)

Implement exception handling

Avoid:

void methods

Property or Method

Property example:

ProductName, Description, InventoryCount?, SuggestedPrice?

Method example:

PlaceOrder(), CalculateInventoryCount()?, CalculateSuggestedPrice()?

Property:

Does it describe data?

Does it execute quickly?

Method:

Does it describe processing?

Does it produce side effects?

Does it require parameters?

Method Overloading

  1. public bool PlaceOrder(Product product, int quantity)
  2.  
  3. public bool PlaceOrder(Product product, int quantity, DateTimeOffset deliveryBy)
  4.  
  5. public bool PlaceOrder(Product product, int quantity, DateTimeOffset deliberyBy, string instructions)
  6.  
  7. public void PlaceOrder(Product prouct, int quantity) ** Not a valid overloading

Best Practices

Do:

Keep the number of parameters to a minimum

Keep the order of the parameters consistent

Define a XML document comment for each overload

Consider optional parameters

Avoid:

Confusing overloads

Overloads that differ in purpose

(Example: OrderItems. One overload get the ordered items, the second overload orders a set of items)

Duplicating code

Method chaining

  1. public OperationResult PlaceOrder(Product product, int quantity)
  2. {
  3. return PlaceOrder(product, quantity, null, null);
  4. }
  5.  
  6. public OperationResult PlaceOrder(Product product, int quantity, DateTimeOffset? deliverBy)
  7. {
  8. return PlaceOrder(product, quantity, deliverBy, null);
  9. }
  10.  
  11. public OperationResult PlaceOrder(Product product, int quantity, DateTimeOffset? deliverBy)
  12. {
  13. // All of the code here
  14. }

Method Chaining Best Practices

Do:

Use to minimize repeated code in method overloads

Consider optional parameters

Avoid:

If it adds complexity

Method Overriding

We can override ToString() for each entity class to make debug easy

FAQ

1.What's the primary purpose of a method?

To implement the logic required for specific behavior or functionality in a class

2.What is the difference between a parameter and an argument?

A parameter is part of the method signature

An argument is part of the method call

3.What is method overloading?

Methods with the same name and purpose but different signatures.

4.What is method chaining?

One method overload calls another overload to prevent repeated code.

5.When is it the best to use method overloading vs. method overriding?

Use overloading when one method requires multiple signatures. Such as GetCustomer(id) to get a customer by id and GetCustomer(name) to get a customer by name.

Use overriding when replacing a method defined high up the object hierarchy. Such as replacing the ToString() method.

C# Best Practices - Creating Good Methods的更多相关文章

  1. C# Best Practices - Creating Good Properties

    Coding Properties Code in the Getter Check the user's credentials Check application state Format the ...

  2. OOP in JS Public/Private Variables and Methods

    Summary private variables are declared with the 'var' keyword inside the object, and can only be acc ...

  3. zookeeper kazoo Basic Usage

    http://kazoo.readthedocs.org/en/latest/basic_usage.html Basic Usage Connection Handling To begin usi ...

  4. 转: angular编码风格指南

    After reading Google's AngularJS guidelines, I felt they were a little too incomplete and also guide ...

  5. [转]JavaScript Namespaces and Modules

    Namespaces In most programming languages we know the concept of namespaces (or packages).Namespaces ...

  6. SAP HANA学习资料大全[非常完善的学习资料汇总]

    Check out this SDN blog if you plan to write HANA Certification exam http://scn.sap.com/community/ha ...

  7. Sonar 常用代码规则整理(一)

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 摘要:公司部署了一套sonar,经过一段时间运行,发现有一些问题出现频率很高,因此有必要将这些问题进行整理总结和分 ...

  8. 单元测试系列之九:Sonar 常用代码规则整理(一)

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 摘要:公司部署了一套sonar,经过一段时间运行,发现有一些问题出现频率很高,因此有必要将这些问题进行整理总结和分 ...

  9. Mocks Aren't Stubs

    Mocks Aren't Stubs The term 'Mock Objects' has become a popular one to describe special case objects ...

随机推荐

  1. 6_StopWatch

    6 // // ViewController.swift // StopWatch // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. Al ...

  2. A20 GPIO中断类型差别结果迥异的问题思考

    A20GPIO中断类型差别结果迥异的问题思考 最近在使用全志A20做开发时,发现在处理中断的时候,用电平触发模式,报中断比较乱,用边沿触发则很稳定,不会乱报.笔者感到比较困惑,笔者用电平触发写的cod ...

  3. hdu 5461 Largest Point

    Thinking about it: 对于式子 a * ti * ti + b * tj,可以看作时有两部分构成 a * ti * ti 和 b * tj,如果整个式子要最大,则要求这两部分都要尽量大 ...

  4. Echoprint系列--Android编译与调用

    在Echoprint系列--编译中编译了源代码,这次将Echoprint移植到Android平台并測试识别歌曲功能. 一.编译库 1.环境准备 Android NDK,我的是android-ndk-r ...

  5. UIKit封装的系统动画

    简介 在UIKit中,对UIView封装了很多类方法来进行简单的动画实现,在动画过程中,通过对属性值的修改来完成一系列的效果. 在IOS4以前,主要通过 + beginAnimation + setA ...

  6. 2013杭州网络赛D题HDU 4741(计算几何 解三元一次方程组)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. js小写转实现资本,js数字革命万元

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>  <hea ...

  8. AngularJS Directive 学习笔记

    指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...

  9. 浅谈JDBC(二)

    JDBC中的事务 简单来说,事务就是要保证一组DAO方法的操作,要么一起成功要么一起失败. 1.事务控制的位置 在Service中的业务方法内进行事务控制. 2.事务控制的代码 a.注意:JDBC会自 ...

  10. html加载与脚本运行中,由于html未完全加载而导致脚本找不到dom元素无法执行事件

    问题: 如题,就是说alert后出现一个对话框,我没有点关闭,其他js文件或html会被加载吗,我遇到一个问题就是在页面加载时调用一个div的click事件,却没有被执行,但是在调用事件前面加上ale ...