title: is it better to test if a function is needed inside or outside of it?
body: <p>what is the best practice? <strong>call a function then return if you test for something, or test for something then call?</strong></p>
  
  <p>i prefer the test inside of function because it makes an easier viewing of what functions are called.</p>
  
  <p>for example:</p>
  
  <pre><code>protected void Application_BeginRequest(object sender, EventArgs e)
          {
              this.FixURLCosmetics();
          }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>private void FixURLCosmetics()
          {
              HttpContext context = HttpContext.Current;
              if (!context.Request.HttpMethod.ToString().Equals("GET", StringComparison.OrdinalIgnoreCase))
              {
                  // if not a GET method cancel url cosmetics
                  return;
              };
  
              string url = context.Request.RawUrl.ToString();
              bool doRedirect = false;
  
              // remove &gt; default.aspx
              if (url.EndsWith("/default.aspx", StringComparison.OrdinalIgnoreCase))
              {
                  url = url.Substring(0, url.Length - 12);
                  doRedirect = true;
              }
  
              // remove &gt; www
              if (url.Contains("//www"))
              {
                  url = url.Replace("//www", "//");
                  doRedirect = true;
              }
  
              // redirect if necessary
              if (doRedirect)
              {
                  context.Response.Redirect(url);
              }
          }
  </code></pre>
  
  <p>is this good:</p>
  
  <pre><code>if (!context.Request.HttpMethod.ToString().Equals("GET", StringComparison.OrdinalIgnoreCase))
              {
                  // if not a GET method cancel url cosmetics
                  return;
              };
  </code></pre>
  
  <p>or should that test be done in <code>Application_BeginRequest</code>?</p>
  
  <p>what is better?</p>
  
  <p><strong>thnx</strong></p>