Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please review my code

 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use eclipse, install the Checkstyle plug-in. This will display all violations of Sun's Code Conventions for the Java Programming Language.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting a space in front of and after "=" would help with the legability.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic