{"id":94,"date":"2025-08-30T04:16:37","date_gmt":"2025-08-30T04:16:37","guid":{"rendered":"http:\/\/katzcs.com\/?page_id=94"},"modified":"2025-08-30T04:16:37","modified_gmt":"2025-08-30T04:16:37","slug":"readability-variables-when-to-make-them","status":"publish","type":"page","link":"http:\/\/katzcs.com\/?page_id=94","title":{"rendered":"Readability &#8211; Variables &#8211; When to make them"},"content":{"rendered":"\n<div style=\"height:13px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h1 class=\"wp-block-heading has-text-align-center\">Readability Expectations<\/h1>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-center\">Variables &amp;&amp; When to Create Them<\/h2>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group has-tertiary-background-color has-background has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:0;margin-bottom:0;padding-top:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--50)\">\n<div class=\"wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-1a9727ad wp-block-columns-is-layout-flex\" style=\"margin-top:0;margin-bottom:0\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p>A general rule: If you are going to use a value more than once in a certain scope of code its reasonable to create a reference variable.<\/p>\n\n\n\n<p>Rationale:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It makes your code easier to maintain, understand and update if you use a variable instead of a random value\n<ul class=\"wp-block-list\">\n<li>Having the literal 12 all over your code is not good practice. Its better to make a variable eggPackageSize=12. Then its clear where the 12 is coming from. Its also much easier to update your code if eggPackageSize changes from 12 to 18, you only have to change the variable.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Even if you are using a value retrieved by a &#8216;getter&#8217; method&#8230; if you know the value will be the same, it is more efficient to create a variable than to have your program create multiple function calls.\n<ul class=\"wp-block-list\">\n<li>(Note that accessing a field or attribute of an object does not require a function call so you gain no speed\/processing by creating a variable. )<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p style=\"line-height:1.6\">Note: If you see code from my introductory courses you may see extra (unnecessary) variables! These are added deliberately to help new programmers to understand what is happening.<\/p>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-702decc0 wp-block-columns-is-layout-flex\" style=\"margin-top:0;margin-bottom:0;padding-top:var(--wp--preset--spacing--50)\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<h2 class=\"wp-block-heading\" style=\"font-size:clamp(25.014px, 1.563rem + ((1vw - 3.2px) * 1.733), 42px);font-style:normal;font-weight:700;line-height:1.2\">Style preference:<\/h2>\n\n\n\n<p>If you have a complex line\/section of code, for example a long string of method calls, it may increase readability to break this up and use an &#8216;extra&#8217; variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/Java example <br> \n\/\/ Before<br>String[] test = input.nextLine().toLowercase().split(\"\\t\");<br><br>\/\/ After<br>String line = input.nextLine().toLowercase();<br>String[] test = line.split(\"\\t\");<\/pre>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<p>Be aware: These are my opinions. Other instructors will have their own policies! <\/p>\n\n\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-container-core-group-is-layout-b2082ebc wp-block-group-is-layout-constrained\" style=\"margin-top:0;margin-bottom:0;padding-top:100px;padding-right:20px;padding-bottom:100px;padding-left:20px\">\n<div class=\"wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-9cad5244 wp-block-columns-is-layout-flex\" style=\"margin-top:0;margin-bottom:0\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p>Check out more of my readability expectations by language:<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-content-justification-space-between is-layout-flex wp-container-core-buttons-is-layout-8f3c3dcc wp-block-buttons-is-layout-flex\" style=\"margin-top:40px\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-primary-background-color has-background wp-element-button\" style=\"border-radius:0px\">C++<\/a><\/div>\n\n\n\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-primary-background-color has-background wp-element-button\" style=\"border-radius:0px\">Java<\/a><\/div>\n\n\n\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-primary-background-color has-background wp-element-button\" style=\"border-radius:0px\">Python<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Readability Expectations Variables &amp;&amp; When to Create Them A general rule: If you are going to use a value more than once in a certain scope of code its reasonable to create a reference variable. Rationale: Note: If you see code from my introductory courses you may see extra (unnecessary) variables! These are added deliberately [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":82,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-94","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/katzcs.com\/index.php?rest_route=\/wp\/v2\/pages\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/katzcs.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/katzcs.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/katzcs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/katzcs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":3,"href":"http:\/\/katzcs.com\/index.php?rest_route=\/wp\/v2\/pages\/94\/revisions"}],"predecessor-version":[{"id":97,"href":"http:\/\/katzcs.com\/index.php?rest_route=\/wp\/v2\/pages\/94\/revisions\/97"}],"up":[{"embeddable":true,"href":"http:\/\/katzcs.com\/index.php?rest_route=\/wp\/v2\/pages\/82"}],"wp:attachment":[{"href":"http:\/\/katzcs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}