Yet even more JAVA programming basics...

This document was written by CS 290W TA Joshua Kay and was last modified

Alright, we have been pretty stringent about our code so far. We have written all of our for, while, and do...while statements pretty much the same. But everyone has a different programming style, so let's talk about what's allowed, and also what is preferred.

First off, white space is ignored by the compiler. White space is all of the spaces *IN BETWEEN*. For example:

for(x = 0; x < 10; x++ ) { //action; } ... is exactly the same as...
for(x=0;x<10;x++){//action} ... which is exactly the same as...
for(x=0; x<10; x++){ //action; } ... which is exactly the same as...
for(x=0; x<10; x++) { //action; } Now, obviously, some of these are harder to read than others. Three general rules apply:
  1. Don't cram it all together. It's hard to read.
  2. Don't spread it way far apart. It's hard to follow.
  3. Be consistent with your style.
Now, we will be grading your code - and one of the things we will grade on is readability of your code. So, remember that before you try to cram everything onto one line, or take five pages for seven lines of code. It is also easier to debug if you code well. Finally, if your program doesn't work - well, if we can't figure out what you were trying to do when we look at your code, you won't get any credit at all.

So, let's give you the recommended styles, and you can choose...

import java.applet.*; public class Style1 extends Applet { Button button=new Button("New Button"); public void init() { add(button); } public void paint(Graphics g) { for(int x=0; x<1; x++) { //this does nothing but demonstrate style of coding } g.drawString("Writing Style#1",50,50); } } Here's the second:

import java.applet.*; public class Style2 extends Applet{ Button button=new Button("New Button"); public void init(){ add(button); } public void paint(Graphics g){ for(int x=0; x<1; x++){ //this does nothing but demonstrate style of coding } g.drawString("Writing Style#2",50,50); } } These are the two most popular forms of coding, or a cross between. Remember, find your style and stick with it!