• Post Reply Bookmark Topic Watch Topic
  • New Topic

Readability of my code  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been exploring the basics of Java this past month. I have read the first 5 chapters of Java Software Solution (Edition 7) and worked though some of the problems.

I was just wondering if the following code is readable. Do I have clear variables and enough comments?

 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julie Anne,
Welcome to CodeRanch!

Yes, your code is very easy to read. A few comments on conventions that will be good habits to get in the habit of:

1) Class names use camel case. This means that we use names like HiLo rather than Hi_Lo. The capital shows we are starting a new "word".
2) Most people declare one local variable per line.
3) If you've learned "static" yet, MAX should be one. If not, don't worry about it. It will come up eventually that constants can be shared by the class.

As far as readability goes, this is wonderful. I only have one suggestion. If you declare count where you first use it, you don't need to have the code to reset it. (because it would get automatically redeclared in each iteration of the loop.) That's the only thing that "took me by surprise." It's not wrong/bad the way you did it though. Just not necessary.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Julie Anne!

Your code looks very good - it's well-formatted, and indented correctly, it's easy to read, so you did a good job!

There's one minor point: In some of the "if" and "else" statements, you're not using curly braces { and } to delimit the statement that's inside the "if" or "else". For example in lines 36, 45, 47 and 54, 55.

Java doesn't require you to use { and } if the body of the "if" or "else" consists of just a single statement, but it's considered good style to always use the { and }, even if the body consists of just one statement. Always doing this will protect you from an easy to make mistake, when you actually have multiple statements that are supposed to be in the body - if the indentation is wrong it might mislead you to think that the body consists of for example two statements, while in reality only the first statement is in the body.

For example:

 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the quick replays. I will take both of your suggestions into account.

I am really enjoying learning Java and grateful to have found this helpful forum to ask questions on.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks ok
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch both of you.

Does the book really teach you to write main() methods that long?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing, which the book may not have shown you.
You appear to have a loop which you intend to run once regardless. That means you are checking at the end of the loop for repetition. The do‑while statement is specifically intended for that sort of requirement. Alternatively you can have a test at the beginning of a while loop and get the input simultaneously. You do need some strange syntax, however:-Note I am using stop in that example, rather than yes for continuing. Hence the bang sign ! for “not”.

\u201c or d gives you “posh quotes”.
 
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julie-

It looks really good. I've seen many unreadable programs from my students. I'm impressed that you took the time to write readable code. For more information, take a look at these coding conventions:

Oracle Java Code Conventions: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
Google Java Style: https://google-styleguide.googlecode.com/svn/trunk/javaguide.html
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote: . . . take a look at these coding conventions:

Oracle Java Code Conventions: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
Google Java Style: https://google-styleguide.googlecode.com/svn/trunk/javaguide.html
But beware: they are different from each other. If your employer or teacher has coding conventions you must stick to them. Until then, choose one convention and stick to that. The first link is actually Sun's conventions and they are fifteen years old.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But beware: they are different from each other. If your employer or teacher has coding conventions you must stick to them. Until then, choose one convention and stick to that. The first link is actually Sun's conventions and they are fifteen years old.

Understood. I was just giving OP some guidelines to read. Some companies I've worked for had strict coding standards. Some had none. Some students I've talked to lost points for not using/understanding camel case. You are right. It depends on the receiver of the code. I wonder if OP had specific coding conventions to follow.
 
Julie Anne Moore
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks every one for the helpful information.

Campbell Ritchie:

So far the book has only shown how to write a class and create a driver class (example class: Die, driver class: RollingDice).
Yes the books main methods that are are long.

Thanks for the tip about do-while statement. They are the in next chapter.

Edwin Torres:

Thanks for the information of coding conventions. I have been following the books advice so far, and am waiting to see what my professor ask for when I start classes this fall.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!