How to refactor deeply nested functions?












0















My code currently looks like this:



If (case = 1)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 2)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 3)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}


Obviously terrible and difficult to maintain. I want to add yet another super If statement on top of this, which splits this tree 6 times. Each ;do something does one line of code.



Any help on how to refactor this monstrosity? I've written a lot of code that looks like this and I dread working with it every time. I need a new way to approach these problems. Extending the functionality of this code requires a lot of work.










share|improve this question























  • What is the programming paradigm to consider? Stuctural or OO?

    – Jul10
    Dec 18 '18 at 17:23











  • Object-oriented.

    – GeekGurl
    Dec 19 '18 at 14:33
















0















My code currently looks like this:



If (case = 1)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 2)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 3)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}


Obviously terrible and difficult to maintain. I want to add yet another super If statement on top of this, which splits this tree 6 times. Each ;do something does one line of code.



Any help on how to refactor this monstrosity? I've written a lot of code that looks like this and I dread working with it every time. I need a new way to approach these problems. Extending the functionality of this code requires a lot of work.










share|improve this question























  • What is the programming paradigm to consider? Stuctural or OO?

    – Jul10
    Dec 18 '18 at 17:23











  • Object-oriented.

    – GeekGurl
    Dec 19 '18 at 14:33














0












0








0








My code currently looks like this:



If (case = 1)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 2)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 3)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}


Obviously terrible and difficult to maintain. I want to add yet another super If statement on top of this, which splits this tree 6 times. Each ;do something does one line of code.



Any help on how to refactor this monstrosity? I've written a lot of code that looks like this and I dread working with it every time. I need a new way to approach these problems. Extending the functionality of this code requires a lot of work.










share|improve this question














My code currently looks like this:



If (case = 1)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 2)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}

else if (case = 3)
{
If (option = 1)
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
else
{
If (otherOption = 1)
;do something
else if (otherOption = 2)
;do something
else if (otherOption = 3)
;do something
}
}


Obviously terrible and difficult to maintain. I want to add yet another super If statement on top of this, which splits this tree 6 times. Each ;do something does one line of code.



Any help on how to refactor this monstrosity? I've written a lot of code that looks like this and I dread working with it every time. I need a new way to approach these problems. Extending the functionality of this code requires a lot of work.







if-statement nested refactoring statements






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 18 '18 at 7:10









GeekGurlGeekGurl

1




1













  • What is the programming paradigm to consider? Stuctural or OO?

    – Jul10
    Dec 18 '18 at 17:23











  • Object-oriented.

    – GeekGurl
    Dec 19 '18 at 14:33



















  • What is the programming paradigm to consider? Stuctural or OO?

    – Jul10
    Dec 18 '18 at 17:23











  • Object-oriented.

    – GeekGurl
    Dec 19 '18 at 14:33

















What is the programming paradigm to consider? Stuctural or OO?

– Jul10
Dec 18 '18 at 17:23





What is the programming paradigm to consider? Stuctural or OO?

– Jul10
Dec 18 '18 at 17:23













Object-oriented.

– GeekGurl
Dec 19 '18 at 14:33





Object-oriented.

– GeekGurl
Dec 19 '18 at 14:33












1 Answer
1






active

oldest

votes


















1














Even if you didn't specify the context of your application, it seems a scenario that could take advantage from the application of Strategy Pattern.



The code you posted can be settled to act as the Context object with the responsibility of control the higher level condition of your outer if-else statement.

Within every block of if-else statement, a particular behaviour can be loaded using a a ConcreteStrategy object (that usually implements a Strategy interface).



This pattern can be repeated for inner if-else blocks.



Example



Consider a game in which a Character moves around a 2D-map. Depending of its life indicator (e.g 0-200), it can moves of 2,5,8 tiles if he is,respectively,in a tired, ok or a super state.



The Character object acts like the "Context of the Strategy Pattern and he has the responsability of which MoveStrategy is correct for the situation.
Putting of life the correct MoveStrategy object and calling by forwarding the method move() throght the move() method of Character.



A hint for a Java implementation of the example:



public class Character{

private int life=100;
private int x=0
private int y=0;
private MoveStrategy strategy=new DefaultStrategy();


public int getLife(){
return life;
}

public void setLife(int value){
this.life=value;
}

public void move(){
if(life<30)
strategy=new TiredStrategy();
else if(life > 100)
strategy=new SuperStrategy();
else
strategy=new DefaultStrategy();


strategy.move();

}
}



public interface MoveStrategy{

public abstract void move();

}

public DefaultStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 5 tiles");
}
}

public TiredStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 2 tiles");
}
}

public SuperStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 8 tiles");
}
}





share|improve this answer


























  • Incredibly helpful answer. This is much better and makes it easier to extend functionality. And easier to understand. Thank you so much.

    – GeekGurl
    Dec 20 '18 at 6:40













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53827955%2fhow-to-refactor-deeply-nested-functions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Even if you didn't specify the context of your application, it seems a scenario that could take advantage from the application of Strategy Pattern.



The code you posted can be settled to act as the Context object with the responsibility of control the higher level condition of your outer if-else statement.

Within every block of if-else statement, a particular behaviour can be loaded using a a ConcreteStrategy object (that usually implements a Strategy interface).



This pattern can be repeated for inner if-else blocks.



Example



Consider a game in which a Character moves around a 2D-map. Depending of its life indicator (e.g 0-200), it can moves of 2,5,8 tiles if he is,respectively,in a tired, ok or a super state.



The Character object acts like the "Context of the Strategy Pattern and he has the responsability of which MoveStrategy is correct for the situation.
Putting of life the correct MoveStrategy object and calling by forwarding the method move() throght the move() method of Character.



A hint for a Java implementation of the example:



public class Character{

private int life=100;
private int x=0
private int y=0;
private MoveStrategy strategy=new DefaultStrategy();


public int getLife(){
return life;
}

public void setLife(int value){
this.life=value;
}

public void move(){
if(life<30)
strategy=new TiredStrategy();
else if(life > 100)
strategy=new SuperStrategy();
else
strategy=new DefaultStrategy();


strategy.move();

}
}



public interface MoveStrategy{

public abstract void move();

}

public DefaultStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 5 tiles");
}
}

public TiredStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 2 tiles");
}
}

public SuperStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 8 tiles");
}
}





share|improve this answer


























  • Incredibly helpful answer. This is much better and makes it easier to extend functionality. And easier to understand. Thank you so much.

    – GeekGurl
    Dec 20 '18 at 6:40


















1














Even if you didn't specify the context of your application, it seems a scenario that could take advantage from the application of Strategy Pattern.



The code you posted can be settled to act as the Context object with the responsibility of control the higher level condition of your outer if-else statement.

Within every block of if-else statement, a particular behaviour can be loaded using a a ConcreteStrategy object (that usually implements a Strategy interface).



This pattern can be repeated for inner if-else blocks.



Example



Consider a game in which a Character moves around a 2D-map. Depending of its life indicator (e.g 0-200), it can moves of 2,5,8 tiles if he is,respectively,in a tired, ok or a super state.



The Character object acts like the "Context of the Strategy Pattern and he has the responsability of which MoveStrategy is correct for the situation.
Putting of life the correct MoveStrategy object and calling by forwarding the method move() throght the move() method of Character.



A hint for a Java implementation of the example:



public class Character{

private int life=100;
private int x=0
private int y=0;
private MoveStrategy strategy=new DefaultStrategy();


public int getLife(){
return life;
}

public void setLife(int value){
this.life=value;
}

public void move(){
if(life<30)
strategy=new TiredStrategy();
else if(life > 100)
strategy=new SuperStrategy();
else
strategy=new DefaultStrategy();


strategy.move();

}
}



public interface MoveStrategy{

public abstract void move();

}

public DefaultStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 5 tiles");
}
}

public TiredStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 2 tiles");
}
}

public SuperStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 8 tiles");
}
}





share|improve this answer


























  • Incredibly helpful answer. This is much better and makes it easier to extend functionality. And easier to understand. Thank you so much.

    – GeekGurl
    Dec 20 '18 at 6:40
















1












1








1







Even if you didn't specify the context of your application, it seems a scenario that could take advantage from the application of Strategy Pattern.



The code you posted can be settled to act as the Context object with the responsibility of control the higher level condition of your outer if-else statement.

Within every block of if-else statement, a particular behaviour can be loaded using a a ConcreteStrategy object (that usually implements a Strategy interface).



This pattern can be repeated for inner if-else blocks.



Example



Consider a game in which a Character moves around a 2D-map. Depending of its life indicator (e.g 0-200), it can moves of 2,5,8 tiles if he is,respectively,in a tired, ok or a super state.



The Character object acts like the "Context of the Strategy Pattern and he has the responsability of which MoveStrategy is correct for the situation.
Putting of life the correct MoveStrategy object and calling by forwarding the method move() throght the move() method of Character.



A hint for a Java implementation of the example:



public class Character{

private int life=100;
private int x=0
private int y=0;
private MoveStrategy strategy=new DefaultStrategy();


public int getLife(){
return life;
}

public void setLife(int value){
this.life=value;
}

public void move(){
if(life<30)
strategy=new TiredStrategy();
else if(life > 100)
strategy=new SuperStrategy();
else
strategy=new DefaultStrategy();


strategy.move();

}
}



public interface MoveStrategy{

public abstract void move();

}

public DefaultStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 5 tiles");
}
}

public TiredStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 2 tiles");
}
}

public SuperStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 8 tiles");
}
}





share|improve this answer















Even if you didn't specify the context of your application, it seems a scenario that could take advantage from the application of Strategy Pattern.



The code you posted can be settled to act as the Context object with the responsibility of control the higher level condition of your outer if-else statement.

Within every block of if-else statement, a particular behaviour can be loaded using a a ConcreteStrategy object (that usually implements a Strategy interface).



This pattern can be repeated for inner if-else blocks.



Example



Consider a game in which a Character moves around a 2D-map. Depending of its life indicator (e.g 0-200), it can moves of 2,5,8 tiles if he is,respectively,in a tired, ok or a super state.



The Character object acts like the "Context of the Strategy Pattern and he has the responsability of which MoveStrategy is correct for the situation.
Putting of life the correct MoveStrategy object and calling by forwarding the method move() throght the move() method of Character.



A hint for a Java implementation of the example:



public class Character{

private int life=100;
private int x=0
private int y=0;
private MoveStrategy strategy=new DefaultStrategy();


public int getLife(){
return life;
}

public void setLife(int value){
this.life=value;
}

public void move(){
if(life<30)
strategy=new TiredStrategy();
else if(life > 100)
strategy=new SuperStrategy();
else
strategy=new DefaultStrategy();


strategy.move();

}
}



public interface MoveStrategy{

public abstract void move();

}

public DefaultStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 5 tiles");
}
}

public TiredStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 2 tiles");
}
}

public SuperStrategy implements MoveStrategy{

public void move(){
System.out.println("Move character of 8 tiles");
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 10:55

























answered Dec 19 '18 at 15:27









Jul10Jul10

404515




404515













  • Incredibly helpful answer. This is much better and makes it easier to extend functionality. And easier to understand. Thank you so much.

    – GeekGurl
    Dec 20 '18 at 6:40





















  • Incredibly helpful answer. This is much better and makes it easier to extend functionality. And easier to understand. Thank you so much.

    – GeekGurl
    Dec 20 '18 at 6:40



















Incredibly helpful answer. This is much better and makes it easier to extend functionality. And easier to understand. Thank you so much.

– GeekGurl
Dec 20 '18 at 6:40







Incredibly helpful answer. This is much better and makes it easier to extend functionality. And easier to understand. Thank you so much.

– GeekGurl
Dec 20 '18 at 6:40






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53827955%2fhow-to-refactor-deeply-nested-functions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory