<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mark Rubin</title>
    <description>The latest articles on DEV Community by Mark Rubin (@mbrubin56).</description>
    <link>https://dev.to/mbrubin56</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F917483%2F5943b972-5955-4185-a2f3-ae3c09b9f4d2.JPG</url>
      <title>DEV Community: Mark Rubin</title>
      <link>https://dev.to/mbrubin56</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mbrubin56"/>
    <language>en</language>
    <item>
      <title>Where should main live</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Thu, 01 Sep 2022 04:01:46 +0000</pubDate>
      <link>https://dev.to/mbrubin56/where-should-main-live-244h</link>
      <guid>https://dev.to/mbrubin56/where-should-main-live-244h</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A Java program needs to have a method with the signature&lt;br&gt;
&lt;code&gt;public static void main(String args[])&lt;/code&gt; in some class. It serves as the entry point for your program. When you run your program, the Java runtime finds this magic entry point, calls it, and you're off to the races. You'll have to house this method in a class, because that's just how it works. The question entertained by this post is in &lt;em&gt;which&lt;/em&gt; class should the &lt;code&gt;main&lt;/code&gt; method go.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/mbrubin56/how-to-get-out-of-main-and-reduce-static-1ddp"&gt;How To Get Out Of Main And Reduce Static&lt;/a&gt;, we moved most of our logic out of our &lt;code&gt;public void static main&lt;/code&gt; method into our &lt;code&gt;Square&lt;/code&gt; class and ended up with this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.2f&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The perimiter of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a good idea to review that post. In it, I noted that we should think of &lt;code&gt;main&lt;/code&gt; as just a contractual rendezvous point with the Java runtime. It's how the Java runtime finds a place to start your program, and that is its primary role. Given that, does &lt;code&gt;main&lt;/code&gt; belong in &lt;code&gt;Square&lt;/code&gt;? It really doesn't have anything to do with a square, the shape. If I ask you to tell me the properties of a square that you should model with a &lt;code&gt;Square&lt;/code&gt; class, you'll tell me about side length and the formulas for the area and perimeter of a square and the like (maybe what the angle measurements are for a square, etc.), but you wouldn't say that they have a contractual entry point through which a Java runtime can start a Java program.&lt;/p&gt;

&lt;p&gt;So with that in mind, it should seem off that &lt;code&gt;main&lt;/code&gt; is a method of &lt;code&gt;Square&lt;/code&gt;. It can be &lt;em&gt;convenient&lt;/em&gt; to put it there: in this, case it keeps your program all in one file. But &lt;em&gt;conceptually&lt;/em&gt;, it's odd.&lt;/p&gt;

&lt;p&gt;I'll pile on here. What if you enhance your program to hardcode values for other shapes (e.g. circle, triangle) and output their areas and perimeters? Would you insist on keeping your &lt;code&gt;main&lt;/code&gt; in &lt;code&gt;Square&lt;/code&gt;? Why &lt;code&gt;Square&lt;/code&gt; and not &lt;code&gt;Circle&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Here's a similar point. Let's say you were a reader of the enhanced program and not its author: would you know which file/class had the magic &lt;code&gt;main&lt;/code&gt; method in it from class names such as &lt;code&gt;Square&lt;/code&gt;, &lt;code&gt;Circle&lt;/code&gt;, &lt;code&gt;Triangle&lt;/code&gt;? Would you say to yourself "Oh, it has to be in &lt;code&gt;Square&lt;/code&gt;. Why would it be anywhere else?". Once programs start to get a bit large -- even just a few files -- I find myself having to hunt for students' &lt;code&gt;main&lt;/code&gt; implementations.&lt;/p&gt;

&lt;p&gt;So I recommend having a &lt;code&gt;Main&lt;/code&gt; class (or some class with "Main" in the name) whose sole purpose is to house the &lt;code&gt;public static main&lt;/code&gt; method. Make a &lt;code&gt;Main&lt;/code&gt; class and put your &lt;code&gt;public static void main&lt;/code&gt; in there. Now it'll be obvious to you and to everyone where your &lt;code&gt;main&lt;/code&gt; method lives. And however your program grows over time, you won't have to move it from one class to another to accommodate the growth. I need to note that this is not universal convention, although I bet not many would dislike it much: it can never really hurt, and it can help.&lt;/p&gt;

&lt;p&gt;That also aligns nicely with the idea that &lt;code&gt;main&lt;/code&gt; is just a contractual entry point. Classes should model something, and if you follow this advice, your &lt;code&gt;Main&lt;/code&gt; class is then modeling that it's the entry point for your program. It advertises no more than that, but it does advertise that its purpose is to house &lt;code&gt;main&lt;/code&gt; (unlike &lt;code&gt;Square&lt;/code&gt;'s name). So in our example, we'd have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In Main.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.2f&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The perimiter of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In Square.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Does main still contain too much?
&lt;/h2&gt;

&lt;p&gt;I'm going to get more controversial here. I think even this version has &lt;code&gt;main&lt;/code&gt; doing too much. I'd prefer that &lt;code&gt;Main&lt;/code&gt; call a method on &lt;em&gt;another&lt;/em&gt; class that's well named for doing the work here for creating our &lt;code&gt;Square&lt;/code&gt;. I'd like &lt;code&gt;main&lt;/code&gt; to have as little logic as possible: it's there to fulfill our contract with Java and nothing else. Anything else that we do that's interesting should be done in a class whose &lt;em&gt;purpose&lt;/em&gt; is to do that thing. I'm advocating keeping this contract with Java separate from everything else. I'm pretty extreme here. I'd rather see something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In Main.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Driver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startTheProgram&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In Driver.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Driver&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;startTheProgram&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.2f&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The perimiter of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In Square.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;Main&lt;/code&gt; does nothing than fulfill our contract with the Java runtime, &lt;code&gt;Driver&lt;/code&gt; is in charge of our logic for how to drive our program, and &lt;code&gt;Square&lt;/code&gt; simply models a &lt;code&gt;Square&lt;/code&gt;. If we want our program to do more, we put more in &lt;code&gt;Driver&lt;/code&gt;. If we change our mind about what to do first, we either change &lt;code&gt;Driver&lt;/code&gt;'s &lt;code&gt;startTheProgram&lt;/code&gt; implementation or we create another class that we call from &lt;code&gt;main&lt;/code&gt; to do something first, and have it call &lt;code&gt;Driver&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;I have to admit this: you'll find lots of folks using &lt;code&gt;Main&lt;/code&gt; classes or appreciating my point about having a separate &lt;code&gt;Main&lt;/code&gt; class; you'll probably find fewer excited about my creating yet another class, such as &lt;code&gt;Driver&lt;/code&gt; that &lt;code&gt;main&lt;/code&gt; calls, especially for small programs.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Naming Conventions</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:45 +0000</pubDate>
      <link>https://dev.to/mbrubin56/naming-conventions-55ni</link>
      <guid>https://dev.to/mbrubin56/naming-conventions-55ni</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Java allows you a lot of flexibility in how you name classes, variables, methods, etc. But there are conventions that Java programmers follow. Obeying these conventions is important: it makes it much easier for all of us to comprehend each others' programs quickly and easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Class and Interface names begin with an &lt;em&gt;uppercase&lt;/em&gt; letter
&lt;/h2&gt;

&lt;p&gt;Your class and interface names should always begin with an uppercase letter. Java does not &lt;em&gt;require&lt;/em&gt; this -- you won't get a syntax or compiler error if you start your class name with a lowercase letter. But it's just how things are done. Not following this convention will ultimately lead to confusion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variable, method, parameter names should always begin with a &lt;em&gt;lowercase&lt;/em&gt; letter
&lt;/h2&gt;

&lt;p&gt;Again, this is not required by Java, and your Java programs will compile and run if you break this convention. Nonetheless, convention has value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use &lt;a href="https://en.wikipedia.org/wiki/Camel_case"&gt;CamelCase&lt;/a&gt; for class, variable, parameter, and method names
&lt;/h2&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;code&gt;double sellerCost;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;double buyerPrice;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;double getProfit(double sellerCost, double buyerPrice)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;void displayDataAsTable()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;class ProfitCalculator&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/mbrubin56/meaningful-names-bh1"&gt;Meaningful names&lt;/a&gt; for your classes, variables, methods, and parameters are generally multiple words stuck together in sequence to make a small phrase that describes what they do or what they're for. You should be able to get the idea from the examples above.&lt;/p&gt;

