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

Thread: Is this bad code design?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is this bad code design?

    This method checks to see if the proposed move can be made.
    My lab says "You are not expected to catch the exception, just be sure it is thrown in the appropriate place. "

    But for my final assignment we have to turn it into a try catch statement. My issue with the code is that there is 2 separate if else statements.
    This code
            // no errors, proceed with move
            GamePiece locationFrom = board[fromX][fromY];
            GamePiece locationTo = board[toX][toY];

    Can't be moved on top because you get a runtime error when you set fromX to -445 or something because that position doesn't exist. When really it should be doing my throw statement.




        /**
         * move gamepiece method
         */
        public void movePiece(Location from, Location to) throws InvalidMoveException
        {
            int fromX = from.getXPosition();
            int fromY = from.getYPosition();
            int toX = to.getXPosition();
            int toY = to.getYPosition(); 
     
            if((fromX < 0 || fromX >= ROW) || (fromY < 0 || fromY >= ROW)){
                throw new InvalidMoveException("Location FROM position is out of bounds");
            }
            else if((toX < 0 || toX >= COLUMN) || (toY < 0 || toY >= COLUMN)){
                throw new InvalidMoveException("Location TO position is out of bounds");
            }
     
            // no errors, proceed with move
            GamePiece locationFrom = board[fromX][fromY];
            GamePiece locationTo = board[toX][toY];
     
            if(board[fromX][fromY] == null){
                throw new InvalidMoveException("You cant move a piece that isn't there");
            }
            else if(!locationFrom.isLegalMove(from, to)){
                throw new InvalidMoveException("Not a valid move: You can only move pieces up");
            }
     
            board[toX][toY] = locationFrom;
            board[fromX][fromY] = null;
        }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Is this bad code design?

    What is your question?
    No, it's not a bad design.

Similar Threads

  1. star design programs
    By prasansani in forum Java Theory & Questions
    Replies: 6
    Last Post: September 20th, 2011, 12:42 PM
  2. Help in possible Design UI
    By beni.kurniawan in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 10:51 AM
  3. Class design
    By arithmetics in forum Object Oriented Programming
    Replies: 4
    Last Post: November 4th, 2010, 08:44 AM
  4. Replies: 2
    Last Post: April 12th, 2010, 11:13 AM