Pages

Showing posts with label FizzBuzz. Show all posts
Showing posts with label FizzBuzz. Show all posts

Tuesday, August 30, 2011

FizzBuzz in the Eclipse IDE

A "Simple Program"

"FizzBuzz programs" are tests of basic programming ability proposed by Imran Khan in the article "Using FizzBuzz to Find Developers who Grok Coding." Khan designed the FizzBuzz tests after meeting many programmers who struggled to solve simple problems, and argued that the tests could be used to weed out weak programmers during the interview process. The FizzBuzz challenge is as follows:
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

In my ICS 314 class, Professor Johnson assigned us the task of writing a FizzBuzz program in Eclipse, an integrated development environment (IDE) for Java developers, and measuring the time it took to do so. The below program was written and checked for correct output in a time of  5 minutes and 35 seconds:

Eclipse

I found Eclipse easy to use, as I already had prior experience with this IDE from my ICS 211 class. Eclipse has many useful features, including refactoring, automatic error detection, and easily compiling/exporting .jar files.

While using Eclipse in ICS 211, one of the most common error notifications that I saw was "Cannot make a reference to the non-static method "foo()" from the type "bar". In FizzBuzz, when I tried to call determineOutput(int i) in main, this error appeared again. Since this was a timed exercise, I just made the method static; I knew that if I did, Eclipse would stop bothering me and let the program run. I knew this because, in ICS 211, I had gotten into that exact habit of declaring many methods static just to make Eclipse stop bothering me. As it turned out, Professor Johnson went over this issue on the first day of class: static methods should be used as little as possible because, as described by Miško Hevery on the Google Testing Blog, they make testing more difficult.

A Lesson in Coding Techniques

In the case of the FizzBuzz program, we were shown how to get around having to use a static method by instantiating a copy of the FizzBuzz class within the main() method of the FizzBuzz class itself:

In this case, the non-static version of determineOutput can be accessed by any class (including a tester) that instantiates a copy of the FizzBuzz class. This can be done without causing compiler warnings or other problems when FizzBuzz.determineOutput(int i) is called:

To me, the FizzBuzz static method example is related to one of the lessons of software engineering: code must be written not just to finish a task, but to ensure that you and others can easily understand, test, and improve it. The attitude of "planning takes too long, just finish this" was a major part of my programming process when I took 211, and it probably resulted in some of the long nights of testing I experienced in which I struggled to find all of the logical bugs I had unintentionally introduced. This stronger emphasis on planning clearly sets apart a higher-level class like ICS 314 (which focuses on developing more efficient and elegant methods of engineering software) from a more basic-level class like ICS 211 (which teaches basic methods of implementing standard Java classes and data structures). In a class like 211 you are given tools and must be taught how to use them without hurting yourself, but in a class like 314 you learn how to build precisely and build to last.