&lt;p&gt;If it's a class or interface name, you start with an uppercase letter; if it's a variable, method, or parameter name you start with a lowercase letter. Then you lowercase the rest of the letters in the first of the jammed together words, and then capitalize the first letter of the next word, lowercase its remaining letters, capitalize the first letter of the next word, and so on. The examples are easier to understand than the explanation.&lt;/p&gt;
&lt;h2&gt;
  
  
  There are other rules
&lt;/h2&gt;

&lt;p&gt;These cover most everything you'll use early on. &lt;/p&gt;

&lt;p&gt;There are rules for so-called constants in Java, which use a capitalized &lt;a href="https://en.wikipedia.org/wiki/Snake_case"&gt;SNAKE_CASE&lt;/a&gt;: e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="no"&gt;MAX_PRICE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;100.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and more constructs. I only mention this for completeness. Don't sweat them. Focus on the rules for class names, variables, methods, and parameters, and you'll be in great shape.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Writing Good Comments</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:42 +0000</pubDate>
      <link>https://dev.to/mbrubin56/writing-good-comments-1j3e</link>
      <guid>https://dev.to/mbrubin56/writing-good-comments-1j3e</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't narrate your code
&lt;/h2&gt;

&lt;p&gt;Many beginning Java programmers have been taught to make sure to include comments in their programs. And because they are good, dutiful students, they do. A great deal of this time, those comments are simply narrations of their code, and so are unnecessary. Here are some examples in an abbreviated class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** Calculates profit. */&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProfitCalculator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;buyerPrice&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the buyer's price&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;sellerCost&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the seller's cost&lt;/span&gt;

    &lt;span class="c1"&gt;// default constructor&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ProfitCalculator&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// assign buyerPrice&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;buyerPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// assign sellerCost&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sellerCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ProfitCalculator&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;buyerPrice&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;sellerCost&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Save the buyer price in a member field.&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;buyerPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buyerPrice&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Save the seller cost in a member field.&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sellerCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sellerCost&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Sets the buyer price.&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setBuyerPrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;buyerPrice&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;buyerPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buyerPrice&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// More code would follow.&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;None of these comments add anything useful. &lt;/p&gt;

&lt;p&gt;Because the author has used &lt;a href="https://dev.to/mbrubin56/meaningful-names-bh1"&gt;meaningful names&lt;/a&gt; for the variables and methods, the comment here&lt;br&gt;
&lt;code&gt;private double buyerPrice; // the buyer's price&lt;/code&gt;&lt;br&gt;
doesn't add anything and isn't needed.&lt;/p&gt;

&lt;p&gt;A method with the signature&lt;br&gt;
&lt;code&gt;public void setBuyerPrice(double buyerPrice)&lt;/code&gt;&lt;br&gt;
had better set the buyer price! So a comment telling us that isn't needed, or else the author ought to rename their method and their parameter!&lt;/p&gt;

&lt;p&gt;Let's look at this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// Default constructor&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ProfitCalculator&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is interesting because texts that are teaching students what a default constructor is will often have a comment just like that on their examples of default constructors. That's helpful to you as a student. And so it's totally natural that you would mimic the example from your texts. But the programs you're mimicking are intended to teach you what a default constructor is; the programs you're writing are not meant to be examples to teach &lt;em&gt;others&lt;/em&gt; how to code, and so you shouldn't include comments as if they were. Which means not mimicking your examples in this way.&lt;/p&gt;

&lt;p&gt;This leads to a bit of a tricky situation for students. Maybe you think it's still useful to add to your default constructor &lt;code&gt;// default constructor&lt;/code&gt; because it's helpful to &lt;em&gt;you&lt;/em&gt; as you're starting out. I'm torn here: I like that you're adding comments that you think are genuinely helpful and are genuinely helpful to you. But we can assume that the readers of our code know basic Java, and so don't need default constructors and other standard bits of Java highlighted for them unless for some reason, it would be tricky to see those things without your comment.&lt;/p&gt;

&lt;p&gt;Another example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// default constructor&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ProfitCalculator&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// assign buyerPrice&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;buyerPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// assign sellerCost&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sellerCost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anyone familiar with Java will know that those lines in the body of the constructor are assignment statements and what variables the assignments are being made to. So it's not helpful to add those comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what comments should I include?
&lt;/h2&gt;

&lt;p&gt;I'm going to be pretty heretical here. If you have used meaningful names for your classes, variables, methods, parameters, and so on, maybe very few! Ideally, the code is self-documenting because it's obvious from the names of things and the flow of your statements what the program is doing and why. Especially for the size of the programs you're writing.&lt;/p&gt;

&lt;p&gt;Less heretically, any time your program does anything important that a reader might not pick up on right away, that deserves a comment. What's wrong with the comments in the previous section is that they focus too much on &lt;em&gt;what&lt;/em&gt; the code does rather than on &lt;em&gt;why&lt;/em&gt; it does it. The &lt;em&gt;what&lt;/em&gt; is often not a problem for fluent Java readers. It's when they may not understand &lt;em&gt;why&lt;/em&gt; your code does what it does that they may need help. &lt;/p&gt;

&lt;p&gt;So if you, for example, skip a line of input from a file or an element of an array for some reason, you should comment that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="c1"&gt;// We need to skip printing the first price because&lt;/span&gt;
        &lt;span class="c1"&gt;// it has already been printed in &amp;lt;some other place&amp;gt;, &lt;/span&gt;
        &lt;span class="c1"&gt;// so we start iterating at 1.&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it's good advice to have a top-level comment on your program saying what the program does in general and any interesting facts about it. And it's generally good advice to think about putting a top-level comment on your classes, describing what the classes do and how they work, although the same principles apply here, too, about making sure you're adding something meaningful in your comment: if it's totally obvious what a &lt;code&gt;ProfitCalculator&lt;/code&gt; class does, perhaps a comment isn't necessary.&lt;/p&gt;

&lt;p&gt;So some fellow instructors may dislike this advice because instructors are generally pleading with their students to add comments. I also plead with you to add comments, but &lt;em&gt;when they are necessary and meaningful&lt;/em&gt;. I'm pleading with you not to add comments that add no real value. And I'll plead with you even harder to &lt;a href="https://dev.to/mbrubin56/meaningful-names-bh1"&gt;use meaningful names&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grammar
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Comments should be grammatical
&lt;/h3&gt;

&lt;p&gt;Comments are there to &lt;em&gt;help&lt;/em&gt; you and especially your readers. If they are ungrammatical, they add a mental tax to you and your readers; poor grammar makes it harder to figure out what was meant. Not everyone is good at grammar, and that's okay. Do your best, but do try.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comments should not contain abbreviations
&lt;/h3&gt;

&lt;p&gt;This is a bit of a hot take, but don't abbreviate where elaboration would be clearer, except when using very standardly abbreviated terms, such as &lt;code&gt;e.g.&lt;/code&gt; and &lt;code&gt;etc.&lt;/code&gt;. So rather than say&lt;br&gt;
&lt;code&gt;// Calc the profit.&lt;/code&gt;&lt;br&gt;
say&lt;br&gt;
&lt;code&gt;// Calculate the profit.&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Comments should be complete sentences that end with periods.
&lt;/h3&gt;

