HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to simplify my else if statement

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
statementsimplifyelsehow

Problem

Alright so this isn't an issue with my program not working, it's more of a fact that I wanna learn to code better.

This is one of 6 if else statements that I use inside of my program. What I'm trying to do is make it shorter (easier) to do. I am learning AS3 with Flex 4.6 (started with Flex 4).

If I left something out that is important let me know I'll get it for you. All the .text's can be changed at any time the user wants.

Thanks In advance for any help!

```
if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "10"){
bap.text = String(Number(1147))
tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
}
else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "11"){
bap.text = String(Number(1217))
tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
}
else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "12"){
bap.text = String(Number(1297))
tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
}
else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "13"){
bap.text = String(Number(1343))
tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
}
else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "14"){
bap.text = String(Number(1407))
tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
}
else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "15"){
bap.text = String(Number(1467))
tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
}
else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "16"){
bap.text =

Solution

Since you want to learn to code properly, my advice would be to create a model for your application (read up on the model-view-controller pattern if you haven't already). I'll try to get you started with this example.

For starters, your variable names don't speak for themselves, which leaves me (or any other developer) guessing as to what their value is. I'll guess a bit and I seem to see a combination of 3 properties that make up one unique ID. We'll create a model class for that item; let's call it ShopItem.

[Bindable]
public class ShopItem {

    public var id:int;
    public var material:Material;
    public var type:Type;
    public var mysteriousId:int;

}


As you see it has an ID property, plus the three other properties that make up a unique combination. The last property is just an int as I have no more information about it than that, but it could be a class just as the others. Also, I have made the class Bindable, so we can use it for data binding later on. Now lets create the other two classes.

[Bindable]
public class Material {

    public var id:int;
    public var label:String;

}

[Bindable]
public class Type {

    public var id:int;
    public var label:String;

}


We give these an ID and a label. This has two main advantages:

  • if you want to change the labels, or translate your application, the logic doesn't break (as it would with your code)



  • you can now use this class to store this data in a database



Now we can create a collection of ShopItems:

var items:IList = new ArrayCollection();

    var material:Material = new Material();
    material.id = 123;
    material.label = 'Woven Acrylic';

    var type:Type = new Type();
    type.id = 456;
    type.label = 'Motorized Pro';

    var item:ShopItem = new ShopItem();
    item.id = 1;
    item.material = material;
    item.type = type;
    item.mysteriousId = 789;

    items.addItem(item);


We can now create the following method to replace your code:

private function findShopItem(material:Material, type:Type, mysteriousId:int):ShopItem {
    for each (var item:ShopItem in items) {
        if (item.material == material &&
            item.type == type &&
            item.mysteriousId == mysteriousId)
                return item;
    }

    return null;
}


Edit: I understand that this is the most difficult answer for you to get your head around, because it has a certain level of abstraction you're not accustomed to yet. It is also just meant to confront you with the concept of a data model and get you started. I think from here on you can do a few things:

  • A data model and data binding between the view and the model is at the core of every Flex application. You should be able to find some decent tutorials on this concept.



  • Or just read an entry-level book on Flex development; the first chapters usually cover data binding.



  • Take a step back and think about your application only in terms of what it's supposed to do; then functionally design the views you will need to accomplish that functionality; create a data model; create the views (usually pure MXML); try to bind the data model to your views; feel free to ask questions as you go either here on StackOverflow

Code Snippets

[Bindable]
public class ShopItem {

    public var id:int;
    public var material:Material;
    public var type:Type;
    public var mysteriousId:int;

}
[Bindable]
public class Material {

    public var id:int;
    public var label:String;

}

[Bindable]
public class Type {

    public var id:int;
    public var label:String;

}
var items:IList = new ArrayCollection();

    var material:Material = new Material();
    material.id = 123;
    material.label = 'Woven Acrylic';

    var type:Type = new Type();
    type.id = 456;
    type.label = 'Motorized Pro';

    var item:ShopItem = new ShopItem();
    item.id = 1;
    item.material = material;
    item.type = type;
    item.mysteriousId = 789;

    items.addItem(item);
private function findShopItem(material:Material, type:Type, mysteriousId:int):ShopItem {
    for each (var item:ShopItem in items) {
        if (item.material == material &&
            item.type == type &&
            item.mysteriousId == mysteriousId)
                return item;
    }

    return null;
}

Context

StackExchange Code Review Q#12022, answer score: 6

Revisions (0)

No revisions yet.