Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Right now, what you have is clear and legible. If you're trying to get there with less processing time, I don't think you're going to make it. If you're trying to get there with fewer lines of code, you can, but it's going to be ugly. </p> <p>One easy way to shorten it on-screen (same LOC count, -1) is to remove some of your curly braces and not store <code>visitorBrowser</code>:</p> <pre><code>public static string GetUa(HttpRequest hr) { try { string originalBrowser = hr.ServerVariables["X-OperaMini-Phone-UA"]; string anotherOriginalBrowser = hr.ServerVariables["X-Device-User-Agent"]; //novarra if (!String.IsNullOrEmpty(originalBrowser)) return "OPERAMINI " + originalBrowser; else if (!String.IsNullOrEmpty(anotherOriginalBrowser)) return "NOVARRA " + anotherOriginalBrowser; else return hr.UserAgent.ToString(); } catch { return "No UA Found"; } } </code></pre> <p>To me, this is slightly less readable, but probably still livable.</p> <p>Now you can make it <strong>really</strong> short by using the conditional operator (<code>?:</code>), but it's going to also be <strong>really</strong> nasty for readability. If I saw code like the following in a code review, I'd make the developer rewrite it for clarity:</p> <pre><code>public static string GetUa(HttpRequest hr) { try { string visitorBrowser = hr.UserAgent.ToString(); string originalBrowser = hr.ServerVariables["X-OperaMini-Phone-UA"]; string anotherOriginalBrowser = hr.ServerVariables["X-Device-User-Agent"]; //novarra return !(string.IsNullOrEmpty(originalBrowser)) ? "OPERAMINI " + originalBrowser : !(string.IsNullOrEmpty(anotherOriginalBrowser)) ? "NOVARRA " + anotherOriginalBrowser : visitorBrowser); } catch { return "No UA Found"; } } </code></pre> <p>Seriously, <strong>please</strong> don't do the second example. (I'm not 100% sure that will compile; I'm writing it off the top of my head on my Mac at the moment. But I'm 99.9% sure it will, and will work, and the next developer will HATE you for it.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload