Skip to main content

Crash course in writing code

Posted by kirillcool on November 23, 2005 at 4:59 AM PST

Following the previous entry on bug handling, here is the second chapter - writing code.

public class Developer {
   public void writeCode(AssignmentDetails details) {
      Document designDocument = details.getDesignDocument();

      // see if it was done already somewhere in the project
      if (details.isEnhancement()) {
         int myExperience = this.getExperience(Calendar.YEAR);
         if (myExperience <= 2) {
            // Can safely ignore the design requirements. If they are
            // so clever, why don't they write code anymore?
            designDocument.ignore();
            // Time to reinvent the wheel. Surely the guys before me
            // didn't know what they were doing (those "gurus"). It
            // will take me half the time with zero bugs
            details.setClosingDate(Calendar.getCurrent().add(Calendar.DAY, 2));
            // Like i need to see what's out there
            Internet.getDefaultBrowser().close();
            while (true) {
               try {
                  System.hang();
               }
               catch (InterruptedException exc) {
                  System.out.println("Will be ready in a few days");
               }
            }
         }
         else {
            if (myExperience <= 5) {
               // Time to copy-paste. If I change the existing function or
               // refactor it, I'll have to run tons of backwards-compatibility
               // tests.
               Set<Method> methodsToCopy = details.getBase().getImplementation();
               Package newPackage = this.getProject().createNewPackage(
                   details.getBase().getPackage().getName() + ".issue" + details.getID());
               newPackage.create();
               for (Method method : methodsToCopy) {
                  method.update(details);
                  newPackage.getClass(method.getClass()).add(method);
               }
               // test
               for (TestCase case : designDocument.getTestCases()) {
                  boolean success = case.run();
                  if (!success) {
                     // damn!!
                     if (!designDocument.isReadOnly()) {
                        designDocument.getTestCases().remove(case);
                     }
                     else {
                        // damn!!!
                        designDocument.descope(case.getFunctionality());
                     }
                  }
               }
               // All tests passed successfully (woot)!
               return;
            }
            else {
               // I'm a guru. WTF? Don't they know that gurus' time is more important?
               List<Developer> devs = this.getProject().getDevelopers();
               Collections.sort(devs, new Comparator<Developer>() {
                  public int compare(Developer d1, Developer d2) {
                     return d1.getExperience(Calendar.YEAR) - d2.getExperience(Calendar.YEAR);
                  }
               });
               Developer junior = devs.get(0);
               // 0wned!!!
               return junior.writeCode(details);
            }
         }
      }
     
      // A new feature. Let's try the Internet first.
      Browser browser = Internet.getDefaultBrowser().open();
      browser.go("http://slashdot.org");
      browser.hangAround(Date.HOUR, 1);
      browser.go("http://javablogs.com");
      browser.hangAround(Date.MINUTE, 20);
      browser.search("BileBlog");
      browser.hangAround(Date.SECOND, 5);
     
      browser.go("http://www.google.com");
      browser.type(details.getShortDescription);
      browser.click(Key.ENTER);
      // No need to check that the code actually does what it's supposed
      // to. Google knows what it's doing.
      String contents = browser.getFirstLink().copy();
     
      Package package = this.getProject().getRootPackage();
      package.getClass(details.getShortDescription().trim()).write(contents);
     
      for (TestCase case : designDocument.getTestCases()) {
         boolean success = case.run();
            if (!success) {
               // Ignore - if the testers are smart enough to catch them,
               // hopefully this will be after I move to another project.
            }
         }
      }   
     
   }
}