Skip to main content
deleted 15 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I have a set of ifif/elseelse statements that trigger certain actions. As As a first step, I was thinking about moving the logic in each ifif statement to be its own function, but are there any recommendations on what design patterns that can help clean this code up?

I guess I am trying to see if there is a cleaner way to determine what the next action/command should be based on a set of criteria.

  I see people recommend the Command pattern, but how dodoes one abstract the logic in the ifif-statement to begin with?

The code is shown here:

I have a set of if/else statements that trigger certain actions. As a first step, I was thinking about moving the logic in each if statement to be its own function, but any recommendations on what design patterns that can help clean this code up?

I guess I am trying to see if there is a cleaner way to determine what the next action/command should be based on a set of criteria.

  I see people recommend the Command pattern, but how do abstract the logic in the if-statement to begin with?

The code is shown here:

I have a set of if/else statements that trigger certain actions. As a first step, I was thinking about moving the logic in each if statement to be its own function, but are there any recommendations on what design patterns can help clean this code up?

I guess I am trying to see if there is a cleaner way to determine what the next action/command should be based on a set of criteria. I see people recommend the Command pattern, but how does one abstract the logic in the if-statement to begin with?

edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

What pattern do I use to refactor Finding a lofway to obtain some volume of if/else statementswater using a large and small bucket

Tweeted twitter.com/#!/StackCodeReview/status/228900443227561986
Post Migrated Here from stackoverflow.com (revisions)
Source Link
Abe
  • 121
  • 3

What pattern do I use to refactor a lof of if/else statements

I have a set of if/else statements that trigger certain actions. As a first step, I was thinking about moving the logic in each if statement to be its own function, but any recommendations on what design patterns that can help clean this code up?

I guess I am trying to see if there is a cleaner way to determine what the next action/command should be based on a set of criteria.

I see people recommend the Command pattern, but how do abstract the logic in the if-statement to begin with?

The code is shown here:

function WaterRetriever(largeBucketSize, smallBucketSize, waterSize) {
    var buckets = {};

    if (waterSize > largeBucketSize) {
        throw new Error("The buckets are not large enough.");
    }

    buckets.firstContainer = new Bucket(largeBucketSize);
    buckets.secondContainer = new Bucket(smallBucketSize);

    this.getWaterFromLake = function () {
        var maxSteps = 25,
            step = 1;

        buckets.firstContainer.fill();

        while (step <= maxSteps && buckets.firstContainer.getCurrentAmount() !== waterSize) {
            if (buckets.firstContainer.isFull() && buckets.secondContainer.isEmpty()) {
                buckets.firstContainer.transferTo(buckets.secondContainer);

            } else if (buckets.secondContainer.isFull() && buckets.firstContainer.hasCapacityAvailable()) {
                buckets.secondContainer.empty();
                buckets.firstContainer.transferTo(buckets.secondContainer);
            } else if (buckets.firstContainer.isEmpty() && buckets.secondContainer.hasCapacityAvailable()) {
                buckets.firstContainer.fill();
            } else if (buckets.firstContainer.isFull() && buckets.secondContainer.hasCapacityAvailable()) {
                buckets.firstContainer.transferTo(buckets.secondContainer);
            }
            console.log(buckets.firstContainer.getCurrentAmount() + ", " + buckets.secondContainer.getCurrentAmount());
            step = step + 1;
        }

        if (step > maxSteps) {
            console.log("could not find a solution within the steps limit...");
        }
    };
}