Skip to main content
added 888 characters in body
Source Link
dustytrash
  • 2.4k
  • 8
  • 17

Add method documentation also known as JavaDocs when a method is unclear.

Taking the a glance at the method ballInSlot from the view of another programmer:

public static int ballInSlot(String path, int Slots[]) {
...
return frequencyOfRight;

It's really hard to tell what this method is actually doing. If you cannot make it clear through naming, it might be an indication the method is too complicated and should be split into multiple methods.

However sometimes this isn't possible / doesn't help. In these cases you should also add documentation:

Add a method documentation AKA Java Docs:

/**
 * explanation of the method
 * @param path explanation of the parameter 'path'
 * @param Slots explanation of parameter 'Slots'
 * @return what does the method return?
 */

IMO every public method should be documented, and every unclear private method. Some people believe every method should be documented. However it's pretty standard to thesedocument methods since it's verythat are unclear from reading the return statements & the method names.

However you should also add documentation to these methods since it's very unclear from reading the return statements & the method names.

Add method documentation also known as JavaDocs when a method is unclear.

Taking the a glance at the method ballInSlot from the view of another programmer:

public static int ballInSlot(String path, int Slots[]) {
...
return frequencyOfRight;

It's really hard to tell what this method is actually doing. If you cannot make it clear through naming, it might be an indication the method is too complicated and should be split into multiple methods.

However sometimes this isn't possible / doesn't help. In these cases you should:

Add a method documentation AKA Java Docs:

/**
 * explanation of the method
 * @param path explanation of the parameter 'path'
 * @param Slots explanation of parameter 'Slots'
 * @return what does the method return?
 */

IMO every public method should be documented, and every unclear private method. Some people believe every method should be documented. However it's pretty standard to document methods that are unclear.

added 189 characters in body
Source Link
dustytrash
  • 2.4k
  • 8
  • 17

Don't instantiate a new Scanner object for every input, youDon't instantiate a new Scanner object for every input

You only ever need 1 Scanner.

Format Strings

You can use System.out.printf to print formatted strings, such as:

System.out.printf("You are dropping %d balls into %d slots\n", numberOfBalls, numberOfSlots); 

Put comments above the line of code, not in the same line.Put comments above the line of code, not in the same line.

// like this
someCode();

someCode(); // NOT like this

Use java naming standards,Use java naming standards

private & method scoped variables should begin with a lower case letter. The formatting in stackexchange gives you a hint, it thinks Slots is a class since it begins with an uppercase:

// should be int slots[].
int Slots[] = new int[numberOfSlots];

Give names that make sense

eachBallPath doesn't really make sense as a method name. printEachBallPath would make more sense. Same printBallPaths would make even more sense.

Or if you'd like, calculateBallPaths, and have the method return an Array that can be printed elsewhere.

Same for ballInSlot and pathTaken. Maybe getBallInSlot and getPathTaken. If you don't like using get for methods other than getters, I'd suggest calculatePathTaken & calculateBallInSlot.

However you should also add documentation to these methods since it's very unclear from reading the return statements & the method names.

Regarding the code below:Regarding the code below:

always use curly braces even when the code is 1 length long.

It's upsetting when instructors teach students to this, it's more upsetting that your instructor actually encourages it (As you mentioned in the comments.

A good explanationA good explanation:

Skimping on braces might save you a few keystrokes the first time, but the next coder who comes along, adds something to your else clause without noticing the block is missing braces is going to be in for a lot of pain.

Write your code for other people.

use else if or else whenever it makes sense to do so

You can use shorthand +=, -=, *=, /= etc, instead of variable = + variable etc.

if (random < 0.5)
    path = path + "L";
if (random >= 0.5)
    path = path + "R";

Don't instantiate a new Scanner object for every input, you only ever need 1.

You can use System.out.printf to print formatted strings, such as:

System.out.printf("You are dropping %d balls into %d slots\n", numberOfBalls, numberOfSlots); 

Put comments above the line of code, not in the same line.

Use java naming standards, private & method scoped variables should begin with a lower case letter. The formatting in stackexchange gives you a hint, it thinks Slots is a class since it begins with an uppercase:

// should be int slots[].
int Slots[] = new int[numberOfSlots];

eachBallPath doesn't really make sense as a method name. printEachBallPath would make more sense. Same for ballInSlot and pathTaken. Maybe getBallInSlot and getPathTaken. However you should also add documentation to these methods since it's very unclear from reading the return statements & the method names.

Regarding the code below:

always use curly braces even when the code is 1 length long.

It's upsetting when instructors teach students to this, it's more upsetting that your instructor actually encourages it (As you mentioned in the comments.

A good explanation:

use else if or else whenever it makes sense to do so

You can use shorthand +=, -=, *=, /= etc, instead of variable = + variable etc.

if (random < 0.5)
    path = path + "L";
if (random >= 0.5)
    path = path + "R";

Don't instantiate a new Scanner object for every input

You only ever need 1 Scanner.

Format Strings

You can use System.out.printf to print formatted strings, such as:

System.out.printf("You are dropping %d balls into %d slots\n", numberOfBalls, numberOfSlots); 

Put comments above the line of code, not in the same line.

// like this
someCode();

someCode(); // NOT like this

Use java naming standards

private & method scoped variables should begin with a lower case letter. The formatting in stackexchange gives you a hint, it thinks Slots is a class since it begins with an uppercase:

// should be int slots[].
int Slots[] = new int[numberOfSlots];

Give names that make sense

eachBallPath doesn't really make sense as a method name. printEachBallPath would make more sense. printBallPaths would make even more sense.

Or if you'd like, calculateBallPaths, and have the method return an Array that can be printed elsewhere.

Same for ballInSlot and pathTaken. Maybe getBallInSlot and getPathTaken. If you don't like using get for methods other than getters, I'd suggest calculatePathTaken & calculateBallInSlot.

However you should also add documentation to these methods since it's very unclear from reading the return statements & the method names.

Regarding the code below:

always use curly braces even when the code is 1 length long.

A good explanation:

Skimping on braces might save you a few keystrokes the first time, but the next coder who comes along, adds something to your else clause without noticing the block is missing braces is going to be in for a lot of pain.

Write your code for other people.

use else if or else whenever it makes sense to do so

You can use shorthand +=, -=, *=, /= etc, instead of variable = + variable etc.

if (random < 0.5)
    path = path + "L";
if (random >= 0.5)
    path = path + "R";
added 189 characters in body
Source Link
dustytrash
  • 2.4k
  • 8
  • 17

Don't instantiate a new Scanner object for every input, you only ever need 1.

You can use System.out.printf to print formatted strings, such as:

System.out.printf("You are dropping %d balls into %d slots\n", numberOfBalls, numberOfSlots); 

Put comments above the line of code, not in the same line.

Use java naming standards, private & method scoped variables should begin with a lower case letter. The formatting in stackexchange gives you a hint, it thinks Slots is a class since it begins with an uppercase:

// should be int slots[].
int Slots[] = new int[numberOfSlots];

eachBallPath doesn't really make sense as a method name. printEachBallPath would make more sense. Same for ballInSlot and pathTaken. Maybe getBallInSlot and getPathTaken. However you should also add documentation to these methods since it's very unclear from reading the return statements & the method names.

Regarding the code below:

always use curly braces evenalways use curly braces even when the code is 1 length long.

It's upsetting when instructors teach students to this, it's more upsetting that your instructor actually encourages it (As you mentioned in the code is 1 length longcomments.

A good explanation:

use else if or else whenever it makes sense to do so

You can use shorthand +=, -=, *=, /= etc, instead of variable = + variable etc.

if (random < 0.5)
    path = path + "L";
if (random >= 0.5)
    path = path + "R";

Don't instantiate a new Scanner object for every input, you only ever need 1.

You can use System.out.printf to print formatted strings, such as:

System.out.printf("You are dropping %d balls into %d slots\n", numberOfBalls, numberOfSlots); 

Put comments above the line of code, not in the same line.

Use java naming standards, private & method scoped variables should begin with a lower case letter. The formatting in stackexchange gives you a hint, it thinks Slots is a class since it begins with an uppercase:

// should be int slots[].
int Slots[] = new int[numberOfSlots];

eachBallPath doesn't really make sense as a method name. printEachBallPath would make more sense. Same for ballInSlot and pathTaken. Maybe getBallInSlot and getPathTaken. However you should also add documentation to these methods since it's very unclear from reading the return statements & the method names.

Regarding the code below:

always use curly braces even when the code is 1 length long

use else if or else whenever it makes sense to do so

You can use shorthand +=, -=, *=, /= etc, instead of variable = + variable etc.

if (random < 0.5)
    path = path + "L";
if (random >= 0.5)
    path = path + "R";

Don't instantiate a new Scanner object for every input, you only ever need 1.

You can use System.out.printf to print formatted strings, such as:

System.out.printf("You are dropping %d balls into %d slots\n", numberOfBalls, numberOfSlots); 

Put comments above the line of code, not in the same line.

Use java naming standards, private & method scoped variables should begin with a lower case letter. The formatting in stackexchange gives you a hint, it thinks Slots is a class since it begins with an uppercase:

// should be int slots[].
int Slots[] = new int[numberOfSlots];

eachBallPath doesn't really make sense as a method name. printEachBallPath would make more sense. Same for ballInSlot and pathTaken. Maybe getBallInSlot and getPathTaken. However you should also add documentation to these methods since it's very unclear from reading the return statements & the method names.

Regarding the code below:

always use curly braces even when the code is 1 length long.

It's upsetting when instructors teach students to this, it's more upsetting that your instructor actually encourages it (As you mentioned in the comments.

A good explanation:

use else if or else whenever it makes sense to do so

You can use shorthand +=, -=, *=, /= etc, instead of variable = + variable etc.

if (random < 0.5)
    path = path + "L";
if (random >= 0.5)
    path = path + "R";
Source Link
dustytrash
  • 2.4k
  • 8
  • 17
Loading