Preface
In the process of writing code to achieve business requirements, a large number of if...else judgment statements will be used. Depending on the complexity of the business, the judgment statements are multi-layer nested, multi-branched, etc., resulting in poor code readability and increasing maintenance difficulty. To reduce and eliminate this complex, noodle-style code, you need to pay more attention to programming style when writing code and try to avoid the complexity of if...else. Learn some code optimization methods and cultivate your own easy-to-read and beautiful code style.
plan
1. Return in advance to remove unnecessary else
Disadvantages: There is no big effect on more if...else.
Example:
#region Before optimizationif (()) { ("The order number cannot be empty, please enter the order number."); (); } else { // Source business logic} #endregion #region Optimizedif (()) { ("The order number cannot be empty, please enter the order number."); (); return; } // Source business logic#endregion
2. Use the conditional three-point operator
Disadvantages: Only if...else two choices will be effective. If there are multiple, reading comprehension will be more laborious.
Example:
#region Before optimizationif ("CaiNiao".Equals(templateType, )) { // The setting button is visible = true; } else { // Settings button is not visible = false; } #endregion #region Optimized// Set whether the button is visible = "CaiNiao".Equals(, ) ? true : false; #endregion
3. Use switch
If...else can be used instead of switch. When there are many branches, try to use switch.
Example:
#region Before optimizationif (keyValue==120) { // Press F9 shortcut key to locate the product input box (); } else if (keyValue==121) { // Press F10 shortcut key to locate the order input box (); } else if (keyValue==122) { // Press F11 shortcut key to review (); } #endregion #region Optimizedswitch () { case 120: // Press F9 shortcut key to locate the product input box (); break; case 121: // Press F10 shortcut key to locate the order input box (); break; case 122: // Press F11 shortcut key to review (); break; default: break; } #endregion
4. Merge conditional expressions
Clarify and summarize logical judgments to change them into simpler and easier to understand logical judgment codes.
Example:
#region Before optimizationstring expressCode ="DOUYIN-YTO" string templateurl=; string[] splitCode = expressCode..Split('-'); if ( >= 2) { if ((splitCode[1])) { templateurl="/" } } #endregion #region Optimizedstring expressCode = "DOUYIN-YTO"; string templateurl = ; string[] splitCode = ('-'); if ( >= 2 && (splitCode[1])) { templateurl = "/"; } #endregion
5. Use enumeration
Using conditions as the value of an enum can avoid the use of a large number of if...else statements.
Example:
#region Before optimizationint platformID = 0; string platformCode ="DOUYIN"; if ("PINDUODUO".Equals(platformCode, )) { platformID = 1; } else if ("DOUYIN".Equals(platformCode, )) { platformID = 2; } else if ("KUAISHOU".Equals(platformCode, )) { platformID = 3; } else if ("SHIPINHAO".Equals(platformCode, )) { platformID = 4; } else if ("AIKUCUN".Equals(platformCode, )) { platformID = 5; } else if ("XIAOHONGSHU".Equals(platformCode, )) { platformID = 6; } #endregion #region Optimized// Define a data enumpublic enum Platform { PINDUODUO=1, DOUYIN, KUAISHOU, SHIPINHAO, AIKUCUN, XIAOHONGSHU } Platform platform; bool result=<Platform>("SHIPINHAO", out platform); int platformID = Convert.ToInt32(platform); // It is necessary to determine whether SHIPINHAO is enumeratedif (platformID==0) { // Exception handling} #endregion
6. Use dictionary
Use the condition as the key of the dictionary and the processing method as the key value of the dictionary. This avoids the use of a large number of if...else statements.
Example:
#region Before optimizationint platformID = 0; string platformCode ="DOUYIN"; if ("PINDUODUO".Equals(platformCode, )) { platformID = 1; } else if ("DOUYIN".Equals(platformCode, )) { platformID = 2; } else if ("KUAISHOU".Equals(platformCode, )) { platformID = 3; } else if ("SHIPINHAO".Equals(platformCode, )) { platformID = 4; } else if ("AIKUCUN".Equals(platformCode, )) { platformID = 5; } else if ("XIAOHONGSHU".Equals(platformCode, )) { platformID = 6; } #endregion #region Optimized// Define a data dictionaryDictionary<string,int> platformDictionary = new Dictionary<string,int>(); ("PINDUODUO",1); ("DOUYIN", 2); ("KUAISHOU", 3); ("SHIPINHAO", 4); ("AIKUCUN", 5); ("XIAOHONGSHU", 6); int platformID = 0; string platformCode = "DOUYIN"; if ((platformCode)) { platformID = platformDictionary[platformCode]; } #endregion
summary
The above introduces some methods to optimize if...else statements to make the code clearer, easier to maintain and expand. Of course there are other optimization methods (Leave a message to provide your method to improve together). Hope some of the methods in this article can help you. If there are any places you can't do, please forgive me.