Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 07-20-2013, 04:59 PM   #11
Site Team
berry120's Avatar
Join Date: Jul 2009
Location: England, UK
Posts: 3,434
Default Re: My first Java program.

The improvements everyone has suggested thus far are good, and I'd echo celegorm's point about not expecting user input to be sanitised - some very famous and expensive programming mistakes have happened this way, and no doubt many more will happen in the future. I'm not sure I'd go as far as to do the same thing for, say, UI controls that can only accept numbers - I haven't known a version of Java that broke something that fundamental, and I'd argue that the excess code here harms readability more than the benefit you'd gain in such a corner case. But I agree with the principle, and functionally at least it can't do any harm to double check.

Where I will chime in is on style - overall it's not bad, but just a few points that I think are worth mentioning:
  • Inline Comments - You've got a lot of comments in your code like "//print message" and so on. Look at any piece of code written by a beginner and they'll almost always put these in, look at any piece of code written by a professional, and apart from in odd corner cases, you won't find any. That's because too many comments in the wrong places can actually *hurt* readability of the code. If you feel that you must comment something this way, then you should really break it up into separate methods and use:
  • Javadoc comments. These are what professionals *do* use, and I rarely see beginners use these - but they're what are often required in a professional setting. The javadoc tool takes these and generates the code documentation for you, so they're what people actually look at. Pretty much every method you write should be commented this way, in this format - they start "/**" and end "*/". See here for details - worth taking this on board
  • Indentation - it's pretty good, but there's a couple of cases (such as in your last return statement) where it doesn't quite line up. Get this right early on, otherwise when you're staring at complex code later it's really going to haunt you if you can't read through its flow simply.
  • Java is an OO language by nature, and while many people start just using static methods as defined here try not to go too far down this road before you start looking at objects. When you understand them then most coding should be done via objects, not static methods - all the main method should really do is instantiate an object or two and perhaps call a method or two on them. The rest should be handled in non-static code. (Just something to bear in mind for the future.)

Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 07-21-2013, 04:00 AM   #12
Baseband Member
Join Date: Apr 2012
Location: UK
Posts: 78

Thanks for all your feedback guys! I am going to reasearch some more, I will keep everything you have said in mind in my next project. Thanks again

CPU - AMD Athlon II X3 450, MB - Asus M5A78L-M LX, GPU - EVGA GTX 660 Ti, RAM - 8GB Ballistix 1333Mhz, PSU - 600W OCZ Modxstream-Pro
ParalizedTime is offline   Reply With Quote
Old 07-29-2013, 05:26 PM   #13
Beta Member
the prince's Avatar
Join Date: Jul 2013
Location: usa
Posts: 1
Default Re: My first Java program.

good work go man....!

the prince is offline   Reply With Quote

first program, java

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 01:50 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0