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

Thread: Is there any significant difference between these segments of code?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Is there any significant difference between these segments of code?

    Hello

    I am a very beginner in java programming and I am trying to learn on my one using the "objects first with java" book. So I would like to know if there are differences between my solution and the one the book gives. Actually the result is the same so mine is also correct but I wonder if one way is actually better to use than the other and if yes the reasons why.

    First:
     
    private ArrayList<String> files;
     
    /** takes a single integer and checks whether it is a valid index for the current
         * state of the collection. If the parameter is not valid, print an error message, otherwise print nothing.
         */
     public void checkIndex(int indexNumber)
        {
            if(indexNumber < 0 || indexNumber > files.size() - 1) {
                System.out.println("Error: index number not valid!");
            }
        }
     
     public void checkIndex(int checkIndex)
        {
            int isValid = checkIndex;
            if (!((0 < isValid) && (isValid < (files.size()-1))))
            {
                System.out.println("Error: index number not valid!");
            }
        }

    note. I post only the field and the method of my class...

    The first checkIndex method is mine and the second is the solution given. I want to ask first why to use a local variable when you can directly use the parameter for the test of the if statement, and secondly I know that a||b is equal to !(!a && !b) but I am baffled about the use of the latter by the book because it mentions all the time about trying to make our code as less complicated as possible and at the same time is using a more complicate expression.

    Are there reasons why the second method is better? I want to know so that in that case to adapt my way of thinking in order to write more accurate code...

    p.s. I have nobody to assist me and help me out with programming neither to give me a deeper explanation to the concepts I am going through the book and sometimes I am really struggling and feel frustrated so please excuse me for my ignorance...

    Thank you in advance


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Is there any significant difference between these segments of code?

    The second implementation looks horrible. I know nobody that would write something like that.
    The first implementation looks fine though. Although I would probably prefer:
    if(indexNumber < 0 || indexNumber >= files.size()) {
    Just to have the magic-number removed.
    Its not much, but I try to have as few literals in my code as possible because otherwise people might not know what they are used for.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Is there any significant difference between these segments of code?

    I see... thank you for your time...But how would that possibly work? I will have a problem because the index number of the last entry of my arraylist is always the size of my arraylist minus 1 because the first entry has index number 0.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Is there any significant difference between these segments of code?

    Agree with Cornix - the second implementation is not only overly complicate, as posted it is incorrect (presuming I am understanding what the variable 'files' represents).

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

    bihlas (June 19th, 2014)

  6. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Is there any significant difference between these segments of code?

    Quote Originally Posted by bihlas View Post
    I see... thank you for your time...But how would that possibly work? I will have a problem because the index number of the last entry of my arraylist is always the size of my arraylist minus 1 because the first entry has index number 0.
    These two statements are equivalent:
    a > size - 1
    a >= size
    They always return the same result.

  7. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    bihlas (June 19th, 2014), GregBrannon (June 19th, 2014)

  8. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Is there any significant difference between these segments of code?

    gg Off course you are right...I didnt notice that... like that it becomes more straightforward. Thank you very much, so I guess I am in the right way.
    So there isnt a real need in this case to declare a local variable and use it in the if statement... I thought so myself that is redundant to do so, but seeing the book solution I got confused...
    Thank you Cornix, I appreciate your time!

Similar Threads

  1. Replies: 3
    Last Post: April 18th, 2014, 12:28 PM
  2. Graphics Quality - Interconnecting Line Segments ???
    By russdb in forum AWT / Java Swing
    Replies: 0
    Last Post: June 25th, 2012, 07:50 AM
  3. What is the difference between the belows code
    By lulzimfazlija in forum Java Theory & Questions
    Replies: 4
    Last Post: January 21st, 2011, 02:15 PM