Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Requesting Constructive Criticism About My Code

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Requesting Constructive Criticism About My Code

    Hey everybody, after learning most of the basic stuff in java(Strings, Variables, Classes etc...) I really wanted to make a program that would have user input and was also useful. So I made a program that would allow the user to enter a username-password combination and if both entries were correct, it would open up a web page to something really cool, in this case, a picture of bacon. So I would really appreciate if you looked at my code and gave me some tips for future reference.
    Here is the code, is there any easier way I could have done this/anything that I missed?

    import java.io.IOException;
    import javax.swing.JOptionPane;
    public class Chuck {
    public static void main(String[] args){
     
    String username = JOptionPane.showInputDialog("Enter Username");
    String password = JOptionPane.showInputDialog("Enter Password");
    String user = "bacon123";
    String pass = "ilovebacon";	
    String url = "https://www.google.com/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1024&bih=677&q=bacon&oq=bacon&gs_l=img.3..0l10.4039.4699.0.6159.5.5.0.0.0.0.121.506.2j3.5.0.cpsugrpqhmsignedin%2Chmss2%3Dfalse...0.0..1.1.20.img.ilIvv5MwsRs";
     
    {try {
    	if (username.equals(user) && password.equals(pass))java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
    	else {JOptionPane.showMessageDialog(null, "Access Denied!", "Error", JOptionPane.ERROR_MESSAGE);}
    } 
     
    catch (IOException e) {
     
    }
    }
    }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Requesting Constructive Criticism About My Code

    You could have used code formatting, because that code is unreadable. No indentation, messed up line breaks, no Excepotion handling, if without braces. All logic in the main method. IMHO a main should never contain more than five LOC.

  3. The Following User Says Thank You to PhHein For This Useful Post:

    Wumbo (July 25th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Requesting Constructive Criticism About My Code

    Post your code between code tags.

    Empty catch blocks are worthless and a bad habit.

    To PhHein's point about the length and purpose of the main() method: If you've learned about the use of multiple classes and methods besides the main() method, you should be writing classes and methods with the thought that they might be reusable. Some possibilities here are a class that might be called LoginUser with methods getUserName(), getPassword(), validateUser(), and others as needed. The main() method contained in a separate driver or test class could be used to simply create an instance of LoginUser, possibly passing that instance to a method that tests/exercises every part of the LoginUser class.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Wumbo (July 25th, 2013)

  6. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Requesting Constructive Criticism About My Code

    Thank you! What do you mean by reusable?

  7. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Requesting Constructive Criticism About My Code

    Quote Originally Posted by Wumbo View Post
    Thank you! What do you mean by reusable?
    Rather than dumping all of the functionality into the main method, the main method should instruct instances to perform actions. For example a simple class used to store two numbers, and has methods to return values based on those numbers. A bank account for example. When you make a deposit, the main method should not say bankAccount.balance += 34;
    Rather it should look more like: bankAccount.deposit(34);
    With the second version, the deposit method is reusable, and keeps the state modification within the class, as it should be.
    Why? Consider another method: bankAccount.withdraw(99999999999);
    If the account does not have that amount of cash, the bank can reject the request, or charge a line of credit, or take other actions based on other factors...
    Rather if the main method just said bankAccount.balance -= 9999999999; and then spit out the cash at an atm, someone may be upset.
    Basically the idea is to encapsulate the functionality of a class within the class, and call on that functionality from outside the class rather than modifying the state of an instance from outside.

  8. The Following User Says Thank You to jps For This Useful Post:

    Wumbo (July 26th, 2013)

  9. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Requesting Constructive Criticism About My Code

    Write methods as general as possible. If you write a method called getUserName() so that it includes all possible cases of getting a person's user name with corresponding fault handling, then you should never have to write another method that gets a user's name. The next time you need to get the user's name, you can say, "I already wrote that," and you can REUSE the version you've already written. Maybe it'll need adjusting. Maybe you'll look at it and think, "What was I thinking when I wrote that?" Either way, you'll have a starting point. Eventually, you'll find that the starting point is an acceptable ending point.

  10. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Requesting Constructive Criticism About My Code

    Ooooh okay I get it now. Thank you very much for all the help!

Similar Threads

  1. Beginner Requesting Others To Critique My Code
    By mepis in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 30th, 2013, 07:41 AM
  2. JAVA criticism or inexperience?
    By reapaz1 in forum Java Theory & Questions
    Replies: 12
    Last Post: February 12th, 2013, 07:41 PM
  3. Replies: 4
    Last Post: May 12th, 2012, 09:53 AM
  4. No error, but I want criticism
    By JustForLawls in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 10th, 2011, 10:34 PM