0

I have my code and I would like to optimize it. For me it looks like it's already optimized. Can anyone suggest how I could make it a bit more optimized?

if (target == "power")
{
    return new JsonResult { Data = new { RC = new Data.AdminPower(datastoreValue).Refresh() } };
} 
if (target == "notes")
{
    return new JsonResult { Data = new { RC = new Data.AdminNotes(datastoreValue).Refresh() } };
}
if (target == "book")
{
    return new JsonResult { Data = new { RC = new Data.AdminBook(datastoreValue).Refresh() } };
}
return null;
3
  • 2
    Since it already looks good enough to you, you can just move on in most cases. Commented Jul 28, 2011 at 0:01
  • 1
    Surely any performance you gain here would be fairly trivial? But if you really want to change something, I would agree with using switch. Commented Jul 28, 2011 at 0:04
  • Can you turn your target into an enumeration? Then you could have Target.Book, Target.Notes, etc. Then your comparing enumerated values, not literal strings. However, I don't see where target is being assigned, so that may not be an option. Commented Jul 28, 2011 at 0:13

5 Answers 5

2

switch statements are great for these situations:

switch(target)
{
case "power":
  return new JsonResult { Data = new { RC = new Data.AdminPower(datastoreValue).Refresh() } };
case "notes":
  return new JsonResult { Data = new { RC = new Data.AdminNotes(datastoreValue).Refresh() } };
case "book":
  return new JsonResult { Data = new { RC = new Data.AdminBook(datastoreValue).Refresh() } };
default:
  return null;
}

Dont really need the breaks tho, since it will be returning each time, but its good practice..

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

4 Comments

@Nick You're going to get unreachable code warnings for the breaks;
I don't think the breaks are necessary.
I said the breaks weren't necessary, just said its good practice imo. OP can delete if she wants
2

If you know that your method will be called more often with "book" values, then you should put that first. Basically sort by order of frequency.

Comments

0

As someone else mentioned, you should put them in order of most likely to occur to minimize your total number of incorrect comparisons.

Maybe it's possible to change "power" "notes" and "book" to an enum or int representation, and that may be slightly faster than the string comparisons.

There's not a whole lot you can do that will result in any significant optimizations, though.

Comments

0

I don't know what's your idea about optimized, as a matter of speed, just put some 'else' before second and third 'if's but if you mean less code lines, then may be something like this could help you:

return 
((target == "power") ? new JsonResult { Data = new { RC = new Data.AdminPower(datastoreValue).Refresh() } } :
((target == "notes") ? new JsonResult { Data = new { RC = new Data.AdminNotes(datastoreValue).Refresh() } } : 
((target == "book") ? new JsonResult { Data = new { RC = new Data.AdminBook(datastoreValue).Refresh() } } : null))))

2 Comments

My eyes would roll up into my head if I saw that line in code I had to maintain, and I'd probably mutter some very unnice things about the programmer's origin. Optimization is fine, but not to the point where readability goes out the window....
While you're at it, remove all of the spaces and line breaks :). Nothing like reading a multiline ternary statement to wake you up in the morning.
0

The only thing I would do is change the ifs to an if-elseif chain. There isn't really anything else you can do to improve performance.

2 Comments

The OP returns in the if-block, so else if won't help.
@CodeNaked: I didn't say it would help. It is better communicated to anyone reading the code that the conditions are mutually exclusive.

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.