{"id":11675,"date":"2018-01-31T14:11:22","date_gmt":"2018-01-31T14:11:22","guid":{"rendered":"https:\/\/sep.live.e3cms.net\/blog\/stop-using-variables\/"},"modified":"2018-01-31T14:11:22","modified_gmt":"2018-01-31T14:11:22","slug":"stop-using-variables","status":"publish","type":"post","link":"https:\/\/hexboxdev.wpenginepowered.com\/blog\/stop-using-variables\/","title":{"rendered":"Stop Using Variables &#8211; Learning Through Constraints"},"content":{"rendered":"<p><!--kg-card-begin: markdown--><\/p>\n<p>My new year\u2019s resolution for 2017 was to stop using variables. My goal was to force some learning by applying a constraint to my work. Over the course of the year, I attempted this while writing various types of code:<\/p>\n<ul>\n<li>Restful api endpoints \u2013 good. It felt quite natural to write code without variables in this environment.<\/li>\n<li>Parsing \u2013 good. The end result is a series of small functions with well defined responsibilities. The code was much easier to understand when compared to other parsers I have written with variables. The main reason for this is when I am reading a line of code I don\u2019t have to wonder if the value of something is going to change.<\/li>\n<li>Stateful view-controllers \u2013 mixed. Obviously \u2018stateful\u2019 is the problem here. I ended up continuing to use member variables, but eliminated local variables. This did result in small functions with a clear purpose, but I can\u2019t really call this a complete success.<\/li>\n<li>Stateless services \u2013 good. Whenever state is not a requirement, eliminating variables typically results in cleaner code.<\/li>\n<li>Unit Tests \u2013 bad. The unit tests are easier to read, write, and understand with variables.<\/li>\n<\/ul>\n<p>The method I chose to use is to break functions into multiple functions that do more limited things. So basically what would have been variables become parameters. Also I almost exclusively used collections as data streams when available. This style led most of my functions to begin with the return keyword. Other effects were an increase my use of the ternary operator and reduced my usage of brackets when making if-else blocks. The brackets seem unnecessary when I am just executing one line of code or calling a function. The only place where this felt awkward is when creating an instance of an object, doing something with it, and returning it.<\/p>\n<p>With this method, there are definitely some holes that you can dig yourself into. Particularly if the language allows easy declarations of anonymous functions. It is easy to follow a chain of thought that would result in several nested ternaries branching into anonymous functions that may or may not also contain ternaries. The solution is to refactor to reduce <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cyclomatic_complexity\">cyclomatic complexity<\/a> \u2013 which should be done regardless of programming style. However, I find that un-refactored procedural programming is easier to follow than un-refactored functional programming.<\/p>\n<p>Overall I believe this experiment to have been a success. I feel like my programming has improved by operating under this constraint. Going forward I\u2019ll be mixing variables back into my code, but I will be using them sparingly.<\/p>\n<p>Below is a basic example of the two programming styles.<\/p>\n<h2 id=\"codeexample\">Code Example<\/h2>\n<p><!--kg-card-end: markdown--><\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/hexboxdev.wpenginepowered.com\/wp-content\/uploads\/2020\/12\/2018-01-variables-vs-no-variables.png\" class=\"kg-image\" alt=\"Example code with and without variables\"><figcaption>With and Without Variables<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>My new year\u2019s resolution for 2017 was to stop using variables. My goal was to force some learning by applying a constraint to my work. Over the course of the year, I attempted this while writing various types of code: Restful api endpoints \u2013 good. It felt quite natural to write code without variables in [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","associated_team_member":0,"associated_user_id":0,"footnotes":""},"categories":[268],"tags":[394,382],"service":[],"class_list":["post-11675","post","type-post","status-publish","format-standard","hentry","category-programming","tag-functional-programming","tag-programming"],"_links":{"self":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/posts\/11675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/comments?post=11675"}],"version-history":[{"count":0,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/posts\/11675\/revisions"}],"wp:attachment":[{"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/media?parent=11675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/categories?post=11675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/tags?post=11675"},{"taxonomy":"service","embeddable":true,"href":"https:\/\/hexboxdev.wpenginepowered.com\/wp-json\/wp\/v2\/service?post=11675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}