{"id":1023,"date":"2021-04-06T14:48:46","date_gmt":"2021-04-06T14:48:46","guid":{"rendered":"http:\/\/www.wellformedness.com\/blog\/?p=1023"},"modified":"2021-04-06T20:08:33","modified_gmt":"2021-04-06T20:08:33","slug":"should-you-assign-it-to-a-variable","status":"publish","type":"post","link":"https:\/\/www.wellformedness.com\/blog\/should-you-assign-it-to-a-variable\/","title":{"rendered":"Should you assign it to a variable?"},"content":{"rendered":"<p>Let us suppose that you&#8217;re going to compute some value, and then send it to another function. Which snippet is better (using <code>lhs<\/code> as shorthand for a variable identifier and <code>rhs()<\/code> as shorthand for the right-hand side expression)?<\/p>\n<pre>lhs = rhs()\r\ndo_work(lhs)<\/pre>\n<pre>do_work(rhs())<\/pre>\n<p>My short answer is\u00a0<em>it depends<\/em>. Here is my informal set of heuristics:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Is a type-cast involved (e.g., in a statically typed language like C)? If so, assign it to a variable.<\/li>\n<li>Would the variable name be a meaningful one that would provide non-obvious information about the nature of the computation? If so, assign it to a variable. For instance, if a long right-hand side expression computes perplexity, <code>ppl = ...<\/code> or <code>perplex = ...<\/code> is about as useful as an inline comment.<\/li>\n<li>Is the computation used again in the same scope? If so, assign it to a variable.<\/li>\n<li>Is the right-hand side just a very complicated expression? If so, consider assigning it to a variable, and try to give it an informative name.<\/li>\n<li>Otherwise, do not assign it to a variable.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let us suppose that you&#8217;re going to compute some value, and then send it to another function. Which snippet is better (using lhs as shorthand for a variable identifier and rhs() as shorthand for the right-hand side expression)? lhs = rhs() do_work(lhs) do_work(rhs()) My short answer is\u00a0it depends. Here is my informal set of heuristics: &hellip; <a href=\"https:\/\/www.wellformedness.com\/blog\/should-you-assign-it-to-a-variable\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Should you assign it to a variable?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-1023","post","type-post","status-publish","format-standard","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/comments?post=1023"}],"version-history":[{"count":6,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1023\/revisions"}],"predecessor-version":[{"id":1029,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1023\/revisions\/1029"}],"wp:attachment":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/media?parent=1023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/categories?post=1023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/tags?post=1023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}