{"id":5471,"date":"2010-03-09T06:12:15","date_gmt":"2010-03-09T06:12:15","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/bharry\/2010\/03\/09\/programming-practices-part-1-watching-from-a-distance\/"},"modified":"2018-08-14T00:20:32","modified_gmt":"2018-08-14T00:20:32","slug":"programming-practices-part-1-watching-from-a-distance","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/bharry\/programming-practices-part-1-watching-from-a-distance\/","title":{"rendered":"Programming Practices: Part 1 &#8211; Watching from a distance"},"content":{"rendered":"<p>I figured I&rsquo;d start the series with a more abstract post about what watching me code looks like.&nbsp; It was interesting to observe myself doing it because I didn&rsquo;t really realize the degree to which I do some things.&nbsp; If I were to summarize my overall approach to coding with a simple description, I&rsquo;d call it &ldquo;Annealing&rdquo; &ndash; from the Encarta dictionary: &ldquo;metallurgy craft transitive and intransitive verb to subject an alloy, metal, or glass to a process of heating and slow cooling to make it tougher and less brittle.&rdquo;&nbsp; Or in more of a software sense, &ldquo;Simulated Annealing&rdquo; &ndash; from Wikipedia: &ldquo;a technique for searching for a solution in a space otherwise too large for &#8220;ordinary&#8221; search methods to yield results&rdquo;.\nWhat does that mean?\nLet me try talking about it as a set of principles and techniques:\n1) <strong>Working code every few hours<\/strong> &ndash; I find I can&rsquo;t stand to have code that is in pieces all over the floor.&nbsp; I don&rsquo;t ever like to be far from code that works.&nbsp; My coding is defined by a sequence of &ldquo;stable&rdquo; points that are no more than a few hours apart.&nbsp; A &ldquo;stable point&rdquo; is a state where all of the code compiles, it runs and it does something useful.&nbsp; It may not be anywhere near doing what the ultimate application is intended to do but it does some fraction of it.&nbsp; A quantum of work transitions from one stable point to the next.\nI find that as I approach a problem, I think about how I am going to build the code in bite sized chunks.&nbsp; It&rsquo;s kind of funny to watch but if I get a few hours into something and I&rsquo;m not very close to a &ldquo;stable point&rdquo;, I actually get anxious.&nbsp; I start getting nervous and looking for ways to stub out or cauterize parts of the application so I can get back to something I can build and hit F5 on.&nbsp; I think part of this is related to why I got into programming in the first place &ndash; I&rsquo;m a huge fan of the &ldquo;instant gratification&rdquo; that you get programming.&nbsp; I can think of no other profession where you can start with nothing and in a few short hours have built something really useful.&nbsp; But more practically, it&rsquo;s an important way to make sure you don&rsquo;t get lost in the weeds of a solution.\n2) <strong>Prove the concept<\/strong> &ndash; When I&rsquo;m getting ready to leave a stable point, I find the first thing I want to do is map my path to the next stable point.&nbsp; I choose some specific, containable goal &ndash; add a dialog, a menu item, move an operation to a background thread, add a control to a form, something.&nbsp; Before I enter the chaos between stable points I want to prove to myself that I know how to get to the next one.&nbsp; I often start, particularly if I&rsquo;m working with API surface I&rsquo;m unfamiliar with, by doing a rapid prototype of the core concepts that I need.&nbsp; I don&rsquo;t care what the code looks like. I don&rsquo;t modularize anything.&nbsp; I don&rsquo;t worry about comments, variable names, code organization, error handling, performance, anything.&nbsp; I just want to touch each of the core APIs that I need to use and prove that I can get access to the information that I need and understand roughly what I need to do with it.\nI usually get it all the way to running code and step through it in the debugger so that I can see all of the information flow and ensure that I understand all of the side effects, etc.&nbsp; To a first approximation, I then throw it all away.&nbsp; Sometimes, I&rsquo;ll just comment out the routine(s) that I wrote and keep them around a while for reference and a source of code snippets.&nbsp; I step back and conceptualize an organization of the code that feels clean and I then start writing the code &ldquo;for real&rdquo;.\nAt some level this might feel like a waste of time but I believe it is a huge time saver for me.&nbsp; It usually doesn&rsquo;t take me more than 15-20 minutes or so to explore the key concepts of the few hour leap from one stable point to the next and I often learn a ton in the process and make significant shifts in my approach as a result.&nbsp; It saves me from getting an hour or two into something and realizing that some assumption I made at the beginning was invalid and I need to redo everything I just did.&nbsp; I think it also helps ensure that I end up with better organized code when I&rsquo;m done.\n3) <strong>Refactor, Refactor, Refactor<\/strong> &ndash; I never really realized how much I just naturally do this.&nbsp; I&rsquo;ll bet the net effect is that I write any given stable point to stable point quantum 2 or 3 times in the few hours I work on it.&nbsp; I start with a conceptual picture of the algorithm but I don&rsquo;t try to plan out everything.&nbsp; I start writing the code organized in a way that feels the &ldquo;cleanest&rdquo; but as soon as I realize something&rsquo;s not right, I refactor.&nbsp; I&rsquo;m constantly splitting methods, reordering code, reorganizing data structures, building abstractions, etc.&nbsp; This is the main reason I started by talking about annealing &ndash; because that&rsquo;s what the process feels like to me.&nbsp; I keep jiggling the code, gradually reducing the temperature until all of the crystals line up just right and I have a very clean, maintainable, reusable, fast, robust, &hellip; implementation.\nI now realize that I used to do this even before &ldquo;refactoring tools&rdquo; were invented but I am finding some of the refactoring tools to be very useful in saving me some typing &ndash; though they have a long way to go to make me really happy.&nbsp; The VS ones, at least, don&rsquo;t automatically do everything I want and don&rsquo;t always have the flexibility for me to control it.&nbsp; Examples &ndash; Extract method often leaves me with a lot of clean up to do.<\/p>\n<ul>\n<li>It doesn&rsquo;t give me the ability to control parameter ordering (and as you&rsquo;ll see from this series, I&rsquo;m anal about everything).&nbsp; I am very particular about the ordering of parameters.<\/li>\n<li>It doesn&rsquo;t allow me to control the types of parameters to the method.&nbsp; Every method should have an abstraction or contract of what it operates on and what it does.&nbsp; I might want to type a parameter as a base class of the passed object.&nbsp; I might want to pass a one or more members of an object that is used rather than the whole object (because the type of the object is not part of the method&rsquo;s contract).<\/li>\n<li>It doesn&rsquo;t allow me to extract to a different class.<\/li>\n<li>It rarely puts the method where I want it in the class (more on this in future posts).<\/li>\n<li>Lastly, I find that before I extract the method, I actually want to type the call to the extracted function.&nbsp; It&rsquo;s part of making sure I understand the factoring of the code and what the contract is.&nbsp; I wish I could type the call site, then select the code I want to extract and say &ndash; extract it such that the call matches what I just typed.&nbsp; But instead, of course, it extracts it, adds another call site, which I immediately delete and then go fix up the parameter types and order, etc.<\/li>\n<\/ul>\n<p>As I write the code, I comment lightly.&nbsp; Because I refactor so much, I don&rsquo;t want to waste time writing and rewriting comments, method contract descriptions, etc.&nbsp; But once I&rsquo;ve gotten the code just right, I polish it.&nbsp; I go back over all of it making sure I like the abstractions and the flow, I comment everything and I move all of the methods that I&rsquo;ve written to their permanent resting place in the class &ndash; I&rsquo;m very particular about the order of methods in a class (did I mention I&rsquo;m anal about everything? :)).\n4) <strong>Step through everything in the debugger<\/strong> &ndash; Before I start polishing a quantum of work, I walk through everything in the debugger.&nbsp; I conceive and write or manually execute test cases for every interesting scenario and I step through all of them in the debugger.&nbsp; It&rsquo;s not sufficient that the code works.&nbsp; I&rsquo;m a firm believer that you don&rsquo;t actually understand your code if you don&rsquo;t understand exactly how and why it works and the only way to do that is to step through it in the debugger and see what it is actually doing.\nYou&rsquo;d be stunned to realize how often the results of code match what you expect but the execution path is nothing like what you expect.&nbsp; This is one of my greatest secrets to getting high quality, high performance code that I never have to come back and revisit again.&nbsp; When it&rsquo;s done, it&rsquo;s done done \ud83d\ude42\n5) <strong>Paper and pencil<\/strong> &ndash; The last thing that I&rsquo;ll mention that&rsquo;s perhaps a bit odd and I think you&rsquo;d notice if you sat and watched me code is that I always have a pencil and paper in front of me.&nbsp; As I&rsquo;m coding, I&rsquo;m constantly drawing pictures of data structures, writing out sample data, hand walking through the algorithm on paper to see how things work.&nbsp; I suspect it&rsquo;s just a quirk of mine but there&rsquo;s no better way for me to think through the algorithms as I go.&nbsp; When I&rsquo;m done with a quantum of work, I always have a sheet of paper full of scribbles.&nbsp; I generally extract some of the value and then just throw the paper away.&nbsp; Some of the scribbles become part of the documentation &ndash; pictures, algorithm descriptions, etc.&nbsp; Some become the source of test cases that I keep to serve as regression tests in the event that I ever need to come back to the code.\n<strong><u>Summary<\/u><\/strong>\nAt a very high level, that&rsquo;s what watching me code looks like.&nbsp; Some of what I&rsquo;ve written here, I think of as quirks of my personality &ndash; heavy use of pencil and paper, for example.&nbsp; But most of it I consider important practices to produce high quality code efficiently.&nbsp; I can imagine a lot of people looking at this and saying, &ldquo;wow, that sounds expensive&rdquo; and if you are banging out a one off project with a short lifetime, it&rsquo;s really not worth it.&nbsp; But if you are writing code that is part of a significant application, is going to live for years and potentially have other people needing to understand it, I believe these practices are invaluable.\nI&rsquo;m not sure I&rsquo;ve captured everything.&nbsp; Perhaps in the discussions that follow and subsequent posts I&rsquo;ll think of a few more &ldquo;high level&rdquo; things.&nbsp; We&rsquo;ll see.<\/p>\n<p>Brian<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I figured I&rsquo;d start the series with a more abstract post about what watching me code looks like.&nbsp; It was interesting to observe myself doing it because I didn&rsquo;t really realize the degree to which I do some things.&nbsp; If I were to summarize my overall approach to coding with a simple description, I&rsquo;d call [&hellip;]<\/p>\n","protected":false},"author":244,"featured_media":14617,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-5471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>I figured I&rsquo;d start the series with a more abstract post about what watching me code looks like.&nbsp; It was interesting to observe myself doing it because I didn&rsquo;t really realize the degree to which I do some things.&nbsp; If I were to summarize my overall approach to coding with a simple description, I&rsquo;d call [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts\/5471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/users\/244"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/comments?post=5471"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts\/5471\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/media\/14617"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/media?parent=5471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/categories?post=5471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/tags?post=5471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}