• Post Reply Bookmark Topic Watch Topic
  • New Topic

First Java program, just want some general tips on structure  RSS feed

 
Cj Nicholas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good afternoon experts. I picked up the Head First Java book yesterday and am enjoying it. I wrote a short program, and it compiles and runs (hooray) but I'd like some tips on making it more...programmatically acceptable? I did some programming years ago in Basic, Pascal, and a little bit of C++, so I understand many of the concepts. I have this sneaking feeling, however, that my code will make some of you twitch. Can you please offer feedback to help guide me in the future, and avoid bad habits now? I left out remarks to keep it short, and I may suffer from a mild aversion to white-space. Thanks!

 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cj,

congrats on getting into Java and especially for getting a program of more than a couple lines to compile and run--I had an office mate in college that never had a program of 5 lines or more actually compile.

Java has a Random that is over in java.util.Random that has much more functionality than the one in Math. It has a feature like nextInt where you can specify the range of int you want back. Your code looks good,
mostly there are just style comments--which I will keep to myself, as I am a style Natzi--so once again: congratulations.

Les
 
Cj Nicholas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Les. I am actually looking for style comments too. I may not use them all, but anything that makes my code easier to read will be a benefit, I think, so Natzi away!
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Cj, you asked for it--and remember style is personal preference unless enforced by some type of rule where you work, so what you will now see if my personal style preferences.

There is your code with my preferences--btw: I call all my temp programs for the forums "Junk".
Cj Nicholas wrote:Thanks Les. I am actually looking for style comments too. I may not use them all, but anything that makes my code easier to read will be a benefit, I think, so Natzi away!
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:style is personal preference unless enforced by some type of rule
I do not agree. There is defined already and recommended style by Sun (April 20, 1999). There are given couple of options how to place braces, so you can choose one of them, but the rest as spaces per indentation and other rules are clearly defined. The more people stick to a standard with least improvisation - the better.

Code Conventions for the Java TM Programming Language.

And that code would read following OP's brace placing style:

I might sometimes too pedantic, but that is how I believe it suppose to be.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There we will just have to agree that we do not agree. I've followed the Sun standard and went off he accepted path long ago, the closer together is easier to follow, especially on today's larger monitors. Back in the day when you had 13" or maybe 15" monitors, then you needed more white space--now, I just don't agree. When a standard quits working or becomes a hindrance for you, then it's time to change the standard. BTW: I adopted the current style I have when I went from a 17 to a 21 inch monitor because there was just too much space and I had a difficult time focusing on large sections of code.
Liutauras Vilda wrote:
Les Morgan wrote:style is personal preference unless enforced by some type of rule
I do not agree.
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan, Isn't it bad that you using two spaces instead of tab indents ?
Remember that most of the time, the code you write will not be maintained by you, but by someone else. Hence there are standard and indentations so that everyone follows the same unified approach.
 
Cj Nicholas
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both, so much. I am encouraged that I did most things right and still created a working program. I wasn't aware of the style guide, so thanks for pointing me to it.

Here's what I take away:
1) Preferred open brace placement is to not put it on its own line (but it's ok if I do)
2) Closing brace is on its own line
3) Preferred is to use a blank line to separate statements
4) Indent consistently

It appears I got the variable declarations right and in the right place (front of the block). I haven't learned what a "final" is yet although I'm guessing it means a constant. I also haven't learned about importing yet, but I think I understand what it does (adds class libraries).

I used the Math class because that's what the book used, but I am glad to learn other ways to do things.

Apologies if I am not using the right terms for things, I don't have much background in Object Oriented Programming. Thanks for making my first trip into Java a good one!
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My IDE resolves tabs to spaces any way, so the tab indent is not really that much of an option. I do that because there are compilers out there that don't like tabs, and unless you resolve your tabs to spaces, then you may find out in an unceremonious fashion that takes quite a while to find. Since I have been the senior developer/programmer/lead since I was 1 year out of college, then my will has pretty much been the gold standard. After using the 2 space instead of 5, and the other preferences I have, I have not seen anyone complain about it. They get used to it and find that they can view a lot more on the screen than they could otherwise, and that is what it is meant to do--put more code into your view so you can wrap your head around bigger sections of what is happening.

BTW: that standard was set in 1999 and used in an industry where knowledge doubles about ever year--so a standard from almost 20 years ago on style maybe could be updated--just something to think about.

salvin francis wrote:Les Morgan, Isn't it bad that you using two spaces instead of tab indents ?
Remember that most of the time, the code you write will not be maintained by you, but by someone else. Hence there are standard and indentations so that everyone follows the same unified approach.
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
My IDE resolves tabs to spaces any way, so the tab indent is not really that much of an option.


Most modern IDE would definitely allow you to customize that. I have seen customizations that allow to convert tab to 8 spaces within the IDE itself

Les Morgan wrote:
I do that because there are compilers out there that don't like tabs, and unless you resolve your tabs to spaces, then you may find out in an unceremonious fashion that takes quite a while to find.


I haven't quite encountered a compiler which has such restrictions, maybe you could point me out any ?

Les Morgan wrote:
Since I have been the senior developer/programmer/lead since I was 1 year out of college, then my will has pretty much been the gold standard. After using the 2 space instead of 5, and the other preferences I have, I have not seen anyone complain about it. They get used to it and find that they can view a lot more on the screen than they could otherwise, and that is what it is meant to do--put more code into your view so you can wrap your head around bigger sections of what is happening.

BTW: that standard was set in 1999 and used in an industry where knowledge doubles about ever year--so a standard from almost 20 years ago on style maybe could be updated--just something to think about.


I wouldn't quite comment on above, but here's my opinion :
Its more tedious to add 2 spaces than tabs. When there are huge structures of code, a tab would show a clearer picture than two space indent
Your own code posted above is a simple example of you goofing up on your own format : (line 16)

Here's what the standard java coding conventions say :
4 - Indentation
Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).



YMMV
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they do, and that is why I choose 2 instead of 4 or 5 or 8, the line 16 break in my indent is a copy paste artifact and not in the original code.

My code style is the product of my choices through almost 3 decades of programming, but it is my style unless I enforce it upon others and I have and do for those that work for me, but when I work for someone else I use their preferred style--I was a consultant long enough to know that the client many not always be right, but his money is what I want, so he gets what he wants. BTW: we also call that "being a code *ho**".

One thing we will agree upon, I am sure, is that consistent style is a lot easier to read that no consistency or style at all.

salvin francis wrote:
Les Morgan wrote:
My IDE resolves tabs to spaces any way, so the tab indent is not really that much of an option.


Most modern IDE would definitely allow you to customize that.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cj,

In any case the thing to remember is that if you are working for someone, consult the accepted standard of the shop, and consistent style is the key to readability.

Les
Cj Nicholas wrote:Thank you both, so much. I am encouraged that I did most things right and still created a working program. I wasn't aware of the style guide, so thanks for pointing me to it.

Here's what I take away:
1) Preferred open brace placement is to not put it on its own line (but it's ok if I do)
2) Closing brace is on its own line
3) Preferred is to use a blank line to separate statements
4) Indent consistently

It appears I got the variable declarations right and in the right place (front of the block). I haven't learned what a "final" is yet although I'm guessing it means a constant. I also haven't learned about importing yet, but I think I understand what it does (adds class libraries).

I used the Math class because that's what the book used, but I am glad to learn other ways to do things.

Apologies if I am not using the right terms for things, I don't have much background in Object Oriented Programming. Thanks for making my first trip into Java a good one!
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
One thing we will agree upon, I am sure, is that consistent style is a lot easier to read that no consistency or style at all.


Agreed and it would be lot (more) easier if consistency has standard conventions with it
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I'm with Les on most of his preferences.

I use two spaces for indentation, and never tabs. The benefit of using only spaces is that the code looks the same everywhere. For a language that promotes consistency, it sure is weird that they suggest using tabs.

You can use horizontal alignment very effectively to make some blocks of code more readable. Using tabs will mess that up when the code is viewed in an editor with different tab settings.

Any argument that standard X takes more time to type is flawed, because the time you take to type code should pale in comparison to the time you take to think about the code. Besides, most IDEs can convert tabs to spaces, and will automatically insert indentation on a new line.

As for curly braces, I also prefer to put them on the same line, unless the header is more than one line long. In that case, I put the opening brace on the same line as the closing parenthesis. For instance, here's how I like to format try-with-resources and disjoint catch clauses:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now that you know everything there is to know about proper formatting of code, you might want to look at this: http://www.coderanch.com/t/660020/Wiki/Main-Pain
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!