Java Magazine, Sept/Oct 2018
Using Comments to Design Classes What is the first thing you write when creating a new class Like most developers I believe I create classes of two distinct types the ones where my head is full of contextual information and I need to bang out a class that meets my immediate needs and the ones that I patiently and carefully design The former constitute the majority of my classes alas and they are initially sloppy beasts I hit a keystroke combo and the IDE creates a new class with the copyright block in place I give it a name and I immediately start hacking If Im refactoring code then the IDE will either pull in the code from another class or generate new code for me I love todays tools I can go for a while putting into the code all the things teeming in my brain without fear that I am creating an unsightly tangle My lack of fear is because I know I will refactor the code write the necessary tests compile it finish cleaning it up run the tests and move on ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 05 from the editor PHOTOGRAPH BY BOB ADLER STUDIO AT GETTY IMAGES FOR ORACLE But Im increasingly coming to the opinion that this series of steps which is familiar to every developer creates a lot of unnecessary activity A better approach is to write down all the same data in words rather than code Suppose instead of code I wrote the following comments This class validates a ticket number by computing a pair of check digits Since we acquired company name the checkdigit algorithm varies by vendor so first look up the vendor The constructor accepts the ticket number and the principal method returns an enum indicating a valid number or the type of error All exceptions are caught and converted to error enums This method is called only by class name and so should be private Java in the Cloud Oracle Cloud delivers high performance and battle tested platform and infrastructure services for the most demanding Java apps Oracle Cloud Built for modern app dev Built for you Start here developer oracle com developersrule
You must have JavaScript enabled to view digital editions.