GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Code Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code Format" Watch "Code Format" New topic
Author

Code Format

loni gashi
Greenhorn

Joined: Dec 11, 2012
Posts: 11
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

Joined: Oct 01, 2001
Posts: 2853
    
  11

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

Joined: Jan 10, 2002
Posts: 61314
    
  66

"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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
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
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Bear Bibeault wrote:Tabs are the work of evil.

"I respectfully disagree." -the BackSpace key
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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

Joined: Oct 13, 2005
Posts: 39079
    
  23
You are also inconsistent with your comments. You are sometimes using /*...*/ and sometimes // for block comments.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Indent style
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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

Joined: Oct 13, 2005
Posts: 39079
    
  23
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

Joined: Mar 17, 2011
Posts: 7892
    
  21

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
 
GeeCON Prague 2014
 
subject: Code Format