&lt;p&gt;This is absolutely a hot take, and definitely most developers don't do this. I learned to push for this from a peer, and I love it. Again, comments are meant to be helpful. Complete sentences are generally more helpful than not. And deciding on writing a complete sentence (and removing abbreviations) eliminates the question for you of how non-standard your English is allowed to be. Just write a complete, grammatical sentence with full words, and you and your code readers will be grateful when reading them, and you won't have to debate in your head if your sentence fragment with abbreviations is understandable. It makes programming easier &lt;em&gt;for you&lt;/em&gt; to write your comments well and consistently because now you have a simple rule.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>== true and == false are Redundant</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:38 +0000</pubDate>
      <link>https://dev.to/mbrubin56/-true-and-false-are-redundant-5aoc</link>
      <guid>https://dev.to/mbrubin56/-true-and-false-are-redundant-5aoc</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This won't do anything differently from this variant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;isNew&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid and new."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;won't do anything differently from&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;isNew&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid and new."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the versions that don't tack on the &lt;code&gt;== true&lt;/code&gt;. That's what Java programmers do, conventionally, and the shorter expressions are simpler to read. Well, they're simpler to read once you get the hang of things. Admittedly, you might find it easier to insert a secret "equals true" when reading these boolean expressions to yourself. I did when I was starting out. But, conventionally, we don't write our boolean expressions that way.&lt;/p&gt;

&lt;p&gt;Similarly, for &lt;code&gt;false&lt;/code&gt; checks, we don't, conventionally, write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;isNew&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is not valid and not new."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isNew&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is not valid and not new."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Out loud, professionally, we read this as "if not isValid and not isNew then..." But if you need to mentally adjust that to "if isValid is false and isNew is false then..." that's perfectly fine. Lots of folks do. But the Java is written using the not operator, &lt;code&gt;!&lt;/code&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Always Use Braces For if/else</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:36 +0000</pubDate>
      <link>https://dev.to/mbrubin56/always-use-braces-for-ifelse-2fkg</link>
      <guid>https://dev.to/mbrubin56/always-use-braces-for-ifelse-2fkg</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Yes, it's true, you don't &lt;em&gt;need&lt;/em&gt; curly braces to introduce a scope for a bit of Java such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or even for&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is not valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But it's dangerous and introduces style inconsistencies not to.&lt;/p&gt;

&lt;h2&gt;
  
  
  The danger
&lt;/h2&gt;

&lt;p&gt;The danger comes from the fact that it's far too easy to add a second thing to do when those &lt;code&gt;if&lt;/code&gt; or &lt;code&gt;else&lt;/code&gt; conditions are met and not realize you need to now add the curly braces to make sure both things are done when the condition is met. Consider&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It sure is."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will always execute, even if isValid is false.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oops. That second line will always execute, regardless of the whether &lt;code&gt;isValid&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. The author probably wanted to add curly braces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It sure is."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will only execute if isValid is true.&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly for&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is not valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It sure isn't."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will always execute, even if isValid is true. &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The author probably wanted to add curly braces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is not valid."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It sure isn't."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will only execute if isValid is false.&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Style consistency
&lt;/h2&gt;

&lt;p&gt;A lot of our ability to read programs easily comes from the authors' obeying conventions and their being consistent in their coding style. &lt;/p&gt;

&lt;p&gt;For example, if a file is formatted inconsistently with different indentation patterns everywhere, it's hard to take in. You wouldn't want to read this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Don't do this!&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is valid"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The input is not valid"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It sure isn't"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes adding curly braces for &lt;code&gt;if&lt;/code&gt; statements and sometimes not introduces inconsistency. Always using braces introduces consistency, and consistency increases readability and maintainability. The less work we have to do when understanding a program, the better.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Default To private Access Modifiers</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:29 +0000</pubDate>
      <link>https://dev.to/mbrubin56/default-to-private-access-modifiers-ooj</link>
      <guid>https://dev.to/mbrubin56/default-to-private-access-modifiers-ooj</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Access modifiers really do matter. It's hard to appreciate that with the small programs you're writing all by yourself when you first learn to program. But as programs get bigger and as more people work on them over time and together, the access modifiers matter more and more.&lt;/p&gt;

&lt;p&gt;Why is a large topic, and it may be hard to appreciate with the programs you've been writing. So I'll just be brief about one of the main reasons. So much of the trouble of hunting down bugs in our code is caused by its not being obvious when and why a variable changes state during the execution of the program or when and why a method is called during that execution. And even when it's obvious, there may be so many potential places that variable or method are accessed, we can't keep track of them all in our heads easily to help us think through the potential problems, especially over time and the larger the codebase gets. So one of the ways we combat this is by &lt;em&gt;restricting&lt;/em&gt; access to those variables and methods. It reduces the surface area of what could possibly go wrong and what we have to think about when hunting down a bug: "Since this variable is private, it can only be modified by methods in this class, so I can restrict myself to thinking about only what this class does to find my bug." It also restricts the possibility of our &lt;em&gt;making&lt;/em&gt; errors: you can't mistakenly call a method on a class from another class if it's not accessible to that second class.&lt;/p&gt;

&lt;p&gt;But I fear this may be one of those nuisance topics where your instructors always hassle you to update your access specifiers to be more restrictive and take off points when you don't, and it seems pointless to you. It might be one of those things where you'll just have to develop the habit of doing it the "right" way, according to your instructors, just to get them to stop bothering you and to give you higher grades before you come to experience why this is so valuable a practice.&lt;/p&gt;

&lt;p&gt;So default to specifying modifiers on methods as &lt;code&gt;private&lt;/code&gt;. That's the habit you want to develop. Make them &lt;code&gt;public&lt;/code&gt; only if you need to.&lt;/p&gt;

&lt;p&gt;And always make member variables &lt;code&gt;private&lt;/code&gt;. If you need to somehow expose the ability to set or get their values, create methods that provide a way for a user of your class to set or get the value from the method. Okay, there are exceptions, but none we need to talk about here, except if you're making a constant: e.g. &lt;code&gt;public static final double MAX_SALARY = 100.0;&lt;/code&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How To Get Out Of Main And Reduce Static</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:26 +0000</pubDate>
      <link>https://dev.to/mbrubin56/how-to-get-out-of-main-and-reduce-static-1ddp</link>
      <guid>https://dev.to/mbrubin56/how-to-get-out-of-main-and-reduce-static-1ddp</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  In the beginning...
&lt;/h2&gt;

&lt;p&gt;When you write your first program in Java, you encounter the need to have a method with the signature&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that serves as the entry point for your program. When you run your program, somehow the Java runtime finds this magic entry point, calls into, and you're off to the races. You'll have to house this method in a class, because that's just how it works.&lt;/p&gt;

&lt;p&gt;Then, whatever you learn next, you'll probably be reading and writing short programs that exercise the new Java language bits inside that &lt;code&gt;public static void main&lt;/code&gt; method. Maybe you're learning how to iterate with a &lt;code&gt;for&lt;/code&gt; loop, and so read or write a little program to sum up all the evens between 0 and 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from 0 to 10 is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It makes sense to keep that code in &lt;code&gt;main&lt;/code&gt; from a teaching and learning standpoint: it's the simplest place to put the code, and by introducing no additional complexity, the instructor and you get to focus on the other concepts being taught -- say, how a for loop works or how to use a local variable (&lt;code&gt;sumOfEvens&lt;/code&gt;) or how the algorithm works (adding 2 each time takes us to the next even). I have no problem with this teaching methodology. In fact, I think it's a good idea to leave you to code in &lt;code&gt;main&lt;/code&gt; while you focus on other things. &lt;/p&gt;

&lt;h2&gt;
  
  
  main is simply an entry point. It is not for modeling your data or problem space.
&lt;/h2&gt;

&lt;p&gt;But let's move forward some number of weeks in your learning. You're starting to learn how to use classes to model a domain, and your assignment is to create a program that asks you to hardcode the length of the side of a square, and print out the area and the perimeter. So you create something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;4.0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The perimiter of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a good start. It does what's asked. And it's somewhat nice that the class is named &lt;code&gt;Square&lt;/code&gt;. The name of the class tells me that we're doing something related to a Square in this class, but we're not really doing much modeling of a Square here. There's no representation of a side's length in the &lt;code&gt;Square&lt;/code&gt; class and there are no methods that we'd expect on a &lt;code&gt;Square&lt;/code&gt; class. We &lt;em&gt;call&lt;/em&gt; the class &lt;code&gt;Square&lt;/code&gt;, but since all of the logic for the squarey things we do live in &lt;code&gt;main&lt;/code&gt;, there actually isn't much that's squarey about this class, other than that the magic Java program entry point, &lt;code&gt;main&lt;/code&gt;, is packed with a series steps that relate to doing some operations on a square.&lt;/p&gt;

&lt;p&gt;Let's dwell on this. The &lt;code&gt;main&lt;/code&gt; method is not the best place to put your algorithms or to model the problem domain you're modeling: the need for a &lt;code&gt;main&lt;/code&gt; comes from a contract between the Java runtime and you about how to &lt;em&gt;start&lt;/em&gt; your program. That's it. It's how the Java runtime finds a place to start your program, and that is its primary role. It's not for modeling your problem space or writing interesting algorithms: &lt;em&gt;that&lt;/em&gt; modeling and those interesting algorithms belong in the distinctive methods and and fields of your classes. Treat &lt;code&gt;main&lt;/code&gt; solely as an entry point for the Java runtime to find, and put interesting work into your class methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't get stuck in the &lt;code&gt;static&lt;/code&gt; trap
&lt;/h2&gt;

&lt;p&gt;So you learn about class methods and class fields, and you rewrite your &lt;code&gt;Square&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.2f&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The perimiter of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an improvement. I can see more why this class is named &lt;code&gt;Square&lt;/code&gt;: it stores a side length, and it has methods to calculate the area and perimeter of a square, using the correct algorithms for them based on a square's side's length.&lt;/p&gt;

&lt;p&gt;But there's something funny here. Why is &lt;em&gt;everything&lt;/em&gt; &lt;code&gt;static&lt;/code&gt;? The field &lt;code&gt;sideLength&lt;/code&gt; is &lt;code&gt;static&lt;/code&gt;, and so are &lt;code&gt;getArea&lt;/code&gt; and &lt;code&gt;getPerimeter&lt;/code&gt;? In my experience, this is not so much a conscious choice, but a carry over from the fact that &lt;code&gt;main&lt;/code&gt; is &lt;code&gt;static&lt;/code&gt;. &lt;code&gt;main&lt;/code&gt; has to be &lt;code&gt;static&lt;/code&gt; because that's the contract with the Java runtime. And students get stuck then figuring out how to not carry on that &lt;code&gt;static&lt;/code&gt;. They find that they can't call directly a method named &lt;code&gt;setSideLength&lt;/code&gt; from their &lt;code&gt;static&lt;/code&gt; &lt;code&gt;main&lt;/code&gt; unless &lt;code&gt;setSideLength&lt;/code&gt; is &lt;code&gt;static&lt;/code&gt;, too. And then a &lt;code&gt;static&lt;/code&gt; &lt;code&gt;setSideLength&lt;/code&gt; can't set a value on that field &lt;code&gt;sideLength&lt;/code&gt; unless &lt;em&gt;it's&lt;/em&gt; &lt;code&gt;static&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So there are two big issues here. The first is whether we want all these to be &lt;code&gt;static&lt;/code&gt;. We don't, and I'll explain why. But more than that, the bigger conceptual issue is that it's not that students have made a &lt;em&gt;choice&lt;/em&gt; I disagree with to make the field and the methods of &lt;code&gt;Square&lt;/code&gt; &lt;code&gt;static&lt;/code&gt;: it's that students feel that they are forced into it by their &lt;code&gt;static&lt;/code&gt; &lt;code&gt;main&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There's a big difference between &lt;code&gt;static&lt;/code&gt; fields and non-&lt;code&gt;static&lt;/code&gt; (instance or member) fields and a big difference between &lt;code&gt;static&lt;/code&gt; methods and non-&lt;code&gt;static&lt;/code&gt; (instance or member) methods. The difference I want to focus on for now is that a &lt;code&gt;static&lt;/code&gt; field is &lt;em&gt;shared&lt;/em&gt; among all instances of &lt;code&gt;Square&lt;/code&gt;s, where each instance of &lt;code&gt;Square&lt;/code&gt; has its own private copy of non-&lt;code&gt;static&lt;/code&gt; fields. Conceptually, do we want to make it so that &lt;em&gt;all&lt;/em&gt; &lt;code&gt;Square&lt;/code&gt;s have the side length 3.2 when I set that value, or that just a particular &lt;code&gt;Square&lt;/code&gt; has a side length of 3.2? Or put more broadly, do we want the &lt;code&gt;Square&lt;/code&gt; class to model squares as if they all share the same side length, or do we want to allow it to model that different squares have different side lengths? The second, for sure. In that case, we don't want &lt;code&gt;sideLength&lt;/code&gt; to be &lt;code&gt;static&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Okay, so we don't want that field to be &lt;code&gt;static&lt;/code&gt;, but as I said, most students don't choose to make it &lt;code&gt;static&lt;/code&gt;: they feel forced into it because of the call to &lt;code&gt;setSideLength&lt;/code&gt; in their &lt;code&gt;public void static main&lt;/code&gt;, which itself is seemingly forced into being &lt;code&gt;static&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The solution is to create an &lt;em&gt;instance&lt;/em&gt; of &lt;code&gt;Square&lt;/code&gt;, and call a method on that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.2f&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The perimiter of the square is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setSideLength&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;getPerimeter&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much better! We now have made &lt;code&gt;sideLength&lt;/code&gt; a non-static, instance field, and we access it through non-static, instance members. We should probably add a constructor to &lt;code&gt;Square&lt;/code&gt; that takes an argument for the side length, too, but let's leave it at this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;There are two lessons here I want to summarize:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's very easy to feel stuck having to make everything in a class &lt;code&gt;static&lt;/code&gt; because you start calls to it from a &lt;code&gt;static&lt;/code&gt; method, &lt;code&gt;public static void main&lt;/code&gt;. But you don't have to be stuck: within your &lt;code&gt;main&lt;/code&gt;, you can create an &lt;em&gt;instance&lt;/em&gt; of your class and then make instance method calls on that instance. Those instance methods can access instance fields. If you &lt;em&gt;want&lt;/em&gt; to make a field or method on your class &lt;code&gt;static&lt;/code&gt; because that's appropriate, too, then great! But you don't have to.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public static void main&lt;/code&gt; serves as a rendezvous point between the Java runtime and your program. It is a contract between you and Java about how to start your program. That's it. It's not the place to put parts of your modeling or algorithms specific to your classes and their desired behaviors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's a related post about where &lt;a href="https://dev.to/mbrubin56/where-should-main-live-244h"&gt;&lt;code&gt;main&lt;/code&gt; should live&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What's A Good Error Message</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:23 +0000</pubDate>
      <link>https://dev.to/mbrubin56/whats-a-good-error-message-5hmf</link>
      <guid>https://dev.to/mbrubin56/whats-a-good-error-message-5hmf</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As you progress in your programs, you'll start to handle error conditions. Maybe you'll prompt your users to input a value for the side of a square, and they type a negative value or a string. There's the whole programming side of how you detect those error conditions, but that's not what this post is about. This post is about how to surface those errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error messages should be specific and actionable
&lt;/h2&gt;

&lt;p&gt;Let's say you have a UI where you ask your user to enter in a date of birth and a name. The user can enter in an invalid value for any of those. Tell them specifically what they did wrong and suggest what they can do to fix it. If they've made multiple errors, tell them about the multiple errors. &lt;/p&gt;

&lt;p&gt;Let's say you require that the name not be empty and you expect your dates to be entered in the format MM/dd/yyyy (two digit month followed by a slash, followed by a two digit day followed by a slash, followed by a four digit year). The user enters in some data, and they use an improper date format: they enter "5/14/70". An error message saying "Something went wrong" or "Invalid input" is not helpful. What went wrong? What should they fix? Should they fix their name or their date? And what's wrong with their name or their date? Tell them their date is in an incorrect format and what format you expect. It's frustrating as a user to have no idea how to fix whatever seems to be broken with the data they entered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tell them about all their errors at once, if you can
&lt;/h2&gt;

&lt;p&gt;Let's say they enter an incorrectly formatted date and left the name field blank. Tell them about both errors. It's frustrating for a user to fix one error and then resubmit their data and be told there are more errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't use computer jargon in your error messages
&lt;/h2&gt;

&lt;p&gt;When you're learning to program, you don't really have users: usually, there's you, the programmer pretending to be a user, and your instructor. And both of you are well versed in computer jargon. But you're practicing writing programs for other people. And other people are usually not Java programmers. So don't say something like "Price is incorrectly entered. Please enter a float." as your error message. Don't expect your user to know what a float is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error messages should be grammatical, and usually should be complete sentences
&lt;/h2&gt;

&lt;p&gt;Anything else puts a burden on your user to interpret what you must have meant. Grammatical, clear, full sentences are preferred because when you use them, your users will have the best chance of understanding what went wrong and how to fix it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Double check your error messages
&lt;/h2&gt;

&lt;p&gt;I see a lot of mistakes from beginning programmers that come from copy and pasting. They copy an error message about expecting an integer input to the place where they let a user know about an error where a string is expected, and so they tell the user that an integer is expected when really it's a string that's expected. We all do this - not just beginning programmers. But it's something we should all be reviewing to try to catch on our own.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How To Order Your Fields And Methods</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:20 +0000</pubDate>
      <link>https://dev.to/mbrubin56/how-to-order-your-fields-and-methods-2ihh</link>
      <guid>https://dev.to/mbrubin56/how-to-order-your-fields-and-methods-2ihh</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Be conventional
&lt;/h2&gt;

&lt;p&gt;Java does not require that you declare your fields at the top of the class and your methods below that. But that's what we do conventionally. Java does not require that constructors are placed above other methods, but that's what we do. I &lt;em&gt;think&lt;/em&gt; both of those habits promote readability on their own -- that it's generally easier to comprehend a class that way. But I'm not sure because that way of presenting a class is extremely conventional -- it's what everyone in the industry does. So I can't tell if these placements inherently make a class more readable, or they make a class more readable because I'm used to seeing a class presented that way and so can find my way around the class more easily when people obey the convention. Convention and patterns are very powerful tools for promoting understanding.&lt;/p&gt;

&lt;p&gt;You should be conventional, too, and follow those rules.&lt;/p&gt;

&lt;p&gt;The rest of what I'm going to say is definitely not universally practiced convention. In fact, for much of my career, I didn't follow these rules. When I joined &lt;a href="https://block.xyz/"&gt;Block&lt;/a&gt; (nee &lt;a href="https://squareup.com/us/en"&gt;Square&lt;/a&gt;), I was taught these rules, and I found them extremely helpful. So I'm passing them on to you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Put public things before private things
&lt;/h2&gt;

&lt;p&gt;I know, I know, we instructors are such nuisances when it comes to access modifiers, always telling you when you left something &lt;code&gt;public&lt;/code&gt; that should be &lt;code&gt;private&lt;/code&gt;. We harp on it so. It's annoying. But &lt;a href="https://dev.to/mbrubin56/default-to-private-access-modifiers-ooj"&gt;it is important&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you follow the rules in &lt;a href="https://dev.to/mbrubin56/default-to-private-access-modifiers-ooj"&gt;this post&lt;/a&gt;, you probably don't have &lt;code&gt;public&lt;/code&gt; member fields, but there can be reasons to make exceptions as you progress. Put all the &lt;code&gt;public&lt;/code&gt; fields above the &lt;code&gt;private&lt;/code&gt; fields.&lt;/p&gt;

&lt;p&gt;You almost certainly will have &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;private&lt;/code&gt; &lt;em&gt;methods&lt;/em&gt; though. Same rule: put the &lt;code&gt;public&lt;/code&gt; ones above the &lt;code&gt;private&lt;/code&gt; ones.&lt;/p&gt;

&lt;p&gt;Why? It goes back to why we even make the distinction between &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;private&lt;/code&gt; access modifiers. Our &lt;code&gt;public&lt;/code&gt;ly modified bits of code are available to any class to access. They represent the public face of your class: it's what is advertised to the rest of your classes what they can do with your class. So programmers trying to use your class will want to know what they can do about it, and putting that information at the top of the class is helpful.&lt;/p&gt;

&lt;p&gt;On the flip side, the programmers who use your class don't really care how it's implemented, so they don't care to or need to read or pay attention to your &lt;code&gt;private&lt;/code&gt; helper methods. Those are there for your class to do as it advertises it does, but people who use your class don't care and shouldn't care about those details.&lt;/p&gt;

&lt;p&gt;This may all be too abstract and you may not have had enough experience working on a shared project or a large project to appreciate this. In that case, I'm sorry to say you should just treat this as advice that some day will make more sense to you if you continue to program. At the least, it's a rule, and it provides a convention you can be consistent with, and that has value.&lt;/p&gt;

&lt;h2&gt;
  
  
  If possible, calling methods go on top of called methods
&lt;/h2&gt;

&lt;p&gt;If you have a method &lt;code&gt;private Date getDate()&lt;/code&gt; and it uses a helper method &lt;code&gt;private boolean isValidDate(Date date)&lt;/code&gt;, all things being equal, put &lt;code&gt;getDate&lt;/code&gt; above &lt;code&gt;isValidDate&lt;/code&gt;. That way, when you or anyone else is reading your code, they can follow the bigger picture before diving into the details. They will read that &lt;code&gt;getDate&lt;/code&gt; relies on &lt;code&gt;isValidDate&lt;/code&gt; and set that aside. Later, they can figure out what &lt;code&gt;isValidDate&lt;/code&gt; does.&lt;/p&gt;

&lt;p&gt;Here's another way to think of it. In English, we read from the top of the page down. And in general, when reading, we expect to encounter the more important things before the less important things. So we expect meatier, more algorithmically relevant methods to come first, and their helpers to come later.&lt;/p&gt;

&lt;p&gt;Sometimes you can't always obey this: you have methods crisscrossing in how they all call each other. That's okay. Just do your best. Exercise judgment over which should come before the other, focusing on what makes it easier for someone reading your class to figure out how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Violate these rules if it makes sense
&lt;/h2&gt;

&lt;p&gt;Readability, discoverability, and maintainability are the real goals here. If it seems to you that they are better met by a different ordering, go for it. As long as you're motivated by making your class easier to use or to understand or to maintain, you're focusing on the right things.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does main go?
&lt;/h2&gt;

&lt;p&gt;What about &lt;code&gt;public static void main(String args[])&lt;/code&gt;? If that's in your class, where should it go?&lt;/p&gt;

&lt;p&gt;This is sort of a trick question. It probably &lt;a href="https://dev.to/mbrubin56/where-should-main-live-244h"&gt;shouldn't be in your class at all&lt;/a&gt;. But if it is there, I recommend it be either the first thing we see, above your fields, or just after your fields. I do have one friend who says he would be surprised to see it anywhere except at the bottom of a class, though. Wherever it gets put, it's &lt;em&gt;incredibly&lt;/em&gt; important that it can be spotted easily and quickly, and not buried away in the middle of your class. &lt;/p&gt;

&lt;p&gt;But as I said, you know what makes it even easier to spot and less hard to find a place for in your class? Not having it in your class to begin with. Okay, it has to be in &lt;em&gt;some&lt;/em&gt; class. How about &lt;a href="https://dev.to/mbrubin56/where-should-main-live-244h"&gt;in a class named &lt;code&gt;Main&lt;/code&gt;&lt;/a&gt;, dedicated to housing &lt;code&gt;main&lt;/code&gt;?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Method Decomposition And Class Decomposition</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:50:16 +0000</pubDate>
      <link>https://dev.to/mbrubin56/method-decomposition-and-class-decomposition-32ik</link>
      <guid>https://dev.to/mbrubin56/method-decomposition-and-class-decomposition-32ik</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  In the beginning...
&lt;/h2&gt;

