Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Packages</strong> - As the calculator's only going to have a few classes, you don't need to worry about design of packages too much, but as a rule of thumb, you want to minimise dependencies between them and have classes within them related around a central theme (low coupling, high cohesion). So, even a set of packages like com.kevin.calculator.view, com.kevin.calculator.controller and com.kevin.calculator.model are a good start. </p> <p><strong>Class Design</strong> - What you've started with is a pretty good attempt. When you go about deciding on classes, you should follow <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod" rel="nofollow noreferrer">S.O.L.I.D design principles</a>. In your case, the most important of these principles is the <em>Single Responsibility Principle</em> - ideally, every class should have only one reason to change. If it has more than one reason, coupling is increased, making improvements harder and unexpected side-effects more likely. For example, if you were to include the calculator (business) logic with the responsibility for updating the GUI, you'd have two reasons for change. In the future, you might want to add more calculator logic (think scientific calculator functions), or alternatively, change the appearance and positioning of the buttons. Having this all in the one class means we have to always be careful that changing one responsibility doesn't break the other. It also makes it harder for others to come and understand what's going on in the class. Model-View-Controller is a very nice and standard way of partitioning the responsibilities into separate classes, but even here, you want to make sure they contain only one reason to change, that's why it's common to have JTableModel type classes separate to JListModel classes. </p> <p><strong>Declarative Design</strong> - something else I found tremendously useful when I was starting out was making code as easy to read as possible by not using comments. Comments aren't bad, but question why you use them - is it because your code is hard-to-read? Much better than this is to use lots of methods with descriptive names. Instead of:</p> <pre><code>if(input &lt; 0 || input &gt; 999999999) { throw new BadInputException(); } // continue processing </code></pre> <p>prefer</p> <pre><code>if (checkInputIsInvalid()) { throw new BadInputException(); } // continue processing </code></pre> <p>because it tells the reader what you're doing, without getting them unnecessarily lost in the details of the code, when all they want to do is change a specific functionality (and may not even care about what's valid, only that a check is done).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload