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: Refactoring if statement

  1. #1
    Junior Member jbraque's Avatar
    Join Date
    May 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Refactoring if statement

    Hello all,

    This is an example of pseudocode:

    if string = "" then
    do something
    if string case is all capital
    do something else
    if ...
    do something else
    ...

    Which design is better to implement in cases like above? Should i use a strategy pattern and put each condition inside each strategy?


  2. #2
    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: Refactoring if statement

    Even in pseudo code, if string.equals( something ), would be preferred, and '==' is the comparison operator, '=' the assignment operator. Using '=', '==', and equals() improperly will confuse the less aware.

    I'm not sure what measure one would use to determine the better approach, but what I hear most typically is to use the approach that results in the most readable and understandable source code and then trust in the compiler's optimizer to produce efficient byte code for the JVM.

  3. #3
    Junior Member jbraque's Avatar
    Join Date
    May 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refactoring if statement

    Well yes you are right about the pseudocode...
    Anyway i thought of putting all strategies in a collection and perform all of them as they will have the same interface.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Refactoring if statement

    I think you'll need to expand on your problem if you want a more desirable answer.
    At the moment, it looks like you're just trying to do a handful of if/else statements, but your assumption to use strategy patterns, collections, ect. makes me think you are trying to do something more complex. I'm just not sure what.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member jbraque's Avatar
    Join Date
    May 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refactoring if statement

    I have to catch multiple exceptions and find which exception was thrown without using a if...else block. The exceptions are written by me and their number so far is 10 and may expand in the future.

    an example:

    String str = ...
    if (str.equals("")) { throw new EmptyStringException}
    if (Character.isUpperCase(str)) { throw new WrongCaseException}

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Refactoring if statement

    To catch exceptions, you should use a try/catch block. Have you heard of those?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member jbraque's Avatar
    Join Date
    May 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refactoring if statement

    Ok never mind, thanks

Similar Threads

  1. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  4. if....else statement
    By Appu14 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 2nd, 2012, 06:35 AM
  5. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 08:25 AM