Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code Format

 
loni gashi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me if I'm writing the code in correct format like spacing, comments, etc.
Text in code is in my native language. Thanks

 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That tends to be a matter of opinion or, all too often, screaming disagreements. I might not have blank lines in all the places you do, and I definitely would drop the comments after each closing brace, but otherwise I think it's fine. You used spaces, not tabs, right?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65114
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Correct" is in the eye of the beholder (or group policies).

The only thing I'd personally strongly object to is not leaving spaces between the comment delimiters and the comment words..

I'd also lose the comments at the ends of the loops. They're superfluous.

And yes, spaces, not tabs. Tabs are the work of evil.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also suggest that whatever you do, be consistent. You sometimes put a space before =, and sometimes not. And sometimes a space after the =, and sometimes not. Same for >, < and other similar symbols. Try picking one way of doing it, and doing it consistently. Most coding standards I've seen suggest one space before and after each binary operator or comparator (+, -, =, ==, >, <, etc) so that's what I'd do.
 
Stevens Miller
Bartender
Posts: 1254
24
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Tabs are the work of evil.

"I respectfully disagree." -the BackSpace key
 
Stevens Miller
Bartender
Posts: 1254
24
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:I would also suggest that whatever you do, be consistent.

By all the gods, man, that's the secret!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49774
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are also inconsistent with your comments. You are sometimes using /*...*/ and sometimes // for block comments.
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indent style
 
Winston Gutkowski
Bartender
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are also inconsistent with your comments. You are sometimes using /*...*/ and sometimes // for block comments.

@Ivan: One thing I would suggest is that you learn about Javadoc comments ('/** ... */') as soon as possible, and start using them wherever you can - ie, classes, methods and fields.

Personally, I use them everywhere, even on private members, because I know that it means I have ready-made documentation if/when I need it.
On the rare occasions that I also need to comment a particular piece of code (maybe because it's complex, or not obvious), I generally use the '//' style; but that's just for me so that I have a clear visual difference; it's certainly not mandatory.

In my view, Javadoc is one of the things that sets Java apart from every other language I've ever learned. It's also the basis of all that wonderful API documentation.

Winston
 
Stevens Miller
Bartender
Posts: 1254
24
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Indent style

Good reference, Ivan. I am a long-time adherent to what that article names "Allman style," which is pretty much this:
That's probably because I did a lot of Pascal programming before moving on to C. Over the next 30 years, I took lots of flak for it because most of my colleagues used what the article names "K&R style," which is pretty much this:
However, I also noticed that I was monkishly devoted to the use of my style choice, whereas most of my colleagues were less strict with themselves about their own choices. Not to beat this to death, but the article quotes K&R themselves, with what I believe is the best advice on this topic there is:
The position of braces is less important, although people hold passionate beliefs. We have chosen one of several popular styles. Pick a style that suits you, then use it consistently.

Nearly all else either matches between the styles, or is something people don't care enough about to get red-faced in fist-clenching rage over it (no kidding: where that opening brace goes can put your life in danger, if you show the "wrong" style to the wrong person). But, fwiw, I also noticed that, because I never depart from my choice, my colleagues eventually just got used to it and stopped bitching at me about it.

Of course, life gets more complicated when one is working for one of those soul-crushing sweatshops that requires everyone to adhere to the "company style manual." I'd avoid working in one of those, partly because company-wide enforcement of a style choice has, in my experience, been more to actualize some senior manager's personal wishes about where the opening brace goes than to improve productivity. As a working compromise, I simply adopt the style of whomever's code I am working with, and I ask others to maintain my style when they work on code I wrote. The exception is when I find I am working on code that does not consistently apply a single style, in which case I plant my flag, declare dominion, and reformat it all to my personal taste.

Life is, of course, too short to have spent as much time on this as I just already have, so, in general, it's best to be flexible about this, use the form that causes the least grief to all involved, and devote your energies to other things.

But, for God's sake, pick one style and use it.
 
Stevens Miller
Bartender
Posts: 1254
24
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to prove that I believe I am going to live forever, I've taken one additional moment to reformat the OP's example according to almighty God's my own style choice:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49774
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it was called BSD/Allman, but it is a long time since I read that article. That is a very useful addition to the Ranch, SM. Thank you.
 
Winston Gutkowski
Bartender
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Just to prove that I believe I am going to live forever, I've taken one additional moment to reformat the OP's example according to almighty God's my own style choice:

Now see, I have to admit I prefer K&R, which probably just means that I have lived forever (or as near as makes no diff in CS terms).

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic