Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Navitabs tab wizard
{"type":"custom","custom":{"tabs":[{"id":"75156eac-6ae8-4d85-b66a-ab4859ac3a8e","value":"C++","body":{"version":1,"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 1 - Retirement age example:"}]},{"type":"paragraph","content":[{"type":"text","text":"Recall our previous retirement age example:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"constexpr unsigned int RETIREMENT_AGE_IN_YEARS = 65;\nif (user_age_in_years > RETIREMENT_AGE_IN_YEARS)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"We made progress in making the code communicate more understanding to readers with the constant for the retirement age. However, what does the boolean expression in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement mean? This is a simple example that one might be able to guess, but it still requires some extra mental parsing for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement condition. Introducing a local boolean variable for this expression can help make the intended condition for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement clearer:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"constexpr unsigned int RETIREMENT_AGE_IN_YEARS = 65;\nconst bool user_over_retirement_age = (user_age_in_years > RETIREMENT_AGE_IN_YEARS);\nif (user_over_retirement_age)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"The boolean expression assigned to the boolean variable still has to be mentally parsed to verify correctness, of course. However, once that is done and assigned to a well-named boolean variable like "},{"type":"text","text":"user_over_retirement_age","marks":[{"type":"code"}]},{"type":"text","text":", those details can be forgotten, and a reader can focus on the meaning of the condition in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement and resulting logic. And if the actual intended condition were something different than just “a user being over the retirement age”, such a local boolean would be even more important."}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 2 - Checking the number of entries in a collection"}]},{"type":"paragraph","content":[{"type":"text","text":"In the first version of this code, the reader is required to process the evaluation made inside the if and determine what they think it means. If instead the statement read "},{"type":"text","text":"entries.size() < MAX_ALLOWED_ENTRIES","marks":[{"type":"code"}]},{"type":"text","text":", the reader wouldn't necessarily know if that was the correct intent of the developer:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"if (MAX_ALLOWED_ENTRIES == entries.size())\n{\n    // Do something\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"A well-named local boolean variable helps make intent clearer:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"const bool max_entries_stored = (MAX_ALLOWED_ENTRIES == entries.size());\nif (max_entries_stored)\n{\n    // Do something\n}"}]},{"type":"mediaSingleheading","attrs":{"layoutlevel":"center"1},"content":[{"type":"mediatext","attrstext":{"type":"file","id":"70f87035-81e7-408d-afd4-191738d70403","alt":"Deep coding during work hours ad.png","collection":"contentId-103415913","height":376,"width":1820},"marks":["Example 3 - A complex, multi-part boolean expression"}]},{"type":"linkparagraph","attrs":{"href":"https://www.pulsepoint-ai.com/developers"}}]}]},content":[{"type":"headingtext","attrstext":{"level":1},"content":[{"type":"text","text":"Example 3 - A complex, multi-part boolean expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The "The above are the simplest examples of a more complex expression - only two terms. However, using well-named variables becomes even more important as expressions get more complex. Consider the following example:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"if (user_age_in_years > 18 && user_country == \"USA\" && user_state == \"California\")\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"Good luck guessing the intended condition there. If a local boolean variable is introduced to capture the meaning of the condition, then the code is clearer (and if the specifics of the condition need to be modified, then they will typically be easier to modify if not in the middle of an "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement):"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"const bool user_eligible_for_california_program = (\n    (user_age_in_years > 18) &&\n    (user_country == \"USA\") &&\n    (user_state == \"California\"));\nif (user_eligible_for_california_program)\n{\n    // Do something...\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 4 - Introducing multiple intermediate variables for a complex numeric expression:"}]},{"type":"paragraph","content":[{"type":"text","text":"The above example shows a single variable being introduced for a more complex expression. However, there are times where it may be beneficial to introduce "},{"type":"text","text":"multiple","marks":[{"type":"em"}]},{"type":"text","text":" intermediate variables to break down a complex expression into smaller chunks that are easier for a human to mentally comprehend. Consider the following code:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"double total_cost = (\n    (quantity * unit_price) + \n    (quantity * unit_price * 0.05) +\n    (quantity * shipping_cost_per_unit));"}]},{"type":"paragraph","content":[{"type":"text","text":"The individual parts of the multi-part expression are hard to comprehend.  Breaking up the expression across more intermediate variables/constants makes the meaning easier to understand:"}]},{"type":"codeBlock","attrs":{"language":"c++"},"content":[{"type":"text","text":"const double TAX_RATE = 0.05;\ndouble subtotal = quantity * unit_price;\ndouble tax_amount = TAX_RATE * subtotal;\ndouble shipping_cost = quantity * shipping_cost_per_unit;\ndouble total_cost = subtotal + tax_amount + shipping_cost;"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"25d9e3cf-12a3-4992-974f-4b5999cb2343","value":"C#","body":{"version":1,"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 1 - Retirement age example"}]},{"type":"paragraph","content":[{"type":"text","text":"Recall our previous retirement age example:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"const uint RetirementAgeInYears = 65;\nif (userAgeInYears > RetirementAgeInYears)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"We made progress in making the code communicate more understanding to readers with the constant for the retirement age. However, what does the boolean expression in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement mean? This is a simple example that one might be able to guess, but it still requires some extra mental parsing for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement condition. Introducing a local boolean variable for this expression can help make the intended condition for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement clearer:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"const uint RetirementAgeInYears = 65;\nbool userOverRetirementAge = (userAgeInYears > RetirementAgeInYears);\nif (userOverRetirementAge)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"The boolean expression assigned to the boolean variable still has to be mentally parsed to verify correctness, of course. However, once that is done and assigned to a well-named boolean variable like "},{"type":"text","text":"userOverRetirementAge","marks":[{"type":"code"}]},{"type":"text","text":", those details can be forgotten, and a reader can focus on the meaning of the condition in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement and resulting logic. And if the actual intended condition were something different than just “a user being over the retirement age”, such a local boolean would be even more important."}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 2 - Checking the number of entries in a collection"}]},{"type":"paragraph","content":[{"type":"text","text":"In the first version of this code, the reader is required to process the evaluation made inside the if and determine what they think it means. If instead the statement read "},{"type":"text","text":"entries.Count < MaxAllowedEntries","marks":[{"type":"code"}]},{"type":"text","text":", the reader wouldn't necessarily know if that was the correct intent of the developer:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"if (MaxAllowedEntries == entries.Count)\n{\n    // Do something\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"A well-named local boolean variable helps make intent clearer:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"bool maxEntriesStored = (MaxAllowedEntries == entries.Count);\nif (maxEntriesStored)\n{\n    // Do something\n}"}]},{"type":"mediaSingle","attrs":{"layout":"center"},"content":[{"type":"media","attrs":{"type":"file","id":"70f87035-81e7-408d-afd4-191738d70403","alt":"Deep coding during work hours ad.png","collection":"contentId-103415913","height":376,"width":1820},"marks":[{"type":"link","attrs":{"href":"https://www.pulsepoint-ai.com/developers"}}]}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 3 - A complex, multi-part boolean expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above are the simplest examples of a more complex expression - only two terms. However, using well-named variables becomes even more important as expressions get more complex. Consider the following example:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"if (userAgeInYears > 18 && userCountry == \"USA\" && userState == \"California\")\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"Good luck guessing the intended condition there. If a local boolean variable is introduced to capture the meaning of the condition, then the code is clearer (and if the specifics of the condition need to be modified, then they will typically be easier to modify if not in the middle of an "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement):"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"bool userEligibleForCaliforniaProgram = (\n    (userAgeInYears > 18) &&\n    (userCountry == \"USA\") &&\n    (userState == \"California\"));\nif (userEligibleForCaliforniaProgram)\n{\n    // Do something...\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 4 - Introducing multiple intermediate variables for a complex numeric expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above example shows a single variable being introduced for a more complex expression. However, there are times where it may be beneficial to introduce "},{"type":"text","text":"multiple","marks":[{"type":"em"}]},{"type":"text","text":" intermediate variables to break down a complex expression into smaller chunks that are easier for a human to mentally comprehend. Consider the following code:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"double totalCost = (\n    (quantity * unitPrice) + \n    (quantity * unitPrice * 0.05) +\n    (quantity * shippingCostPerUnit));"}]},{"type":"paragraph","content":[{"type":"text","text":"The individual parts of the multi-part expression are hard to comprehend.  Breaking up the expression across more intermediate variables/constants makes the meaning easier to understand:"}]},{"type":"codeBlock","attrs":{"language":"csharp"},"content":[{"type":"text","text":"const double TaxRate = 0.05;\ndouble subtotal = quantity * unitPrice;\ndouble taxAmount = TaxRate * subtotal;\ndouble shippingCost = quantity * shippingCostPerUnit;\ndouble totalCost = subtotal + taxAmount + shippingCost;"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"b0663965-36d1-4bf9-980a-8c18ddd262fb","value":"Java","body":{"version":1,"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 1 - Retirement age example"}]},{"type":"paragraph","content":[{"type":"text","text":"Recall our previous retirement age example:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"final int RETIREMENT_AGE_IN_YEARS = 65;\nif (userAgeInYears > RETIREMENT_AGE_IN_YEARS)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"We made progress in making the code communicate more understanding to readers with the constant for the retirement age. However, what does the boolean expression in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement mean? This is a simple example that one might be able to guess, but it still requires some extra mental parsing for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement condition. Introducing a local boolean variable for this expression can help make the intended condition for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement clearer:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"final int RETIREMENT_AGE_IN_YEARS = 65;\nboolean userOverRetirementAge = (userAgeInYears > RETIREMENT_AGE_IN_YEARS);\nif (userOverRetirementAge)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"The boolean expression assigned to the boolean variable still has to be mentally parsed to verify correctness, of course. However, once that is done and assigned to a well-named boolean variable like "},{"type":"text","text":"userOverRetirementAge","marks":[{"type":"code"}]},{"type":"text","text":", those details can be forgotten, and a reader can focus on the meaning of the condition in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement and resulting logic. And if the actual intended condition were something different than just “a user being over the retirement age”, such a local boolean would be even more important."}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 2 - Checking the number of entries in a collection"}]},{"type":"paragraph","content":[{"type":"text","text":"In the first version of this code, the reader is required to process the evaluation made inside the if and determine what they think it means. If instead the statement read "},{"type":"text","text":"entries.size() < MAX_ALLOWED_ENTRIES","marks":[{"type":"code"}]},{"type":"text","text":", the reader wouldn't necessarily know if that was the correct intent of the developer:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"if (MAX_ALLOWED_ENTRIES == entries.size())\n{\n    // Do something\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"A well-named local boolean variable helps make intent clearer:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"boolean maxEntriesStored = (MAX_ALLOWED_ENTRIES == entries.size());\nif (maxEntriesStored)\n{\n    // Do something\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 3 - A complex, multi-part boolean expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above are the simplest examples of a more complex expression - only two terms. However, using well-named variables becomes even more important as expressions get more complex. Consider the following example:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"if (userAgeInYears > 18 && userCountry.equals(\"USA\") && userState.equals(\"California\"))\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"Good luck guessing the intended condition there. If a local boolean variable is introduced to capture the meaning of the condition, then the code is clearer (and if the specifics of the condition need to be modified, then they will typically be easier to modify if not in the middle of an "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement):"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"boolean userEligibleForCaliforniaProgram = (\n    (userAgeInYears > 18) &&\n    (userCountry.equals(\"USA\")) &&\n    (userState.equals(\"California\")));\nif (userEligibleForCaliforniaProgram)\n{\n    // Do something...\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 4 - Introducing multiple intermediate variables for a complex numeric expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above example shows a single variable being introduced for a more complex expression. However, there are times where it may be beneficial to introduce "},{"type":"text","text":"multiple","marks":[{"type":"em"}]},{"type":"text","text":" intermediate variables to break down a complex expression into smaller chunks that are easier for a human to mentally comprehend. Consider the following code:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"double totalCost = (\n    (quantity * unitPrice) + \n    (quantity * unitPrice * 0.05) +\n    (quantity * shippingCostPerUnit));"}]},{"type":"paragraph","content":[{"type":"text","text":"The individual parts of the multi-part expression are hard to comprehend.  Breaking up the expression across more intermediate variables/constants makes the meaning easier to understand:"}]},{"type":"codeBlock","attrs":{"language":"java"},"content":[{"type":"text","text":"final double TAX_RATE = 0.05;\ndouble subtotal = quantity * unitPrice;\ndouble taxAmount = TAX_RATE * subtotal;\ndouble shippingCost = quantity * shippingCostPerUnit;\ndouble totalCost = subtotal + taxAmount + shippingCost;"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"102ff972-e816-4279-b3d8-66326c2c80ff","value":"Python","body":{"version":1,"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 1 - Retirement age example"}]},{"type":"paragraph","content":[{"type":"text","text":"Recall our previous retirement age example:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"RETIREMENT_AGE_IN_YEARS: int = 65\nif user_age_in_years > RETIREMENT_AGE_IN_YEARS:\n    # Do something..."}]},{"type":"paragraph","content":[{"type":"text","text":"We made progress in making the code communicate more understanding to readers with the constant for the retirement age. However, what does the boolean expression in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement mean? This is a simple example that one might be able to guess, but it still requires some extra mental parsing for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement condition. Introducing a local boolean variable for this expression can help make the intended condition for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement clearer:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"RETIREMENT_AGE_IN_YEARS: int = 65\nuser_over_retirement_age: bool = (user_age_in_years > RETIREMENT_AGE_IN_YEARS)\nif user_over_retirement_age:\n    # Do something..."}]},{"type":"paragraph","content":[{"type":"text","text":"The boolean expression assigned to the boolean variable still has to be mentally parsed to verify correctness, of course. However, once that is done and assigned to a well-named boolean variable like "},{"type":"text","text":"user_over_retirement_age","marks":[{"type":"code"}]},{"type":"text","text":", those details can be forgotten, and a reader can focus on the meaning of the condition in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement and resulting logic. And if the actual intended condition were something different than just “a user being over the retirement age”, such a local boolean would be even more important."}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 2 - Checking the number of entries in a collection"}]},{"type":"paragraph","content":[{"type":"text","text":"In the first version of this code, the reader is required to process the evaluation made inside the if and determine what they think it means. If instead the statement read "},{"type":"text","text":"len(entries) < MAX_ALLOWED_ENTRIES","marks":[{"type":"code"}]},{"type":"text","text":", the reader wouldn't necessarily know if that was the correct intent of the developer:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"if MAX_ALLOWED_ENTRIES == len(entries):\n    # Do something"}]},{"type":"paragraph","content":[{"type":"text","text":"A well-named local boolean variable helps make intent clearer:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"max_entries_stored: bool = (MAX_ALLOWED_ENTRIES == len(entries))\nif max_entries_stored:\n    # Do something"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 3 - A complex, multi-part boolean expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above are the simplest examples of a more complex expression - only two terms. However, using well-named variables becomes even more important as expressions get more complex. Consider the following example:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"if user_age_in_years > 18 and user_country == \"USA\" and user_state == \"California\":\n    # Do something..."}]},{"type":"paragraph","content":[{"type":"text","text":"Good luck guessing the intended condition there. If a local boolean variable is introduced to capture the meaning of the condition, then the code is clearer (and if the specifics of the condition need to be modified, then they will typically be easier to modify if not in the middle of an "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement):"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"user_eligible_for_california_program: bool = (\n    (user_age_in_years > 18) and\n    (user_country == \"USA\") and\n    (user_state == \"California\"))\nif user_eligible_for_california_program:\n    # Do something..."}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 4 - Introducing multiple intermediate variables for a complex numeric expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above example shows a single variable being introduced for a more complex expression. However, there are times where it may be beneficial to introduce "},{"type":"text","text":"multiple","marks":[{"type":"em"}]},{"type":"text","text":" intermediate variables to break down a complex expression into smaller chunks that are easier for a human to mentally comprehend. Consider the following code:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"total_cost: float = (\n    (quantity * unit_price) + \n    (quantity * unit_price * 0.05) +\n    (quantity * shipping_cost_per_unit));"}]},{"type":"paragraph","content":[{"type":"text","text":"The individual parts of the multi-part expression are hard to comprehend.  Breaking up the expression across more intermediate variables/constants makes the meaning easier to understand:"}]},{"type":"codeBlock","attrs":{"language":"python"},"content":[{"type":"text","text":"TAX_RATE: float = 0.05\nsubtotal: float = quantity * unit_price\ntax_amount: float = TAX_RATE * subtotal\nshipping_cost: float = (quantity * shipping_cost_per_unit)\ntotal_cost: float = subtotal + tax_amount + shipping_cost"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"a5f77970-2e97-46bd-a15e-e0a86b51105c","value":"JavaScript","body":{"version":1,"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 1 - Retirement age example"}]},{"type":"paragraph","content":[{"type":"text","text":"Recall our previous retirement age example:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"const RETIREMENT_AGE_IN_YEARS = 65;\nif (userAgeInYears > RETIREMENT_AGE_IN_YEARS)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"We made progress in making the code communicate more understanding to readers with the constant for the retirement age. However, what does the boolean expression in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement mean? This is a simple example that one might be able to guess, but it still requires some extra mental parsing for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement condition. Introducing a local boolean variable for this expression can help make the intended condition for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement clearer:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"const RETIREMENT_AGE_IN_YEARS = 65;\nconst userOverRetirementAge = (userAgeInYears > RETIREMENT_AGE_IN_YEARS);\nif (userOverRetirementAge)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"The boolean expression assigned to the boolean variable still has to be mentally parsed to verify correctness, of course. However, once that is done and assigned to a well-named boolean variable like "},{"type":"text","text":"userOverRetirementAge","marks":[{"type":"code"}]},{"type":"text","text":", those details can be forgotten, and a reader can focus on the meaning of the condition in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement and resulting logic. And if the actual intended condition were something different than just “a user being over the retirement age”, such a local boolean would be even more important."}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 2 - Checking the number of entries in a collection"}]},{"type":"paragraph","content":[{"type":"text","text":"In the first version of this code, the reader is required to process the evaluation made inside the if and determine what they think it means. If instead the statement read "},{"type":"text","text":"entries.length < MAX_ALLOWED_ENTRIES","marks":[{"type":"code"}]},{"type":"text","text":", the reader wouldn't necessarily know if that was the correct intent of the developer:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"if (MAX_ALLOWED_ENTRIES === entries.length)\n{\n    // Do something\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"A well-named local boolean variable helps make intent clearer:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"const maxEntriesStored = (MAX_ALLOWED_ENTRIES === entries.length);\nif (maxEntriesStored)\n{\n    // Do something\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 3 - A complex, multi-part boolean expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above are the simplest examples of a more complex expression - only two terms. However, using well-named variables becomes even more important as expressions get more complex. Consider the following example:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"if (userAgeInYears > 18 && userCountry === \"USA\" && userState === \"California\")\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"Good luck guessing the intended condition there. If a local boolean variable is introduced to capture the meaning of the condition, then the code is clearer (and if the specifics of the condition need to be modified, then they will typically be easier to modify if not in the middle of an "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement):"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"const userEligibleForCaliforniaProgram = (\n    (userAgeInYears > 18) &&\n    (userCountry === \"USA\") &&\n    (userState === \"California\"));\nif (userEligibleForCaliforniaProgram)\n{\n    // Do something...\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 4 - Introducing multiple intermediate variables for a complex numeric expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above example shows a single variable being introduced for a more complex expression. However, there are times where it may be beneficial to introduce "},{"type":"text","text":"multiple","marks":[{"type":"em"}]},{"type":"text","text":" intermediate variables to break down a complex expression into smaller chunks that are easier for a human to mentally comprehend. Consider the following code:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"let totalCost = (\n    (quantity * unitPrice) + \n    (quantity * unitPrice * 0.05) +\n    (quantity * shippingCostPerUnit));"}]},{"type":"paragraph","content":[{"type":"text","text":"The individual parts of the multi-part expression are hard to comprehend.  Breaking up the expression across more intermediate variables/constants makes the meaning easier to understand:"}]},{"type":"codeBlock","attrs":{"language":"javascript"},"content":[{"type":"text","text":"const TAX_RATE = 0.05;\nlet subtotal = quantity * unitPrice;\nlet taxAmount = TAX_RATE * subtotal;\nlet shippingCost = quantity * shippingCostPerUnit;\nlet totalCost = subtotal + taxAmount + shippingCost;"}]},{"type":"paragraph","content":[]}]},"type":"your-content"},{"id":"e25d75d8-7245-4021-b54b-aeadc6dcae40","value":"TypeScript","body":{"version":1,"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 1 - Retirement age example"}]},{"type":"paragraph","content":[{"type":"text","text":"Recall our previous retirement age example:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"const RETIREMENT_AGE_IN_YEARS: number = 65;\nif (userAgeInYears > RETIREMENT_AGE_IN_YEARS)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"We made progress in making the code communicate more understanding to readers with the constant for the retirement age. However, what does the boolean expression in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement mean? This is a simple example that one might be able to guess, but it still requires some extra mental parsing for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement condition. Introducing a local boolean variable for this expression can help make the intended condition for the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement clearer:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"const RETIREMENT_AGE_IN_YEARS: number = 65;\nconst userOverRetirementAge: boolean = (userAgeInYears > RETIREMENT_AGE_IN_YEARS);\nif (userOverRetirementAge)\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"The boolean expression assigned to the boolean variable still has to be mentally parsed to verify correctness, of course. However, once that is done and assigned to a well-named boolean variable like "},{"type":"text","text":"userOverRetirementAge","marks":[{"type":"code"}]},{"type":"text","text":", those details can be forgotten, and a reader can focus on the meaning of the condition in the "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement and resulting logic. And if the actual intended condition were something different than just “a user being over the retirement age”, such a local boolean would be even more important."}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 2 - Checking the number of entries in a collection"}]},{"type":"paragraph","content":[{"type":"text","text":"In the first version of this code, the reader is required to process the evaluation made inside the if and determine what they think it means. If instead the statement read "},{"type":"text","text":"entries.length < MAX_ALLOWED_ENTRIES","marks":[{"type":"code"}]},{"type":"text","text":", the reader wouldn't necessarily know if that was the correct intent of the developer:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"if (MAX_ALLOWED_ENTRIES === entries.length)\n{\n    // Do something\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"A well-named local boolean variable helps make intent clearer:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"const maxEntriesStored: boolean = (MAX_ALLOWED_ENTRIES === entries.length);\nif (maxEntriesStored)\n{\n    // Do something\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 3 - A complex, multi-part boolean expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above are the simplest examples of a more complex expression - only two terms. However, using well-named variables becomes even more important as expressions get more complex. Consider the following example:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"if (userAgeInYears > 18 && userCountry === \"USA\" && userState === \"California\")\n{\n    // Do something...\n}"}]},{"type":"paragraph","content":[{"type":"text","text":"Good luck guessing the intended condition there. If a local boolean variable is introduced to capture the meaning of the condition, then the code is clearer (and if the specifics of the condition need to be modified, then they will typically be easier to modify if not in the middle of an "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" statement):"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"const userEligibleForCaliforniaProgram: boolean = (\n    (userAgeInYears > 18) &&\n    (userCountry === \"USA\") &&\n    (userState === \"California\"));\nif (userEligibleForCaliforniaProgram)\n{\n    // Do something...\n}"}]},{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Example 4 - Introducing multiple intermediate variables for a complex numeric expression"}]},{"type":"paragraph","content":[{"type":"text","text":"The above example shows a single variable being introduced for a more complex expression. However, there are times where it may be beneficial to introduce "},{"type":"text","text":"multiple","marks":[{"type":"em"}]},{"type":"text","text":" intermediate variables to break down a complex expression into smaller chunks that are easier for a human to mentally comprehend. Consider the following code:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"let totalCost: number = (\n    (quantity * unitPrice) + \n    (quantity * unitPrice * 0.05) +\n    (quantity * shippingCostPerUnit));"}]},{"type":"paragraph","content":[{"type":"text","text":"The individual parts of the multi-part expression are hard to comprehend.  Breaking up the expression across more intermediate variables/constants makes the meaning easier to understand:"}]},{"type":"codeBlock","attrs":{"language":"typescript"},"content":[{"type":"text","text":"const TAX_RATE: number = 0.05;\nlet subtotal: number = quantity * unitPrice;\nlet taxAmount: number = TAX_RATE * subtotal;\nlet shippingCost: number = quantity * shippingCostPerUnit;\nlet totalCost: number = subtotal + taxAmount + shippingCost;"}]},{"type":"paragraph","content":[]}]},"type":"your-content"}],"design":null,"minHeight":0,"vertical":false,"showTabCard":true,"isEmbedded":false}}

...