&lt;p&gt;When you're first learning Java, most of the time you'll be writing short programs whose code is placed entirely in a &lt;code&gt;public static void main&lt;/code&gt; method of a class that does nothing but host that &lt;code&gt;main&lt;/code&gt; method. For example, maybe you're learning how to iterate with a &lt;code&gt;for&lt;/code&gt; loop, and so read or write a little program to sum up all the evens between 0 and 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from 0 to 10 is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It makes sense to keep all of that code in &lt;code&gt;main&lt;/code&gt; from a teaching and learning standpoint: it's the simplest place to put the code, and by introducing no additional complexity, the instructor and you get to focus on the other concepts being taught -- say, how a for loop works or how to use a local variable (&lt;code&gt;sumOfEvens&lt;/code&gt;) or how the algorithm works (adding 2 each time takes us to the next even). I have no problem with this teaching methodology. In fact, I think it's a good idea to leave you to code in &lt;code&gt;main&lt;/code&gt; while you focus on other things. &lt;/p&gt;

&lt;p&gt;This was a small program, but since this is how students start, and each incremental addition in size feels so, well, incremental, often students stay with this model, eventually writing programs with far more code in their &lt;code&gt;public static void main&lt;/code&gt; method than is advisable. As soon as students learn what methods are and how to call them from one another, they should start decomposing their logic from &lt;code&gt;main&lt;/code&gt; into submethods: &lt;code&gt;main&lt;/code&gt; will call one or more helper methods which themselves may call one or more helper methods, and so on, until each method is simple and does a single thing. The goal is to have &lt;a href="https://dev.to/mbrubin56/meaningful-names-bh1"&gt;well named methods&lt;/a&gt; where each method has a single responsibility that aligns with the name of the method. &lt;/p&gt;

&lt;p&gt;Converting a single method that has lots of code embedded in it to do several things into multiple methods is called "decomposition" because it involves breaking apart (decomposing) the single big method into several smaller methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decomposing even this small example
&lt;/h2&gt;

&lt;p&gt;Some students appreciate the point, but aren't sure how to apply it, especially for a small example like this. What would decomposing this look like? Here's one go at it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;until&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from 0 to 10 is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine that instead of hardcoding that 10 for our stopping point, we prompt the user for our stopping point. Then we might have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requestStoppingPoint&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;requestStoppingPoint&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the stopping point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;until&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from 0 to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe we request a &lt;em&gt;starting&lt;/em&gt; point, too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requestStartingPoint&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requestStoppingPoint&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;requestStartingPoint&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the starting point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;requestStoppingPoint&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the stopping point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;until&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's appreciate the difference here from what a typical beginner's program would look like to do the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the starting point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the stopping point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you can see that the second, non-decomposed version, is harder to read and to follow, even for such a short program. Why is that? In large part it's because the entire logic of the program is presented in a single, long, linear sequence of code. You have to mentally figure out what each set of lines does, assign a sort of mental label to what each does, then consider how each interacts with the next mentally labeled bit of code, and so on, down the line. Beginning Java programmers acknowledge this by putting in those newlines to help separate out those mentally labeled bits of code. There's a reason why the newlines are placed where they are: their placement is an attempt to introduce structure and order onto what otherwise seems unstructured and unordered. In fact, you'll often see new programmers add comments to help them and their readers keep track of what's going on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Get the starting point.&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the starting point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Get the ending point.&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the stopping point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Sum the evens&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Print out the result&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So our new programmers usually &lt;em&gt;are&lt;/em&gt; correctly decomposing the problem and are looking for ways to indicate the problem decomposition. The right way to indicate the problem decomposition is to reflect it with method decomposition. &lt;/p&gt;

&lt;p&gt;Lots of nice things come from that method decomposition, but I think the most obvious one is that you no longer need those comments. If you use well named methods, the names of the methods tell me what they do, and the sequence of calls serve to narrate what's going on all on their own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Class decomposition
&lt;/h2&gt;

&lt;p&gt;Does the &lt;code&gt;main&lt;/code&gt; belong in this class, &lt;code&gt;EvenSummer&lt;/code&gt;? As I say in &lt;a href="https://dev.to/mbrubin56/where-should-main-live-244h"&gt;Where Should Main Live&lt;/a&gt;, I don't think so. I would move that &lt;code&gt;main&lt;/code&gt; out into its own class. If you do that, that's a form of class decomposition: breaking down the responsibilities of a single class into multiple classes, each with a dedicated purpose.&lt;/p&gt;

&lt;p&gt;What about the prompting? Should that be in the same class as the class that knows how to sum? What about the printing? It would be better if not and leave &lt;code&gt;EvenSummer&lt;/code&gt; with only the responsibility and modeling of how to sum evens. Make it pure and have it be responsible for one thing only, managing the logic for summing evens.&lt;/p&gt;

&lt;p&gt;So where would the input prompting and parsing and output go? Perhaps in a &lt;code&gt;Driver&lt;/code&gt; class that would ask for input, and call into &lt;code&gt;EvenSummer&lt;/code&gt;. How about a final program that looks like this (note that I'm including multiple files' contents here):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In Main.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Driver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sumUserSuppliedEvens&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In Driver.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Driver&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sumUserSuppliedEvens&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requestStartingPoint&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requestStoppingPoint&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EvensSummer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;requestStartingPoint&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the starting point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;requestStoppingPoint&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Type in a non-negative integer for the stopping point."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Ignore user input errors, and assume they'll type in a non-negative integer.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The sum of evens from "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;startingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;stoppingPoint&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In EvensSummer.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EvensSummer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;until&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;even&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've now decomposed our logic into classes, each with its own responsibility and within those classes, we've decomposed our logic into methods, each with its own responsibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do this?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Readability&lt;/strong&gt; It's a lot easier to read the decomposed code than the code that is not decomposed. It's easier for the author and for the code reviewers to understand what the program does as a whole, and what each part does and how they fit together. And we don't need comments: the code is &lt;em&gt;&lt;strong&gt;self-documenting&lt;/strong&gt;&lt;/em&gt;!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability&lt;/strong&gt; When you break up your methods and classes like this, it's much easier to make updates to your code later. In part, that's because of the improved readability -- you can see where the seams are in the code, and so where the appropriate places are to layer in new functionality. Hopefully, the stepwise way in which I was able to add prompting the user for two different inputs helps drive that home. And as I added more code, it didn't make it harder to read my program. Compare my method decomposed version to the version that did not decompose the methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debuggability&lt;/strong&gt; A lot of debugging involves visually inspecting your code to see what could have gone wrong. Breaking the code into distinct, dedicated methods in distinct, dedicated classes makes it easier for you to train your focus on just what that specific method is supposed to do, and whether it does it correctly. Your mental model gets simpler because each method is simpler. There are also benefits having to do with the impossibility of one bit of code's influencing another: with the non-decomposed, long line of code implementation, every bit of that implementation can share the same variables and possibly accidentally overwrite or misuse them. When you decompose your logic, you break your logic into smaller scoped code blocks that don't influence each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusability&lt;/strong&gt; Single purpose methods are easier to reuse. You can call the same method from different points in your code. That's not so obvious here, in this example, because we're not reusing any code. A lot of your early programs don't have much code reuse. And when you do need to reuse code, you'll probably naturally refactor your code so you have a method you can reuse. So this reason may fall a bit flat for you, even though it's true.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testability&lt;/strong&gt; Your textbooks will likely tell you that it's easier to test individual, small, single purpose methods and small, dedicated classes. That's &lt;em&gt;very&lt;/em&gt; true, and important. But you're also probably not writing testing code right now. So you'll have to trust us that this is true, and we're promoting good habits for later, when you do write tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How do I know when to decompose?
&lt;/h2&gt;

&lt;p&gt;It's often a judgement call when to decompose, and I would bet most students think that decomposing their methods and classes is adding a bunch of unnecessary overhead. Their programs are small, and they wrote them, so they can track what's going on in them.&lt;/p&gt;

&lt;p&gt;But your programs will grow with time, and developing and practicing the good habits of decomposing them now, when it's actually easier &lt;em&gt;because&lt;/em&gt; they are small, will serve you well.&lt;/p&gt;

