{"id":48,"date":"2018-01-10T05:36:30","date_gmt":"2018-01-10T12:36:30","guid":{"rendered":"http:\/\/www.wholebeansoftware.com\/?p=48"},"modified":"2018-01-19T16:04:14","modified_gmt":"2018-01-19T23:04:14","slug":"programming-guideline-code-should-be-run-from-top-of-the-function-to-the-bottom","status":"publish","type":"post","link":"https:\/\/www.wholebeansoftware.com\/index.php\/2018\/01\/10\/programming-guideline-code-should-be-run-from-top-of-the-function-to-the-bottom\/","title":{"rendered":"Programming Guideline:  Code should be run from top of the function to the bottom."},"content":{"rendered":"<p>This seems so simple.\u00a0\u00a0 It just states that line 1 should execute before line 2.\u00a0 But many programs and conventions do not follow this simple guideline.\u00a0 <!--more--><\/p>\n<pre class=\"line-numbers\" data-start=\"0\" data-line-offset=\"-1\" data-line=\"1-2\"><code class=\"language-java\" data-line=\"\">public void myMethod() {\n  System.out.println(&quot;Line 1&quot;);\n  System.out.println(&quot;Line 2&quot;);\n}<\/code><\/pre>\n<p>Should output:<\/p>\n<pre class=\"line-numbers\" data-start=\"1\"><code class=\"\" data-line=\"\">&quot;Line 1&quot;\n&quot;Line 2&quot;<\/code><\/pre>\n<h3>Autoincrement operator<\/h3>\n<p>Following this guideline allows the reader to assume the sequence of execution. In assembler, most code is forced to execute in sequence. Once we &#8216;move up&#8217; to C language and all its descendants (C++, C#, Java, &#8230;) this guideline is ignored and forgotten.<\/p>\n<p>For example, take the code:<\/p>\n<pre class=\"line-numbers\" data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java\" data-line=\"\">int a = 5;\nint b = 2; \nint j = a++ * b;\n<\/code><\/pre>\n<p>What is the value of &#8216;j&#8217;?\u00a0 Most coders *know* the convention that the auto-increment operator (&#8216;++&#8217;) will execute last. So the answer is &#8216;j&#8217; equals 10;\u00a0 The same code can be written as:<\/p>\n<pre class=\"line-numbers\" data-start=\"1\"><code class=\"language-java\" data-line=\"\">int j = a * b;\na = a + 1;\n<\/code><\/pre>\n<p>This looks a bit funny to experienced programmers, but any first-year programmer and most managers will understand it.\u00a0\u00a0 I will grant you this is a\u00a0 near trivial example,\u00a0 and there are cases where using auto-increment makes the code easier to read,\u00a0 but it demonstrates that programmers get very accustomed to such mental gymnastics.\u00a0 Bottom line:\u00a0 cramming two lines of code into one line jumbles the top to bottom readability and increases the complexity of the code.<\/p>\n<h3>Anonymous Inner Classes<\/h3>\n<p>The example code in many tutorials for Java Swing button handlers uses an anonymous inner class (lines 4-7) and is some variation of:<\/p>\n<pre class=\"line-numbers\" data-start=\"1\" data-line=\"6\"><code class=\"language-java\" data-line=\"\">JButton button = new JButton(&quot;Do Something&quot;);\n\/\/Add action listener to button\nbutton.addActionListener(\n  new ActionListener() {\n    public void actionPerformed(ActionEvent e) {\n      System.out.println(&quot;You clicked the button&quot;);\n    }\n});<\/code><\/pre>\n<p>When is line 6 executed? The answer is some time after line 4 and not before line 9. The answer is line 6 is executed some time distant, possibly minutes or hours later when the user clicks the button.\u00a0 Line 5 will never be executed if the user does not press the button. Thus the order of execution is confusing and not apparent.<\/p>\n<p>Also, The syntax is just ugly, especially line 8. Some book writers say you get used to it, but I haven&#8217;t yet.<\/p>\n<p>A first step to make the code simpler and easier to read:<\/p>\n<pre class=\"line-numbers\" data-start=\"1\"><code class=\"language-java\" data-line=\"\">JButton button = new JButton(&quot;Do Something&quot;);\n\/\/Add action listener to button\nbutton.addActionListener( new ButtonPressedListener() );\n\n\/\/ Executed when the user presses the &quot;Do Something&quot; button\nclass ButtonPressedListener implements ActionListener {\n  public void actionPerformed(ActionEvent e) {\n    System.out.println(&quot;You clicked the button&quot;);\n  }\n}<\/code><\/pre>\n<p>This code is somewhat better at least the code is executed top to bottom. The inner class is a bit unsettling, but you could refactor it to its own file, which can be useful if the class is used in multiple files.\u00a0 Moving away from anonymous inner classes also makes the class ButtonPressedListener a &#8216;first class&#8217; class, now freed from the constraints of being crammed into the button constructor, the path to use inheritance, state variables, constructors, and all the goodness of classes is now cleared and unfettered.<\/p>\n<p>The new trend in tutorials is to not use anonymous inner classes, the Oracle web site, and other more recent sites, do not use them.\u00a0 This is good news.<\/p>\n<h3>Take away<\/h3>\n<p>The main point to remember is that when you are writing code try to write it so the lines are executed from top to bottom,\u00a0 this will make the code easier and less complex to read.\u00a0 Don&#8217;t write confusing code where the order of execution is not obvious.<\/p>\n<p>When you are reading code mentally recognize when the code is not executed from top to bottom.\u00a0\u00a0 In these cases note if this has made you confused and if so take steps to refactor the code to be more clear.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This seems so simple.\u00a0\u00a0 It just states that line 1 should execute before line 2.\u00a0 But many programs and conventions do not follow this simple guideline.\u00a0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-journal"],"_links":{"self":[{"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":10,"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wholebeansoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}