{"id":4959,"date":"2021-11-29T16:57:29","date_gmt":"2021-11-29T21:57:29","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=4959"},"modified":"2021-11-29T17:10:05","modified_gmt":"2021-11-29T22:10:05","slug":"benefits-of-refactoring-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2021\/11\/29\/benefits-of-refactoring-code\/","title":{"rendered":"Benefits of Refactoring Code"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\">\r\n<div class = rtcContent><h1  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 28.8px; min-height: 0px; white-space: pre-wrap; color: rgb(213, 80, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 24px; font-weight: 400; text-align: left; '><span>Benefits of Refactoring Code<\/span><\/h1><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>I have seen a lot of code in my life, including code from many different people written for many different purposes, and in many different \"styles\".  These styles range from quick-and-dirty (I only need to do this once), to fully optimized, documented, and tested (I want this to last a long time while other people use it).  For me, I have found, a bit more than I expected, that the quick-and-dirty quickly morphs into something being useful and used a lot, but without the thought and care of making sure the code is really up to the task.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Today I want to argue why, as soon as you take your quick-and-dirty code, to use again, it is time to refactor and use some good engineering techniques to whip it into shape.<\/span><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span>Refactoring leads to smaller components <\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>First break the code into logical units that are small.  Each unit is more concise, it's more focused, does less, and it's clear what it does and does not do.  Decide on edge cases and error conditions and deal with these in a way that is straight-forward and is likely to cause users of this module the least amount of trouble.<\/span><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span>Benefits of smaller components<\/span><\/h2><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Each piece is easier to understand<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>As I already said, each part does less and so it's easier to understand what each piece does. In fact, if you can get the piece of code to do one thing well, that often pays off.  One technique for doing this is to reduce the branching (<\/span><a href = \"https:\/\/www.mathworks.com\/help\/matlab\/ref\/if.html\"><span style=' font-family: monospace;'>if-elseif-else<\/span><\/a><span>) and instead have various branches relegated to separate functions.  Another technique is to use an <\/span><a href = \"https:\/\/www.mathworks.com\/help\/matlab\/ref\/arguments.html\"><span style=' font-family: monospace;'>arguments<\/span><\/a><span> block to check the input arguments.  This generally takes up fewer lines of code and you are able to be concise and precise using it.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Reduced complexity has less overhead (mental and otherwise)<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>When you have smaller components, they are frequently easier to understand, and much easier to test - especially if there are a very limited number of code paths.  You can check out the complexity of your code using either of 2 options to <\/span><a href = \"https:\/\/www.mathworks.com\/help\/matlab\/ref\/checkcode.html\"><span style=' font-family: monospace;'>checkcode<\/span><\/a><span>.<\/span><\/div><div class = 'preformatted-matlab' style = 'margin: 10px 3px 10px 55px; padding: 10px 10px 10px 5px; '><div  style = 'border-left: 0px none rgb(0, 0, 0); border-right: 0px none rgb(0, 0, 0); border-top: 0px none rgb(0, 0, 0); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 12px; '><span style=\"white-space: pre\"><span >checkcode(filename, <\/span><span style=\"color: rgb(160, 32, 240);\">\"-cyc\"<\/span><span >) <\/span><\/span><\/div><div  style = 'border-left: 0px none rgb(0, 0, 0); border-right: 0px none rgb(0, 0, 0); border-top: 0px none rgb(0, 0, 0); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 12px; '><span style=\"white-space: pre\"><span >checkcode(filename, <\/span><span style=\"color: rgb(160, 32, 240);\">\"-modcyc\"<\/span><span >)<\/span><\/span><\/div><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Complexity is reduced when you refactor the code so there are not so many nested statements like <\/span><span style=' font-family: monospace;'>if <\/span><span>and <\/span><span style=' font-family: monospace;'>switch<\/span><span> statements.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Each piece is easier to test<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>With smaller pieces, each part is easier to test, debug, check out edge conditions.  You can be certain you are covering all the bases more readily.  Of course, using the one of the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/matlab\/matlab-unit-test-framework.html\"><span>testing frameworks<\/span><\/a><span>.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Each piece is easier to reuse<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Since we're working with smaller units, it's generally easier to reuse the pieces because, I hope, we've split things up in such a way that the interfaces are simpler to use.  With each piece small, there's usually only a limited number of inputs required.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Each piece is easier to read and understand<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>With input argument lists smaller, I hope the calling syntax is shorter.  If you are calling functions that take optional arguments from within your function, the more the code complexity, the more likely code gets indented further and further to the right, either having code not in view in your window, or the argument list goes on for several lines, especially if you are using the new <\/span><a href = \"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/namevalue-in-function-calls.html\"><span>name=value<\/span><\/a><span> syntax.  In that case, each \"input\" is potentially long, possible causing extra line continuations to fit inside the editor window parameters you have set.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Example<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>I have an example from the file exchange, <\/span><a href = \"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/9817-x-steam-thermodynamic-properties-of-water-and-steam\"><span>X Steam<\/span><\/a><span>.  If you look at the code, there are lots of switch\/case statements with if-else statements inside.  How can you easily debug something near a particular line.  It's hard!  And that's despite the code not being poorly written or structured apart from this nesting.<\/span><\/div><div style=\"background-color: #F7F7F7; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(233, 233, 233); border-right: 1px solid rgb(233, 233, 233); border-top: 1px solid rgb(233, 233, 233); border-bottom: 1px solid rgb(233, 233, 233); border-radius: 4px 4px 0px 0px; padding: 6px 45px 4px 13px; line-height: 17.234px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >checkcode <\/span><span style=\"color: rgb(160, 32, 240);\">Xsteam.m -cyc<\/span><\/span><\/div><div  style = 'color: rgb(64, 64, 64); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"inlineElement eoOutputWrapper embeddedOutputsTextElement scrollableOutput\" uid=\"A775BC3B\" data-testid=\"output_0\" style=\"width: 1146px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 12px;\"><div class=\"textElement eoOutputContent\" data-width=\"1116\" data-height=\"2341\" data-hashorizontaloverflow=\"false\" style=\"max-height: 261px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 12px;\">L 159 (C 14-19): The McCabe cyclomatic complexity of 'XSteam' is 322.\r\nL 1430 (C 4-6): The value assigned to variable 'err' might be unused.\r\nL 1441 (C 18-22): The McCabe cyclomatic complexity of 'v1_pT' is 2.\r\nL 1457 (C 18-22): The McCabe cyclomatic complexity of 'h1_pT' is 2.\r\nL 1473 (C 18-22): The McCabe cyclomatic complexity of 'u1_pT' is 2.\r\nL 1491 (C 18-22): The McCabe cyclomatic complexity of 's1_pT' is 2.\r\nL 1509 (C 19-24): The McCabe cyclomatic complexity of 'Cp1_pT' is 2.\r\nL 1525 (C 19-24): The McCabe cyclomatic complexity of 'Cv1_pT' is 2.\r\nL 1547 (C 18-22): The McCabe cyclomatic complexity of 'w1_pT' is 2.\r\nL 1569 (C 18-22): The McCabe cyclomatic complexity of 'T1_ph' is 2.\r\nL 1584 (C 18-22): The McCabe cyclomatic complexity of 'T1_ps' is 2.\r\nL 1599 (C 18-22): The McCabe cyclomatic complexity of 'p1_hs' is 2.\r\nL 1614 (C 20-26): The McCabe cyclomatic complexity of 'T1_prho' is 3.\r\nL 1634 (C 18-22): The McCabe cyclomatic complexity of 'v2_pT' is 2.\r\nL 1638 (C 1-2): The value assigned to variable 'J0' might be unused.\r\nL 1639 (C 1-2): The value assigned to variable 'n0' might be unused.\r\nL 1653 (C 18-22): The McCabe cyclomatic complexity of 'h2_pT' is 3.\r\nL 1675 (C 18-22): The McCabe cyclomatic complexity of 'u2_pT' is 3.\r\nL 1701 (C 18-22): The McCabe cyclomatic complexity of 's2_pT' is 3.\r\nL 1727 (C 19-24): The McCabe cyclomatic complexity of 'Cp2_pT' is 3.\r\nL 1749 (C 19-24): The McCabe cyclomatic complexity of 'Cv2_pT' is 3.\r\nL 1777 (C 18-22): The McCabe cyclomatic complexity of 'w2_pT' is 3.\r\nL 1805 (C 18-22): The McCabe cyclomatic complexity of 'T2_ph' is 8.\r\nL 1857 (C 18-22): The McCabe cyclomatic complexity of 'T2_ps' is 8.\r\nL 1912 (C 18-22): The McCabe cyclomatic complexity of 'p2_hs' is 8.\r\nL 1966 (C 18-24): The McCabe cyclomatic complexity of 'T2_prho' is 4.\r\nL 1991 (C 20-26): The McCabe cyclomatic complexity of 'p3_rhoT' is 2.\r\nL 2000 (C 1-2): The value assigned to variable 'pc' might be unused.\r\nL 2011 (C 20-26): The McCabe cyclomatic complexity of 'u3_rhoT' is 2.\r\nL 2020 (C 1-2): The value assigned to variable 'pc' might be unused.\r\nL 2031 (C 20-26): The McCabe cyclomatic complexity of 'h3_rhoT' is 2.\r\nL 2040 (C 1-2): The value assigned to variable 'pc' might be unused.\r\nL 2053 (C 20-26): The McCabe cyclomatic complexity of 's3_rhoT' is 2.\r\nL 2062 (C 1-2): The value assigned to variable 'pc' might be unused.\r\nL 2075 (C 21-28): The McCabe cyclomatic complexity of 'Cp3_rhoT' is 2.\r\nL 2084 (C 1-2): The value assigned to variable 'pc' might be unused.\r\nL 2102 (C 21-28): The McCabe cyclomatic complexity of 'Cv3_rhoT' is 2.\r\nL 2111 (C 1-2): The value assigned to variable 'pc' might be unused.\r\nL 2121 (C 20-26): The McCabe cyclomatic complexity of 'w3_rhoT' is 2.\r\nL 2130 (C 1-2): The value assigned to variable 'pc' might be unused.\r\nL 2148 (C 18-22): The McCabe cyclomatic complexity of 'T3_ph' is 4.\r\nL 2182 (C 18-22): The McCabe cyclomatic complexity of 'v3_ph' is 4.\r\nL 2216 (C 18-22): The McCabe cyclomatic complexity of 'T3_ps' is 4.\r\nL 2249 (C 18-22): The McCabe cyclomatic complexity of 'v3_ps' is 4.\r\nL 2283 (C 18-22): The McCabe cyclomatic complexity of 'p3_hs' is 4.\r\nL 2318 (C 18-22): The McCabe cyclomatic complexity of 'h3_pT' is 5.\r\nL 2348 (C 20-26): The McCabe cyclomatic complexity of 'T3_prho' is 3.\r\nL 2369 (C 17-20): The McCabe cyclomatic complexity of 'p4_T' is 1.\r\nL 2380 (C 17-20): The McCabe cyclomatic complexity of 'T4_p' is 1.\r\nL 2391 (C 17-20): The McCabe cyclomatic complexity of 'h4_s' is 9.\r\nL 2449 (C 17-20): The McCabe cyclomatic complexity of 'p4_s' is 4.\r\nL 2462 (C 18-22): The McCabe cyclomatic complexity of 'h4L_p' is 5.\r\nL 2488 (C 18-22): The McCabe cyclomatic complexity of 'h4V_p' is 5.\r\nL 2513 (C 18-22): The McCabe cyclomatic complexity of 'x4_ph' is 3.\r\nL 2525 (C 18-22): The McCabe cyclomatic complexity of 'x4_ps' is 4.\r\nL 2541 (C 18-22): The McCabe cyclomatic complexity of 'T4_hs' is 15.\r\nL 2606 (C 18-22): The McCabe cyclomatic complexity of 'h5_pT' is 3.\r\nL 2630 (C 18-22): The McCabe cyclomatic complexity of 'v5_pT' is 2.\r\nL 2634 (C 1-3): The value assigned to variable 'Ji0' might be unused.\r\nL 2635 (C 1-3): The value assigned to variable 'ni0' might be unused.\r\nL 2650 (C 18-22): The McCabe cyclomatic complexity of 'u5_pT' is 3.\r\nL 2675 (C 19-24): The McCabe cyclomatic complexity of 'Cp5_pT' is 3.\r\nL 2698 (C 18-22): The McCabe cyclomatic complexity of 's5_pT' is 3.\r\nL 2724 (C 19-24): The McCabe cyclomatic complexity of 'Cv5_pT' is 3.\r\nL 2752 (C 18-22): The McCabe cyclomatic complexity of 'w5_pT' is 3.\r\nL 2781 (C 18-22): The McCabe cyclomatic complexity of 'T5_ph' is 3.\r\nL 2798 (C 18-22): The McCabe cyclomatic complexity of 'T5_ps' is 3.\r\nL 2814 (C 18-24): The McCabe cyclomatic complexity of 'T5_prho' is 3.\r\nL 2836 (C 22-30): The McCabe cyclomatic complexity of 'region_pT' is 15.\r\nL 2867 (C 22-30): The McCabe cyclomatic complexity of 'region_ph' is 18.\r\nL 2953 (C 22-30): The McCabe cyclomatic complexity of 'region_ps' is 16.\r\nL 3008 (C 22-30): The McCabe cyclomatic complexity of 'region_hs' is 33.\r\nL 3168 (C 24-34): The McCabe cyclomatic complexity of 'Region_prho' is 17.\r\nL 3238 (C 19-24): The McCabe cyclomatic complexity of 'B23p_T' is 1.\r\nL 3245 (C 19-24): The McCabe cyclomatic complexity of 'B23T_p' is 1.\r\nL 3255 (C 20-26): The McCabe cyclomatic complexity of 'p3sat_h' is 2.\r\nL 3271 (C 20-26): The McCabe cyclomatic complexity of 'p3sat_s' is 2.\r\nL 3285 (C 19-24): The McCabe cyclomatic complexity of 'hB13_s' is 2.\r\nL 3299 (C 20-26): The McCabe cyclomatic complexity of 'TB23_hs' is 2.\r\nL 3319 (C 29-44): The McCabe cyclomatic complexity of 'my_AllRegions_pT' is 13.\r\nL 3348 (C 1-2): The value assigned to variable 'ps' might be unused.\r\nL 3365 (C 29-44): The McCabe cyclomatic complexity of 'my_AllRegions_ph' is 14.\r\nL 3408 (C 1-2): The value assigned to variable 'ps' might be unused.\r\nL 3426 (C 21-28): The McCabe cyclomatic complexity of 'tc_ptrho' is 8.\r\nL 3444 (C 7): Consider using newline, semicolon, or comma before this statement for readability.\r\nL 3444 (C 9-10): Terminate statement with semicolon to suppress output (in functions).\r\nL 3467 (C 30-46): The McCabe cyclomatic complexity of 'Surface_Tension_T' is 3.\r\nL 3485 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_p' is 1.\r\nL 3488 (C 25-36): The McCabe cyclomatic complexity of 'fromSIunit_p' is 1.\r\nL 3491 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_T' is 1.\r\nL 3494 (C 25-36): The McCabe cyclomatic complexity of 'fromSIunit_T' is 1.\r\nL 3497 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_h' is 1.\r\nL 3499 (C 26-37): The McCabe cyclomatic complexity of 'fromSIunit_h' is 1.\r\nL 3501 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_v' is 1.\r\nL 3503 (C 25-36): The McCabe cyclomatic complexity of 'fromSIunit_v' is 1.\r\nL 3505 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_s' is 1.\r\nL 3507 (C 25-36): The McCabe cyclomatic complexity of 'fromSIunit_s' is 1.\r\nL 3509 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_u' is 1.\r\nL 3511 (C 25-36): The McCabe cyclomatic complexity of 'fromSIunit_u' is 1.\r\nL 3513 (C 24-34): The McCabe cyclomatic complexity of 'toSIunit_Cp' is 1.\r\nL 3515 (C 26-38): The McCabe cyclomatic complexity of 'fromSIunit_Cp' is 1.\r\nL 3517 (C 24-34): The McCabe cyclomatic complexity of 'toSIunit_Cv' is 1.\r\nL 3519 (C 26-38): The McCabe cyclomatic complexity of 'fromSIunit_Cv' is 1.\r\nL 3521 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_w' is 1.\r\nL 3523 (C 25-36): The McCabe cyclomatic complexity of 'fromSIunit_w' is 1.\r\nL 3525 (C 24-34): The McCabe cyclomatic complexity of 'toSIunit_tc' is 1.\r\nL 3527 (C 26-38): The McCabe cyclomatic complexity of 'fromSIunit_tc' is 1.\r\nL 3529 (C 24-34): The McCabe cyclomatic complexity of 'toSIunit_st' is 1.\r\nL 3531 (C 26-38): The McCabe cyclomatic complexity of 'fromSIunit_st' is 1.\r\nL 3533 (C 23-32): The McCabe cyclomatic complexity of 'toSIunit_x' is 1.\r\nL 3535 (C 25-36): The McCabe cyclomatic complexity of 'fromSIunit_x' is 1.\r\nL 3537 (C 24-34): The McCabe cyclomatic complexity of 'toSIunit_vx' is 1.\r\nL 3539 (C 26-38): The McCabe cyclomatic complexity of 'fromSIunit_vx' is 1.\r\nL 3541 (C 24-34): The McCabe cyclomatic complexity of 'toSIunit_my' is 1.\r\nL 3543 (C 26-38): The McCabe cyclomatic complexity of 'fromSIunit_my' is 1.\r\nL 3550 (C 16-20): The McCabe cyclomatic complexity of 'check' is 28.\r\nL 3570 (C 14): Terminate statement with semicolon to suppress output (in functions).\r\nL 3571 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3581 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3582 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3592 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3593 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3605 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3606 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3626 (C 14): Terminate statement with semicolon to suppress output (in functions).\r\nL 3627 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3637 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3638 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3648 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3649 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3660 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3661 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3681 (C 14): Terminate statement with semicolon to suppress output (in functions).\r\nL 3682 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3692 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3693 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3703 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3704 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3714 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3715 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3725 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3726 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3736 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3737 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3747 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3748 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3755 (C 1-2): The preallocated value assigned to variable 'R3' might be unused.\r\nL 3757 (C 5-6): The variable 'R4' appears to change size on every loop iteration. Consider preallocating for speed.\r\nL 3759 (C 11): Terminate statement with semicolon to suppress output (in functions).\r\nL 3760 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3764 (C 1-2): The preallocated value assigned to variable 'R3' might be unused.\r\nL 3768 (C 11): Terminate statement with semicolon to suppress output (in functions).\r\nL 3769 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3773 (C 1-2): The value assigned to variable 'R3' might be unused.\r\nL 3777 (C 11): Terminate statement with semicolon to suppress output (in functions).\r\nL 3778 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3798 (C 14): Terminate statement with semicolon to suppress output (in functions).\r\nL 3799 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3809 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3810 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3820 (C 12): Terminate statement with semicolon to suppress output (in functions).\r\nL 3821 (C 4): Terminate statement with semicolon to suppress output (in functions).\r\nL 3835 (C 17-23): If you are operating on scalar values, consider using STR2DOUBLE for faster performance.\r\nL 3835 (C 45-51): If you are operating on scalar values, consider using STR2DOUBLE for faster performance.\r\nL 3836 (C 5-9): The value assigned to variable 'Check' might be unused.\r\nL 3836 (C 10): Terminate statement with semicolon to suppress output (in functions).\r\nL 3838 (C 9-11): The value assigned to variable 'err' might be unused.<\/div><\/div><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>With this many paths through the code, 322, what are the chances that there are no issues, despite the refactored functions?  If I were using this for some work I wanted to publish, I would need to make sure that all the paths I used were correctly computing what I need.  Since that's a hassle, I'd likely refactor the code.<\/span><\/div><h4  style = 'margin: 10px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 15px; font-weight: 700; text-align: left; '><span>Refactor - how?<\/span><\/h4><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Apart from going in the code and copy\/pasting to elsewhere (could be in the same file), you can use the tools in the editor toolstrip or from the right-click context menu once you've made your selection.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/loren\/files\/refactorCode_01.png\" width = \"481\" height = \"149\" alt = \"\" style = \"vertical-align: baseline; width: 481px; height: 149px;\"><\/img><\/div><h3  style = 'margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span>How do you deal with your piles of code?<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Do you let the code rule you or do you rule your code?  Please post any additional techniques or benefits I have not mentioned right <\/span><a href = \"#respond\"><span>here<\/span><\/a><span>.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span style=' font-style: italic; font-family: monospace;'><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span style=' font-style: italic; font-family: monospace;'>Copyright 2021 The MathWorks, Inc.<\/span><\/div>\r\n<script type=\"text\/javascript\">var css = '.variableValue { width: 100% !important; } .embeddedOutputsMatrixElement,.eoOutputWrapper .matrixElement {    min-height: 18px;    box-sizing: border-box;} .embeddedOutputsMatrixElement .matrixElement,.eoOutputWrapper  .matrixElement,.rtcDataTipElement .matrixElement {    position: relative;} .matrixElement .variableValue,.rtcDataTipElement .matrixElement .variableValue {    white-space: pre;    display: inline-block;    vertical-align: top;    overflow: hidden;} .embeddedOutputsMatrixElement.inlineElement {} .embeddedOutputsMatrixElement.inlineElement .topHeaderWrapper {    display: none;} .embeddedOutputsMatrixElement.inlineElement .veTable .body {    padding-top: 0 !important;    max-height: 100px;} .inlineElement .matrixElement {    max-height: 300px;} .embeddedOutputsMatrixElement.rightPaneElement {} .rightPaneElement .matrixElement,.rtcDataTipElement .matrixElement {    overflow: hidden;    padding-left: 9px;} .rightPaneElement .matrixElement {    margin-bottom: -1px;} .embeddedOutputsMatrixElement .matrixElement .valueContainer,.eoOutputWrapper .matrixElement .valueContainer,.rtcDataTipElement .matrixElement .valueContainer {    white-space: nowrap;    margin-bottom: 3px;} .embeddedOutputsMatrixElement .matrixElement .valueContainer .horizontalEllipsis.hide,.embeddedOutputsMatrixElement .matrixElement .verticalEllipsis.hide,.eoOutputWrapper .matrixElement .valueContainer .horizontalEllipsis.hide,.eoOutputWrapper .matrixElement .verticalEllipsis.hide,.rtcDataTipElement .matrixElement .valueContainer .horizontalEllipsis.hide,.rtcDataTipElement .matrixElement .verticalEllipsis.hide {    display: none;} .embeddedOutputsVariableMatrixElement .matrixElement .valueContainer.hideEllipses .verticalEllipsis, .embeddedOutputsVariableMatrixElement .matrixElement .valueContainer.hideEllipses .horizontalEllipsis {    display:none;} .embeddedOutputsMatrixElement .matrixElement .valueContainer .horizontalEllipsis,.eoOutputWrapper .matrixElement .valueContainer .horizontalEllipsis {    margin-bottom: -3px;} .eoOutputWrapper .embeddedOutputsVariableMatrixElement .matrixElement .valueContainer {    cursor: default !important;} .embeddedOutputsVariableElement {    white-space: pre-wrap;    word-wrap: break-word;    min-height: 18px;    max-height: 250px;    overflow: auto;} .variableElement {} .embeddedOutputsVariableElement.inlineElement {} .inlineElement .variableElement {} .embeddedOutputsVariableElement.rightPaneElement {    min-height: 16px;} .rightPaneElement .variableElement {    padding-top: 2px;    padding-left: 9px;} .variableNameElement {    margin-bottom: 3px;    display: inline-block;} \/* * Ellipses as base64 for HTML export. *\/.matrixElement .horizontalEllipsis,.rtcDataTipElement .matrixElement .horizontalEllipsis {    display: inline-block;    margin-top: 3px;    \/* base64 encoded version of images-liveeditor\/HEllipsis.png *\/    width: 30px;    height: 12px;    background-repeat: no-repeat;    background-image: url(\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAJCAYAAADO1CeCAAAAJUlEQVR42mP4\/\/8\/A70xw0i29BUDFPxnAEtTW37wWDqakIa4pQDvOOG89lHX2gAAAABJRU5ErkJggg==\");} .matrixElement .verticalEllipsis,.textElement .verticalEllipsis,.rtcDataTipElement .matrixElement .verticalEllipsis,.rtcDataTipElement .textElement .verticalEllipsis {    margin-left: 35px;    \/* base64 encoded version of images-liveeditor\/VEllipsis.png *\/    width: 12px;    height: 30px;    background-repeat: no-repeat;    background-image: url(\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAZCAYAAAAIcL+IAAAALklEQVR42mP4\/\/8\/AzGYgWyFMECMwv8QddRS+P\/\/KyimlmcGUOFoOI6GI\/UVAgDnd8Dd4+NCwgAAAABJRU5ErkJggg==\");} \/* Styling that is common to warnings and errors is in diagnosticOutput.css *\/.embeddedOutputsErrorElement {    min-height: 18px;    max-height: 250px;    overflow: auto;} .embeddedOutputsErrorElement.inlineElement {} .embeddedOutputsErrorElement.rightPaneElement {} \/* Styling that is common to warnings and errors is in diagnosticOutput.css *\/.embeddedOutputsWarningElement{    min-height: 18px;    max-height: 250px;    overflow: auto;} .embeddedOutputsWarningElement.inlineElement {} .embeddedOutputsWarningElement.rightPaneElement {} \/* Copyright 2015-2019 The MathWorks, Inc. *\/\/* In this file, styles are not scoped to rtcContainer since they could be in the Dojo Tooltip *\/.diagnosticMessage-wrapper {    font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace;    font-size: 12px;} .diagnosticMessage-wrapper.diagnosticMessage-warningType {    color: rgb(255,100,0);} .diagnosticMessage-wrapper.diagnosticMessage-warningType a {    color: rgb(255,100,0);    text-decoration: underline;} .diagnosticMessage-wrapper.diagnosticMessage-errorType {    color: rgb(230,0,0);} .diagnosticMessage-wrapper.diagnosticMessage-errorType a {    color: rgb(230,0,0);    text-decoration: underline;} .diagnosticMessage-wrapper .diagnosticMessage-messagePart,.diagnosticMessage-wrapper .diagnosticMessage-causePart {    white-space: pre-wrap;} .diagnosticMessage-wrapper .diagnosticMessage-stackPart {    white-space: pre;} .embeddedOutputsTextElement,.embeddedOutputsVariableStringElement {    white-space: pre;    word-wrap:  initial;    min-height: 18px;    max-height: 250px;    overflow: auto;} .textElement,.rtcDataTipElement .textElement {    padding-top: 3px;} .embeddedOutputsTextElement.inlineElement,.embeddedOutputsVariableStringElement.inlineElement {} .inlineElement .textElement {} .embeddedOutputsTextElement.rightPaneElement,.embeddedOutputsVariableStringElement.rightPaneElement {    min-height: 16px;} .rightPaneElement .textElement {    padding-top: 2px;    padding-left: 9px;} .embeddedOutputsVariableTableElement .ClientViewDiv  table tr {  height: 22px;  white-space: nowrap;} .embeddedOutputsVariableTableElement .ClientViewDiv  table tr td,.embeddedOutputsVariableTableElement .ClientViewDiv  table tr th {  background-color:white;  text-overflow: ellipsis;  font-family: Arial, sans-serif;  font-size: 12px;  overflow : hidden;} .embeddedOutputsVariableTableElement .ClientViewDiv  table tr span {  text-overflow: ellipsis;  padding: 3px;} .embeddedOutputsVariableTableElement .ClientViewDiv  table tr th {    color: rgba(0,0,0,0.5);  padding: 3px;  font-size: 9px;}'; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script><a href=\"https:\/\/blogs.mathworks.com\/loren\/files\/refactorCode.mlx\"><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a>","protected":false},"excerpt":{"rendered":"<p>\r\nBenefits of Refactoring CodeI have seen a lot of code in my life, including code from many different people written for many different purposes, and in many different \"styles\".  These styles range... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2021\/11\/29\/benefits-of-refactoring-code\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,79,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/4959"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=4959"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/4959\/revisions"}],"predecessor-version":[{"id":4965,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/4959\/revisions\/4965"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=4959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=4959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=4959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}