&lt;p&gt;Plus, you sometimes do iterate on your same program. Homework assignments occasionally ask you to enhance an earlier assignment. So you may be, even in the beginning of your programming journey working on smaller programs, in the business of &lt;em&gt;maintaining&lt;/em&gt; your programs. And you certainly have to try to fix bugs. Even for these small problems, the benefits of decomposition come out, despite the fact that you can sometimes overcome the deficits of not decomposing by exerting some extra brain power.&lt;/p&gt;

&lt;p&gt;Here's a great tip. Beginning programmers &lt;em&gt;do&lt;/em&gt; have an instinct for this: as I said above, it's why they insert newlines into their code, and it's why they add comments to blocks of code. I see it when they ask me for help, and when we're looking at their code together, they tell me "This part here gets the starting number from the user; then this part gets the ending number; then this part sums the evens; and then this part prints out the answer." If you ever find yourself doing &lt;em&gt;any of these things&lt;/em&gt; -- inserting blank lines, adding comments, narrating to yourself or someone else what each part does -- that's a very strong signal you should decompose your logic!&lt;/p&gt;

&lt;h2&gt;
  
  
  Other notes
&lt;/h2&gt;

&lt;p&gt;Notice that in my decomposed example, I create two instances of &lt;code&gt;Scanner&lt;/code&gt;. I could choose to make one instance and pass it in to each of the methods: e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sumOfEvens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sumEvensUntil&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requestStartingPoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;requestStoppingPoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;outputSumOfEvens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sumOfEvens&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Updates to requestStartingPoint and requestStoppingPoint ommitted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also don't call &lt;code&gt;close&lt;/code&gt; on my &lt;code&gt;Scanner&lt;/code&gt;s (and so &lt;code&gt;close&lt;/code&gt; never gets called on those &lt;code&gt;InputStream&lt;/code&gt;s, &lt;code&gt;System.in&lt;/code&gt;). That's not great, but let's limit the contents of what we're dealing with. Closing resources is usually a later topic in a new Java programmer's journey.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Good Habits For New Java Programmers</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 18:49:48 +0000</pubDate>
      <link>https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2</link>
      <guid>https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2</guid>
      <description>&lt;p&gt;&lt;strong&gt;tl;dr&lt;/strong&gt; Here's the habits List&lt;/p&gt;

&lt;h2&gt;
  
  
  What's this all about?
&lt;/h2&gt;

&lt;p&gt;I'm an instructor for an introductory Java course. My students are learning Java mostly on their own using course texts, and I'm available for general help, and I review their code assignments. &lt;/p&gt;

&lt;p&gt;There are some common themes developing for the advice I've been giving my students on Java style, so I'm collecting that advice here. I use "Java style" loosely, covering more than just what a code formatter might handle, to include some basic, good habits that will help the students write cleaner, easier to read, and easier to maintain Java programs: topics such as variable, class, and method naming; what makes a good comment; some language usage tips (e.g. &lt;a href="https://dev.to/mbrubin56/-true-and-false-are-redundant-5aoc"&gt;&lt;code&gt;== true&lt;/code&gt; is redundant&lt;/a&gt;); and more. &lt;/p&gt;

&lt;p&gt;Although all the discussions are presented in the context of Java programming, they are generally good programming tips and extend naturally to other languages. For example, picking &lt;a href="https://dev.to/mbrubin56/meaningful-names-bh1"&gt;meaningful names for identifiers&lt;/a&gt; is important for any language you program in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Habits?
&lt;/h2&gt;

&lt;p&gt;I'm framing these tips as helpful &lt;em&gt;habits&lt;/em&gt; to develop because that framing helps motivate why I'm advocating for them in my code reviews of students' programs. The small size and short-lived nature of those programs obscure the value of the advice a bit. So some of the advice may seem like it isn't so important for this or that specific program, but it is a good habit nonetheless, and developing it will pay off in the long run with larger programs.&lt;/p&gt;

&lt;p&gt;For example, I push for giving variables meaningful names; but a variable named just &lt;code&gt;p&lt;/code&gt; is not hard to track in a ten line program, and that program, just a one-off homework assignment, isn't going to be maintained over time or read by lots of people. So what's the harm in a one letter name in this case? Well, at the very least, the harm is in not developing the right habits. Following my advice will help students start off straight away doing the right things. Bad habits are hard to break, and if you're a student reading this, if you continue with your programming, you'll be glad you developed the right habits early on. I wish I had gotten this advice and developed those habits sooner than I did. &lt;/p&gt;

&lt;p&gt;Developing these habits can add a bit of extra mental work as you start out, but not that much, and it's very worth it. If the other reasons I offer for each item don't hit home, you will just have to trust me on the good habits bit. Sometimes you have to learn the whats before the whys.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;p&gt;If you're a student, it might be useful to maintain a checklist based off of these habits I'm asking you to develop. Then you have a structured way to review your code before you submit it. Reviewing and applying the checklist will help you develop the right habits and will reduce how much of your assignment I'll ask you to revise and improve your grade.&lt;/p&gt;

&lt;h2&gt;
  
  
  List of habits:&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/mbrubin56/meaningful-names-bh1"&gt;Meaningful Names&lt;br&gt;
&lt;/a&gt;&lt;a href="https://dev.to/mbrubin56/naming-conventions-55ni"&gt;Naming Conventions&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/default-to-private-access-modifiers-ooj"&gt;Default To private Access Modifiers&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/writing-good-comments-1j3e"&gt;Writing Good Comments&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/always-use-braces-for-ifelse-2fkg"&gt;Always Use Braces For if/else&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/-true-and-false-are-redundant-5aoc"&gt;== true and == false Are Redundant&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/use-your-ides-code-formatter-1al9"&gt;Use Your IDE's Code Formatter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/whats-a-good-error-message-5hmf"&gt;What's A Good Error Message&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/how-to-get-out-of-main-and-reduce-static-1ddp"&gt;How To Get Out Of Main And Reduce Static&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/method-decomposition-and-class-decomposition-32ik"&gt;Method Decomposition And Class Decomposition&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/where-should-main-live-244h"&gt;Where Should Main Live&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/mbrubin56/how-to-order-your-fields-and-methods-2ihh"&gt;How To Order Your Fields And Methods&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Meaningful Names</title>
      <dc:creator>Mark Rubin</dc:creator>
      <pubDate>Wed, 31 Aug 2022 16:35:25 +0000</pubDate>
      <link>https://dev.to/mbrubin56/meaningful-names-bh1</link>
      <guid>https://dev.to/mbrubin56/meaningful-names-bh1</guid>
      <description>&lt;p&gt;This is part of a brief series on &lt;a href="https://dev.to/mbrubin56/good-habits-for-new-java-programmers-1n2"&gt;Good Habits For New Java Programmers&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use &lt;em&gt;meaningful&lt;/em&gt; names for variables
&lt;/h2&gt;

&lt;p&gt;A meaningful name for a variable is one that tells us what the variable represents or what it's used for or how it's used in the program, and does not use too much shorthand. Let's dive in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't name variables after the type they store
&lt;/h2&gt;

&lt;p&gt;There's a tendency to use a letter or name for a variable that simply matches the type of the variable.&lt;/p&gt;

&lt;p&gt;Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The name &lt;code&gt;d&lt;/code&gt; is not meaningful. It hints that the variable stores a &lt;code&gt;double&lt;/code&gt;, but it doesn't tell me what this &lt;code&gt;double&lt;/code&gt; is going to be used for or what it's storing. Likewise, &lt;code&gt;array&lt;/code&gt; and &lt;code&gt;doubles&lt;/code&gt; in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;doubles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;are not examples of meaningful names. The first name, &lt;code&gt;array&lt;/code&gt;, conveys to me that the variable stores an Array, and the second name, &lt;code&gt;doubles&lt;/code&gt;, conveys that variable stores an Array of &lt;code&gt;double&lt;/code&gt;s. But knowing the types are not enough to know what the variables do in our program. Neither tells me &lt;em&gt;why&lt;/em&gt; I'm going to be storing an array of &lt;code&gt;double&lt;/code&gt;s or how this Array is going to be used in the program.&lt;/p&gt;

&lt;p&gt;Let's imagine that these &lt;code&gt;double&lt;/code&gt;s are meant to store prices. Then these would be more meaningful names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should get even more specific, if that makes sense:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;coffeePrice&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;coffeePrices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Don't over abbreviate your variable names
&lt;/h2&gt;

