Go to home page mail me! RSS Feed
FoxMetrics Web Analytics

DateTime.Now vs DateTime.UtcNow

Try to use .UtcNow() as much as you can over .Now() as it is inherently faster than the later. The problem seems to stem from the fact that DateTime.Now performs a DateTime.UtcNow first and then performs a very expensive called to figure out daylight savings time and time zone information.

public static DateTime Now
{
    get
    {
        DateTime utcNow = DateTime.UtcNow;
        bool isAmbiguousDst = false;
        long ticks = TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(utcNow, 
out isAmbiguousDst).Ticks;
        long num = utcNow.Ticks + ticks;
        if (num > 3155378975999999999L)
        {
            return new DateTime(3155378975999999999L, DateTimeKind.Local);
        }
        if (num < 0L)
        {
            return new DateTime(0L, DateTimeKind.Local);
        }
        return new DateTime(num, DateTimeKind.Local, isAmbiguousDst);
    }
}
 
public static DateTime UtcNow
{
    [TargetedPatchingOptOut("Performance critical to 
inline across NGen image boundaries"), SecuritySafeCritical]
    get
    {
        long systemTimeAsFileTime = DateTime.GetSystemTimeAsFileTime();
        return new DateTime((ulong)(systemTimeAsFileTime + 
504911232000000000L | 4611686018427387904L));
    }
}

Thanks to Keyvan Nayyeri who did a detail analysis of DateTime.Now, DateTime.UtcNow and Stopwatch class at his blog post called The Darkness Behind DateTime.Now. Very good read for detail explanation.

Quantitative comparison between DateTime.Now, DateTime.UtcNow, and StopWatch in .NET

 

Note that Environment.TickCount is also faster than DateTime.UtcNow.Ticks.

Framework layers on layers

As an architect, I stress to my team, colleagues or anyone that wants to talk IT that a solid developer needs to understand the fundamentals of how things work – especially when dealing with such a large framework such as the .Net Framework.

I’m usually very slow when it comes to adopting new versions of the framework, as of now I still write code in 2.0/3.5 while 4.5 is about to start collecting dust. It has become clear to me in the past years that sometimes frameworks simply get bloated. I wrote comfortable powerful and sophisticated applications with .NET 2.0 but when 4.0 came out everyone claimed it and jumped, yeah - I started writing code in assembly, therefore, I believe in less layers which is what newer framework versions are usually made up of.

If a framework does not provide with critical patches and newer features, then there is no need for me to upgrade. simply converting 6 lines of code to 2 lines by wrapping it is just not enough.

HandleError – ASP.NET Mvc Attribute

I’m not a fan of hiding error messages, as it could bite you real hard. However, there are existing functionalities that just need to be discussed such as the HandleError attribute of the ASP.NET MVC application. If you are not a fan of displaying the very common Yellow Screen of Death, then HandleError should be your new friend.

By default, I take certain steps when I’m setting up a new MVC app to prevent my users from ever seeing a single error page by implementing NLog, creating custom error pages and so on and so forth.

HandleError is an action filter and MVC has several others such as Authorize, OutputCache, RequireHttps, etc.. It simply provides a way to map exceptions to a specific template, therefore, enabling you to display your custom error pages when an exception occurs or a simple generic error view.

Once an error is detected the filter first checks the controller specific folder for an error file, i.e. if you are in Home, it will check Views/Home/Error.aspx and if not found, it will default to the shared views folder at Views/Shared/Error.aspx to locate the file.

 

    [HandleError]
    public class ListController : Controller
    {
        //
        // GET: /List/
        public ActionResult Index()
        {
            return View();
        }
    }

I usually extend the System.Web.Mvc.Controller class and override its OnException method, which provides me with a single entry point to capture and log all errors as I see fit.

        protected override void OnException(ExceptionContext filterContext)
        {
            // TODO: NLog exception here. base.OnException(filterContext);
        }

Note that overriding OnException prevents HandleError from taking control, therefore, it becomes completely useless, its almost like you can’t use both, its either clean view or log error but I want and need both.

Here is my final OnException override that gives me both clean messages to my users and also logs using NLog to disk and database for my review.

        protected override void OnException(ExceptionContext filterContext)
        {
            // TODO: NLog exception here.

            // Handle exception
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                filterContext.ExceptionHandled = true;
                Response.StatusCode = 500;
                Response.TrySkipIisCustomErrors = true;
                this.View("Error").ExecuteResult(this.ControllerContext);

                //
                base.OnException(filterContext);
            }
        }

Page size offender – JavaScript

We all love JavaScript and yes its my favorite language thingy! However, because it is loosely coupled, you can easily write very bad bloated code and there aren’t any sweet tools out there to help with this. It is only logical to determine why the existing JavaScript IDE’s are not getting powerful – because its not a language that compiles, therefore, it is very difficult to analyze compared to other languages such as C# & F#.

Its imperative that you remove unused code, write efficient code – check out the work of John Resig, he founder of the JQuery framework.

Pingdom surveyed the top 1,000 sites and the results are astonishing, images have always been a problem, however, JavaScript is the new offender as of 2011. So no excuses clean up your code and compress them and better yet place them on a CDN for better delivery. Here are some nice charts.

Web page size change in one year

 

Web page content size change in percent

My top 10 favorite domain generators

We all know how painful it is when you conceive an idea and you want to start building the foundation for it and you get stuck hunting down a domain that makes sense for several hours. It has been said multiple times that the domain really doesn’t matter but I still think it does and the simpler and easier it is to remember the better it is. It is usually the most important or most complicated step to complete.

Here are the domain generators that I’ll recommend, they will simplify your task of choosing a domain name or names. If GoDaddy had one built in – wouldn’t that make all our lives easier?

There are a billion of these – I’m just listing a few to get you on your way.

  1. http://www.bustaname.com/ – my personal favorite, they suggest, validate and check availability. The domain maker tool is pretty powerful.
  2. http://domai.nr/ – pretty sweet and clean interface and insanely fast, lacks any options or configuration
  3. http://www.dotomator.com/ – Availability is not on screen, the send you to a domain provide which sucks as I have to keep bouncing back and forth. Word combination and suggestion tool is pretty handy and they have an iPhone app.
  4. https://domize.com/ – another nice and clean home page, you can’t possibly get lost. Suggestions are not available it only checks different length of the same word.
  5. http://www.domainsbot.com/ – Super fast and provides lots of juicy data for premium domains and etc..
  6. http://www.stuckdomains.com/ – Helps you hunt down expired domains, you never know you might get lucky
  7. http://ajaxwhois.com/ – Fast since its Ajax driving, however, its missing lost of features.
  8. http://namethingy.com/ – does all the guess work for you, pretty intuitive, just relax and click words that interest you as you see them appear
  9. http://impossibility.org/ –use this very frequently as you can simply type in a word and it will suggest several availability readable domains
  10. http://blungr.com/ – new at this time but it works great and also suggest none .com domains if you are looking for something like list.co

Webcast: The basics of SQL server high availability and disaster recovery – Clustering, Log Shipping, Replication and More

You got to love Brent Ozar, his posts are usually if not always valuable and informative. He is a SQL Server performance tuning guru and I read his blog religiously as it has some juicy stuff.

His most recent post talks about the pros and cons of several methods in which you can make SQL server more reliable which usually leads to happy days.

Here is a  30 minutes video that is great for production DBAs how haven’t implemented clustering, replication or log shipping before and want to get started quickly.

CSSCop

Just fresh out of the box today, the famous VS gallery now has a CSSCop by Mads – got it? the counterpart to FXCop but this time for your style sheets.

CSSCop makes it easy to adhere to best practices for writing stylesheets. It catches common errors that affect browser compatibility and much more. You can think of it as FxCop for stylesheets.

 

Take it for a spin here

RSA was hacked!

Who can we trust if the systems that are suppose to be really secure are not really secure, an email, really? In RSA? You’ll think email attachments would be locked down. Why in the world does Excel load/execute flash object – I’m all for Microsoft but how they make sure critical mistakes is sometimes hard to comprehend.

read more…

read what RSA had to say

Google+ Song

Microsoft® SQL Server® code name 'Denali', Community Technology Preview 3 (CTP 3) Product Guide

Microsoft has release a long list of product guides for project Denali – videos, technical docs, demos and more.

Download the Product Guide

Next entries »