<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="/assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Field Notes (Posts about deprecated)</title><link>https://janetriley.net/</link><description></description><atom:link href="https://janetriley.net/categories/cat_deprecated.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 &lt;a href="mailto:hello@janetriley.net"&gt;Janet Riley&lt;/a&gt; </copyright><lastBuildDate>Tue, 21 May 2024 18:15:28 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Begin Refactoring with Naming the Unknown</title><link>https://janetriley.net/blog/name-the-unknown.html</link><dc:creator>Janet Riley</dc:creator><description>&lt;p&gt;I'm in the midst of adding features to existing code where the original developer is not available.
  &lt;!-- TEASER-END --&gt;
  This is a double
  challenge. Not only must I figure out how to add new logic, I'm still deciphering what the code does.&lt;/p&gt;
&lt;p&gt;My first step has been to add names. In stories, knowing a creature's name gives power over it. Good names add
  context. They indicate the significance of something so I don't have to deduce it. Names communicate that
  understanding to the next maintainer.&lt;/p&gt;
&lt;p&gt;Reading over the code, I noticed several magic numbers, like '3'. 3 what? Sides on a triangle? After some sleuthing I
  replaced it with the constant NUM_BEARS. I'm in the midst of adding features to existing code where the original
  developer is not available. This is a double challenge. Not only must I figure out how to add new logic, I'm still
  deciphering what the code does.&lt;/p&gt;
&lt;p&gt;My first step has been to add names. In stories, knowing a creature's name gives power over it. Good names add
  context. They indicate the significance of something so I don't have to deduce it. Names communicate that
  understanding to the next maintainer.&lt;/p&gt;
&lt;p&gt;Reading over the code, I noticed several magic numbers, like '3'. 3 what? Sides on a triangle? After some sleuthing I
  replaced it with the constant NUM_BEARS. There's a danger of mistaking one 3 for another and lying with code. 3s looks
  much alike. I discovered a few places where there was a different meaning, and confirmed each replacement manually to
  be certain.&lt;/p&gt;
&lt;p&gt;We can give variables more descriptive names. 'Input2' may be literally correct, in that it's the second input field
  on a form. 'CharacterName' tells us much more. IDEs like IntelliJ make it safe and easy to rename variables. Later I
  could refactor the HTML field names as well.&lt;/p&gt;
&lt;p&gt;Related constants can be ordered in a hash, especially where there's a sequence. It doesn't change the code, but it
  helps the humans. For example, in a Goldilocks game,&lt;/p&gt;
&lt;p&gt;&lt;code&gt;var PORRIDGE_LEVEL = { TOO_HOT: 'hot', TOO_COLD:'cold', JUST_RIGHT: 'ok'}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Arbitrary conditionals can be encapsulated in methods that describe what we're looking at. I can move the conditions
  in &lt;code&gt;if( bigBowl.empty &amp;amp;&amp;amp; middleBowl.empty &amp;amp;&amp;amp; littleBowl.empty)&lt;/code&gt; to a descriptive and
  testable method like &lt;code&gt;porridgeEaten()&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Speaking of testable, big blocks of code can be broken into functions. Consider a long narrative method that
  orchestrates what happens at each step of the game. It checks where Goldilocks is in the story with a bunch of if
  statements, and describes what should happen at each step. I've extracted the story chapters into their own shorter
  methods, like &lt;code&gt;kitchenScene()&lt;/code&gt; and &lt;code&gt;bearsEatGoldilocks()&lt;/code&gt;. This breaks several hundred lines up
  into shorter methods. We understand how each method participates in the story, and we can add tests that don't require
  replaying the entire game.&lt;/p&gt;
&lt;p&gt;Once we name what we see, we can improve it.&lt;/p&gt;
&lt;p&gt;Recommended:&lt;br&gt;
  &lt;a href="https://twitter.com/mfeathers"&gt;Michael Feathers'&lt;/a&gt; &lt;a href="http://www.powells.com/biblio/62-9780131177055-1"&gt;Working Effectively with Legacy Code&lt;/a&gt; is invaluable and
  theraputic. Chapter 22, "I Need to Change a Monster Method and I Can't Write Tests For It", has been particularly
  helpful this week.&lt;/p&gt;</description><category>coding style</category><guid>https://janetriley.net/blog/name-the-unknown.html</guid><pubDate>Tue, 11 Nov 2014 20:00:16 GMT</pubDate></item></channel></rss>