1

Is there a more elegant way of assiging these properties? This if statement could get ugly by the time I finish all the various scenarios. I'm also trying to avoid case statements.

if(arrResponse.length>2){
    objResponse = {
        type : evl_ResponseType[arrResponse[0]].name,
        partition : evl_Partition_Status_Code[arrResponse[1]].description,
        icons : iconLED(bin(arrResponse[2]).result),
        numeric : arrResponse[3],
        beeps : BEEP_field[arrResponse[4]],
        msg  : arrResponse[5].replace('$','').trim()};
    } 
else {
    objResponse = {
        type : evl_ResponseType[arrResponse[0]].name;
    }
}
return objResponse;   
5
  • Create object by var objResponse = {}; Then assign properties by objResponse.probName = value; Commented Apr 19, 2015 at 17:43
  • The various evl_ objects are dictionaries, and arrResponse contains keys into that dictionary which are being used to look up the actual values you want to hang on to? Commented Apr 19, 2015 at 17:45
  • 2
    And when you say 'finish all the various scenarios'...can you give an example of what's left? That'll make it easier to find a general pattern. Commented Apr 19, 2015 at 17:47
  • How about starting with the basics, like shorter variable names. There's no generic syntax that could possibly fit your very specific scenario. If this is done in a number of places, you may be able to create a helper function. But if this is the only spot, that would be a waste. Commented Apr 19, 2015 at 17:49
  • Thanks, I will keep this in mind. I'm still learning. Commented Apr 29, 2015 at 2:11

2 Answers 2

3

Well, you could do this:

objResponse = {
    type : evl_ResponseType[arrResponse[0]].name;
};
if(arrResponse.length>2){
    objResponse.partition = evl_Partition_Status_Code[arrResponse[1]].description;
    objResponse.icons = iconLED(bin(arrResponse[2]).result);
    objResponse.numeric = arrResponse[3];
    objResponse.beeps = BEEP_field[arrResponse[4]];
    objResponse.msg  = arrResponse[5].replace('$','').trim();
} 
return objResponse;   

But I don't think I'd call it more elegant.

If you don't mind the properties existing even when they don't have useful values, you could do this:

objResponse = {
    type : evl_ResponseType[arrResponse[0]].name;
    partition : arrResponse.length>2 && evl_Partition_Status_Code[arrResponse[1]].description,
    icons : arrResponse.length>2 && iconLED(bin(arrResponse[2]).result),
    numeric : arrResponse.length>2 && arrResponse[3],
    beeps : arrResponse.length>2 && BEEP_field[arrResponse[4]],
    msg  : arrResponse.length>2 && arrResponse[5].replace('$','').trim()
};
return objResponse;

The properties will have the value false if the condition prefixing them isn't met.

That also has the advantage that you can tailor the conditions to the index you're using (e.g., using arrResponse.length > 5 when you're going to use arrResponse[5] for msg).


Side note: Your code wasn't assigning to partition if arrResponse.length was not >2, but the value you're assigning to partition is there even if arrResponse.length equals 2.

Sign up to request clarification or add additional context in comments.

2 Comments

Make be make sense to improve readibility by creating functions. For example function getPartition() { return arrResponse.length>2 && evl_Partition_Status_Code[arrResponse[1]].description; } and use it. Little bit more code to write, but then it will be simple to read and understand. I personally prefer to move large amount of code from IF statement to functions and then to use them. Such way I can understand logic without lookign in details. Anyway, upped the answer :)
Thanks, your response helped.
1

Why not just use a shorthand? It looks better while still keeping your current structure true.

objResponse = arrResponse.length > 2 ? {
    type: evl_ResponseType[arrResponse[0]].name,
    partition: evl_Partition_Status_Code[arrResponse[1]].description,
    icons: iconLED(bin(arrResponse[2]).result),
    numeric: arrResponse[3],
    beeps: BEEP_field[arrResponse[4]],
    msg: arrResponse[5].replace('$', '').trim()
} : {
    type: evl_ResponseType[arrResponse[0]].name;
}
return objResponse;

4 Comments

"Ugly" is a bit of a subjective thing to say. Wouldn't call it particularly unreadable either, and I'm not the greatest at reading code.
I personally don't find it ugly nor unreadable. But everyone has their opinions so ill let him have his.
Ternary operators are good for one line expressions like var c = test ? expression1 : expression2; I can't see any benefits of using it in this code. For me this is an bad idea to use it just because I can. Additionally, most of IDEs highlights IF ELSE keywords for readibility. If you look in large code, this is hard to see that code has IF statement.
all great comments. the reason I don't use short hand is because I'm trying to get the hang of things first. If I ever get to the pro level, then I will definitely shorten things up a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.