title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
4:34 | /// <summary> |
4:35 | /// This code is being used just for explaining the concept of |
4:36 | /// cyclomatic complexity. |
4:37 | /// It makes no sense at all. Please Calculate Code Metrics for |
4:38 | /// understanding |
4:39 | /// </summary> |
4:40 | private static void CyclomaticComplexitySample() |
4:41 | { |
4:42 | var billingMode = GetBillingMode(); |
4:43 | var messageResponse = ProcessCreditCardMethod(); |
4:44 | switch (messageResponse) |
4:45 | { |
4:46 | case `A`: |
4:47 | if (billingMode == `M1`) |
4:48 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:49 | $`Message Response {messageResponse}`); |
4:50 | else |
4:51 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:52 | $`Message Response {messageResponse}`); |
4:53 | break; |
4:54 | case `B`: |
4:55 | if (billingMode == `M2`) |
4:56 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:57 | $`Message Response {messageResponse}`); |
4:58 | else |
4:59 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:60 | $`Message Response {messageResponse}`); |
4:61 | break; |
4:62 | case `C`: |
4:63 | if (billingMode == `M3`) |
4:64 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:65 | $`Message Response {messageResponse}`); |
4:66 | else |
4:67 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:68 | $`Message Response {messageResponse}`); |
4:69 | break; |
4:70 | case `D`: |
4:71 | if (billingMode == `M4`) |
4:72 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:73 | $`Message Response {messageResponse}`); |
4:74 | else |
4:75 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:76 | $`Message Response {messageResponse}`); |
4:77 | break; |
4:78 | case `E`: |
4:79 | if (billingMode == `M5`) |
4:80 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:81 | $`Message Response {messageResponse}`); |
4:82 | else |
4:83 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:84 | $`Message Response {messageResponse}`); |
4:85 | break; |
4:86 | case `F`: |
4:87 | if (billingMode == `M6`) |
4:88 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:89 | $`Message Response {messageResponse}`); |
4:90 | else |
4:91 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:92 | $`Message Response {messageResponse}`); |
4:93 | break; |
4:94 | case `G`: |
4:95 | if (billingMode == `M7`) |
4:96 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:97 | $`Message Response {messageResponse}`); |
4:98 | else |
4:99 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:100 | $`Message Response {messageResponse}`); |
4:101 | break; |
4:102 | case `H`: |
4:103 | if (billingMode == `M8`) |
4:104 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:105 | $`Message Response {messageResponse}`); |
4:106 | else |
4:107 | Console.WriteLine($`Billing Mode {billingMode} for ` + |
4:108 | $`Message Response {messageResponse}`); |
4:109 | break; |
4:110 | default: |
4:111 | Console.WriteLine(`The result of processing is unknown`); |
4:112 | break; |
4:113 | } |
4:114 | } |
4:115 | |
4:116 | If you calculate the code metrics of this code, you will find a bad result when it comes to cyclomatic complexity, as you can see in the following screenshot. A cyclomatic complexity number above 10 indicates that the code is difficult to read, and a developer will probably have trouble maintaining it in a future code change. |
4:117 | |
4:118 | Figure 4.2: High level of cyclomatic complexity |
4:119 | It is important to reinforce that the purpose of the code from this example is not the focus here. The point here is to show you the number of improvements that can be made to write better code: |
4:120 | |
4:121 | The options from switch-case could be written using Enum. |
4:122 | Each case processing can be done: |
4:123 | In a specific method. |
4:124 | In a specific class, inheriting the action from the superclass, using the polymorphism concept. |
4:125 | In a specific class, implementing an interface to define a contract. |
4:126 | |
4:127 | |
4:128 | switch-case can be substituted with Dictionary<Enum, Method> or by using the switch expression. |
4:129 | |
4:130 | By refactoring this code with the preceding techniques, the result is a piece of code that is much easier to understand, as you can see in the following code snippet of its main method: |
4:131 | static void Main() |
4:132 | { |
4:133 | var billingMode = GetBillingMode(); |