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 3 of 3

Thread: redundant code

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

    Default redundant code

    I was marked off 2 points from my teacher because he said this part of my code was redundant. What could I have done to make it less redundant?
    if (x < 30) {
         a=2;
         x++;
         System.out.println("Java is awesome! " + x);
    } else {
         a = 2;
         System.out.println("Java is awesome! " + x);
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,795
    My Mood
    Mellow
    Thanks
    175
    Thanked 626 Times in 613 Posts

    Default Re: redundant code

    Anytime you repeat lines of code, there's a possibility that there is redundancy (or you need to move repeated code into a method). You didn't give us much context, but I believe what you have is the same as:
            a = 2;
            if (x < 30)
            {
                x++;
            }
            System.out.println("Java is awesome! " + x);

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: redundant code

    A definite is that the println is the same whether it goes into the if or the else so it can be placed outside either.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 10:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 09:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 09:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM