This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Getting MEAN with Mongo, Express, Angular, and Node and have Simon Holmes on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Please review my code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please review my code" Watch "Please review my code" New topic

Please review my code

Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340

Following is a method. I want to know how will I indent long lines (or wrap them) of code.I don't want a line of code to have more than 80 characters.

Apart from the above, how do I line-wrap this statement (one line should not be more than 80 characters)

[ September 17, 2005: Message edited by: Danish Shaukat ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Perhaps first you should choose a documented formatting standard and follow its rules. For example Elements of Java Style.

Otherwise, I would suggest throwing the code into an IDE like Eclipse and choosing Source->Format. After you find out what that looks like, you can then tweak Eclipse's code formatter to produce the results you would like to have.

I guess there are other pretty-printers that would also do the job for you.

To do it manually you can set your tab width to 4 spaces and a wrap indention to be 8 spaces. String literals have to be broken by hand into pieces and concatenated with the + operator. The compiler joins the pieces up at compile time.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
A second spent formatting code is a second wasted. There are tools that will automatically format your code and many of them run from the command line if you have no interest in an software development console commonly known as an IDE � integrated development environment.

Use your editor/IDE to format the code for you. Use a battle-tested format rather than presume that some how you know better than what the industry is using. Without question, the worst programmers I know also have this habit of "cleaning up" code to fit a bizarre non-standard style.

Your employer won't like paying to do something that has been automated for probably 30 years now.

P.S. Your code looks excellent as shown.
[ September 17, 2005: Message edited by: Rick O'Shay ]
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056

although, btw, since these particular exceptions are runtime exceptions, they don't need to be declared at all.
[ September 17, 2005: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
They don't need to be as per the rules of a compileable source code unit, but they mey need to be in accordance perhaps with some other fundamentals. That is, if your public contract may throw a NullPointerException, you should let your clients know (shouldn't you?), and by declaring it with a 'throws' clause allows this information to be available through reflection. In fact, all public contract operations (interface methods) that accept one or more reference type parameters should (at least mine do) declare to throw NullPointerException; the optimal, but not very nice, workaround to the flawed existence of 'null' i.e. fail as early as possible.

Alternatively, if you don't wish to encourage the bind from your API to the language (and particularly its flaws), use an annotation; although unconventional, you get all the benefits otherwise.

Tony Morris
Java Q&A (FAQ, Trivia)
Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Thanks for the guidance, everyone.

I do use Text Pad as my Java editor. But still I wish to keep my line length less than 80 characters so sometimes I have to line wrap the code myself.

Anyway, here is the above code with line length less than or equal to 80 characters. If there are flaws in line wrapping, please let me know.

And the last line...

[ September 17, 2005: Message edited by: Danish Shaukat ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If you use eclipse, install the Checkstyle plug-in. This will display all violations of Sun's Code Conventions for the Java Programming Language.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Putting a space in front of and after "=" would help with the legability.

Please ignore post, I have no idea what I am talking about.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

I myself use Textpad, and have found that Jalopy works great within Textpad -- I set it up as a tool, and so to format I simply do a CTRL-4 and voila! I have formatted code.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Please review my code