###So you want to write good comments.
So you want to write good comments.
###So you want to write good comments.
So you want to write good comments.
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
if (firstLetter == 'y' || firstLetter == 'Y') {
play = true;
} else {
play = false;
}
However you're using a conditional to assign true
in one branch, and false
in the other - the whole if
block is therefore useless:
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
play = firstLetter == 'y' || firstLetter == 'Y';
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
play = firstLetter == 'y' || firstLetter == 'Y';
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
if (firstLetter == 'y' || firstLetter == 'Y') {
play = true;
} else {
play = false;
}
However you're using a conditional to assign true
in one branch, and false
in the other - the whole if
block is therefore useless:
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
play = firstLetter == 'y' || firstLetter == 'Y';
###So you want to write good comments.
Good comments...
- ...say why, not what.
- ...don't state the obvious or rephrase what the code is already saying.
- ...don't turn into lies if the code changes.
Let's see...
import java.util.*; //so I can use scanner
The problem is that you're importing an entire library, and specify that you're only doing that for one of its types. Either you're lying and you're actually using more than that, or the import
statement is overkill.
int max = 100; //sets limit on the random number generator
Comments on variables usually indicate poor naming, because good identifiers are self-explanatory. If max
was called maxRandomValue
, would this comment be needed?
int guess;//so I can compare console input to random number
This is another comment that used in-place of a more meaningful identifier. If guess
was called userInput
, would this comment be needed?
while (play) { //so game will restart after a win
There's no reason to have this comment either; it's pretty clear what the intent is here. The only potential issue I'm seeing with while (play)
is that play
is a verb, when a Boolean identifier is clearer when it starts with is
or has
- in this case, while(isPlaying)
.
//so user can continue guessing until correct
boolean win = false;
This comment isn't needed either. Again it's pretty clear what the intent is, but then again, it looks like a verb and could be called isCorrectGuess
.
This is an interesting one:
//so user can choose to play another game
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
if (firstLetter == 'y' || firstLetter == 'Y') {
play = true;
} else {
play = false;
}
The indentation is clearly off here, and it's confusing. Consider:
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
play = firstLetter == 'y' || firstLetter == 'Y';
Now, about the comment...
//so user can choose to play another game
This one is formulated in a way that makes it look like it's saying why, but it basically says "this chunk of code does [...]". Whenever that happens, the rule of thumb is that you should extract that chunk into its own method.
Bottom line, I don't think any of these comments are really needed. And do yourself a favor, drop that "so abc can xyz" formulation.