Java Style Guide for WebCAT submissions
1 Code formatting
Line length
Make sure every line is at most 80 characters long. I prefer to stop around 75th character - it avoids unpleasant surprises and is more readable.
Tabs
As you may have figured out are a big trouble. This is not just our idea. If several people work on a project and exchange code, different tab settings in different IDEs and different operating systems will quickly turn the code into unreadable mess, if tabs are not replaced by spaces.
White space within the code
The code is more readable if every operator (e.g. + - * &&) is preceeded and followed by a blank. You should also have a blank space after each comma, and before each left brace. (I have not been doing the last one myself in the past - so I edit my programs by replacing every left brace by a left brace preceeded by a space.
Blank lines in the code
This is currently not checked, but will be in the future. You should have a blank line before every method definition and before every constructor. Basically, if any item has a comment line, there should be a blank line before the comment.
2 Coding conventions
The names of all classes and interfaces must start with a capital letter.
The names of all members, i.e. names of fields and methods defined within a class or interface, must start with a lower case letters. If the name consists of several words, use the camelCaseStyle - i.e. start every new word after the first one with a capital letter.
this
Use this. before any reference to a member of the class be it a field or a method. Seasoned programmer have reported that it helps them understand quickly what is the meaning of the variables used in method definitions.
Formatting if - else clauses
We do not enforce this now, but will do so in the furture. Every action following the if (condition) or following the else clause of an if statement shold be enclosed in curly braces. The only exception is the formatting of consecutive if - else if - else if statements.
Here are two examples:
if (x < 7) {
return "small";
}
else {
return "big";
}
if (a < b) {
return "smaller";
}
else if (a == b) {
return "equal;
}
else {
return "bigger";
}
3 Code organization
Java requires that any public class or interface be defined in a file that matches the name of that class or interface (followed by .java). We allow you to define several classes or interfaces in one file, but make sure the file name matches the name of the first class or interface defined in the file. Otherwise, as WebCAT examines your file, it creates a new temporary file with a new name that matches the defined class or interface and looses its ability to refer to your original file.
We will gradually move to this style, as we start using the visibility modifiers public and private for the purposes they are designed to serve.
The tester library and the WebCAT submission script require that all examples of data and all your tests be defined in a class whose name starts with Examples.
Make this a separate file. In general, you want to separate your code from the tests that you run on your code.
Define the Examples... class without public and without any constructor.
When programing the interactive games the code that displays the Canvas and the code that actually runs the game cannot be run on the WebCAT server, and the methods that invoke the display or the bigBang method cannot be tested.
Place all this code in a class GameRunner. This class is excluded from the test coverage checks when your submission is processed by the WebCAT server.
4 Compiling the code
For the program to be graded it must compile. If you cannot complete any part of the assignment, you should comment out the parts that do not compile and submit it that way, subject to the following:
4.1 Method definitions
The assignment lists the methods that will be tested by the instructor’s test program.
If you cannot design these methods, include at least the method header and a stub code within that produces some value of the expected type. So, for example, if the method produces a boolean value, just write return true;, if it produces a String, wirte return "s";, etc. This will make your program compile and will run all instructor’s tests, even though they will probably fail.
4.2 Data definitions
Sometimes the assignment lists the specific instances of data that your program must define. Typically, these will be tested by the instructor’s test program.
If you cannot define the desired data, again, define any instances you can, as long as they are of the desired type. Try not to leave the data undefined.
Ordering the data definitions
Just like in DrRacket, if one of your pieces of data contains another instance of data, that item needs to be defined before it is referenced. Java will not signal an error for the code shown below:
|
Book catInTheHat = new Book("Cat in the Hat", drS, 20); |
Author drS = new Author("Dr Suesse", 100); |
but if you try to find out whether the author of catInTheHat is the same as drS it will fail. At the time the instance of the book catInTheHat has been defined, the author was unknown (null in Java), and the program does not go back to re-initialize all instances that have referred to drS when the field is finally initialized.
If you get unexpected "NullPointerException", check that all data definitions refer to object whose values are already known.
Note: If you can think of other helpful hints that should be included, please let me know and I will add them.