&lt;p&gt;There's a tendency to needlessly abbreviate otherwise meaningful names. Here are some examples someone might use instead of &lt;code&gt;double sellerCost;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;double sc;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;double sCost;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;double selCost;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Those are in increasing order from worst to better (though none great), as the increased number of letters gives more hints as to what the abbreviation is for. Don't force yourself or your readers to guess how to interpret a &lt;a href="https://en.wikipedia.org/wiki/Vanity_plate"&gt;vanity license plate&lt;/a&gt; style abbreviation -- just make it &lt;em&gt;obvious&lt;/em&gt;:&lt;br&gt;
&lt;code&gt;double sellerCost;&lt;/code&gt;&lt;br&gt;
It doesn't cost you much: you type a few extra letters the first time, and then your IDE/programming environment will give you autocomplete suggestions for the next time you want to refer to that identifier. And it &lt;em&gt;&lt;strong&gt;dramatically&lt;/strong&gt;&lt;/em&gt; improves the readability and maintainability of the code. Not just for me, the reader, but for you: when reading an expression such as &lt;code&gt;sc - bp&lt;/code&gt; you will have to translate &lt;code&gt;sc&lt;/code&gt; into "seller cost" and &lt;code&gt;bp&lt;/code&gt; into "buyer price" to understand what the expression is doing. That translation adds a mental tax. You don't have to pay that tax when reading &lt;code&gt;sellerCost - buyerPrice&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sure, a name like &lt;code&gt;purchPrice&lt;/code&gt; for a purchase price isn't that hard to expand in your head to "purchase price", but why not just write it out in full to save us all the mental step? &lt;code&gt;purchasePrice&lt;/code&gt; is a better name for your variable than &lt;code&gt;purchPrice&lt;/code&gt;, even if &lt;code&gt;purchPrice&lt;/code&gt; isn't &lt;em&gt;terrible&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Don't tack on numeric values to variable names to distinguish them
&lt;/h2&gt;

&lt;p&gt;Sometimes you need to represent two of a similar kind of value: maybe you have two kinds of prices, one for the cost of something to you, the seller, and one for the sales price that the buyer pays. Use &lt;strong&gt;meaningful&lt;/strong&gt; names to distinguish your variables for them. &lt;code&gt;costPrice&lt;/code&gt; and &lt;code&gt;salesPrice&lt;/code&gt;, for example. Or maybe &lt;code&gt;sellerCost&lt;/code&gt; and &lt;code&gt;buyerPrice&lt;/code&gt;. You get to think of what names make sense, but you have to put some effort into it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't use &lt;code&gt;price1&lt;/code&gt; and &lt;code&gt;price2&lt;/code&gt;&lt;/strong&gt;. The &lt;code&gt;1&lt;/code&gt; and the &lt;code&gt;2&lt;/code&gt; don't tell you or me (the reader of your code) anything meaningful that helps us to remember or keep track of the difference in what the variables represent. Will you remember as your program expands which of &lt;code&gt;price1&lt;/code&gt; or &lt;code&gt;price2&lt;/code&gt; holds the cost to the seller or the cost to the buyer? Later when you read a line of code&lt;br&gt;
&lt;code&gt;double profit = price1 - price2;&lt;/code&gt;&lt;br&gt;
will you be able to see easily if that's a mistake and should be the other way around? Should it be&lt;br&gt;
&lt;code&gt;double profit = price2 - price1;&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;It's much easier to understand your program if instead, you had&lt;br&gt;
&lt;code&gt;double profit = buyerPrice - sellerCost;&lt;/code&gt;&lt;br&gt;
That will help you make sure your programs are correct, help your readers of your code help you improve it, and make it easier to build on your program later: if you have to come back and add more functionality later -- let's say sales tax -- it'll be much easier to know which of those variables you should be applying the sales tax to.&lt;/p&gt;

&lt;p&gt;There are counterexamples (aren't there always?), but in general, you should ban the usage of number suffixes on your identifiers: don't tack on a &lt;code&gt;1&lt;/code&gt; or a &lt;code&gt;2&lt;/code&gt;, etc. onto any identifier.&lt;/p&gt;
&lt;h2&gt;
  
  
  Use &lt;em&gt;meaningful&lt;/em&gt; names for methods, classes, parameters, and everywhere else, too.
&lt;/h2&gt;

&lt;p&gt;A method named &lt;code&gt;doIt()&lt;/code&gt; doesn't have a helpful name. Do what? What happens when you add another method? How will you distinguish them? Perhaps with a method named &lt;code&gt;doIt2()&lt;/code&gt;? Which one does what? &lt;/p&gt;

&lt;p&gt;That &lt;code&gt;doIt&lt;/code&gt; example may seem far-fetched, but it's not uncommon to see students create a method &lt;code&gt;getPrice()&lt;/code&gt; (much more meaningful than &lt;code&gt;doIt()&lt;/code&gt;, for sure!) and then need another method to do something similar, and create a &lt;code&gt;getPrice2()&lt;/code&gt;. Instead, it would be better to have methods named, for example, &lt;code&gt;getSellerCost()&lt;/code&gt; and &lt;code&gt;getBuyerPrice()&lt;/code&gt;. Now you know and I know what these methods should do.&lt;/p&gt;

&lt;p&gt;The same sort of thing applies to class names or wherever you come up with names in your programs. If you see a method signature&lt;br&gt;
&lt;code&gt;private void getProfit(double d1, double d2)&lt;/code&gt;&lt;br&gt;
do you know which &lt;code&gt;double&lt;/code&gt; is intended to hold seller's cost and which is intended to hold the buyer's price? How about&lt;br&gt;
&lt;code&gt;private void getProfit(double sellerCost, double buyerPrice)&lt;/code&gt;? That makes it clear.&lt;/p&gt;
&lt;h2&gt;
  
  
  I admit it: there are exceptions
&lt;/h2&gt;

&lt;p&gt;Okay, so, it's true that sometimes, the language for the subject matter we're talking about &lt;em&gt;naturally&lt;/em&gt; uses abbreviated terms or single letters for things. For example, when we talk about points on a plane, we use &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; to represent the coordinates of that point. In that case, it does make sense to have variables named &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;: e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it is convention to use &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;j&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt; for iterators in for loops. E.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// Your code here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there's a natural abbreviation that does not require work to decipher, it's okay to use that: &lt;code&gt;String ceoName;&lt;/code&gt; is perfectly readable and much more natural than &lt;code&gt;String chiefExecutiveOfficerName;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And sometimes, you really are writing a generic function that takes two of a primitive type, say, and it makes sense to write something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  You can go too far
&lt;/h2&gt;

&lt;p&gt;My experience is that there's little danger here for new programmers, but it's also true that a name like &lt;code&gt;itemPriceIncludingAllTaxesAndDiscounts&lt;/code&gt; is a bit of a mouthful and takes up a lot of room on a line. Naming is hard and involves judgement, but in general, err towards more informative names than less informative ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sometimes examples you find do not use meaningful names
&lt;/h2&gt;

&lt;p&gt;Sometimes the texts you use or examples you look up on the web will not do as I'm saying. &lt;/p&gt;

&lt;p&gt;That's unfortunate, since it means some of your authorities on how to write code will be tacitly contradicting my advice. And it's common (advisable, even!) to copy and paste examples to help you get started on some piece of code, and it's easy to miss the need to go back and clean up the example. But do go back and clean up what you copied to use meaningful names.&lt;/p&gt;

&lt;p&gt;Perhaps there's some good fortune in the fact that you'll run across plenty of code that doesn't do as I say, though. I'm hoping you'll realize that when you read that code, you find it harder to read and harder to learn from than you would have had your source used more meaningful names. Maybe that will help this topic seem like sound advice, rather than just a set of rules to follow.&lt;/p&gt;

&lt;p&gt;If you're here, you might also want to read &lt;a href="https://dev.to/mbrubin56/naming-conventions-55ni"&gt;Naming Conventions&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
