Click here to Skip to main content
15,881,455 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Isomorphic Git Pin
Super Lloyd21-May-18 6:16
Super Lloyd21-May-18 6:16 
GeneralRe: Isomorphic Git Pin
Richard Deeming21-May-18 7:23
mveRichard Deeming21-May-18 7:23 
GeneralRe: Isomorphic Git Pin
Super Lloyd21-May-18 7:47
Super Lloyd21-May-18 7:47 
GeneralIs it my fault? Pin
PIEBALDconsult14-May-18 14:31
mvePIEBALDconsult14-May-18 14:31 
GeneralRe: Is it my fault? Pin
Super Lloyd14-May-18 16:51
Super Lloyd14-May-18 16:51 
GeneralRe: Is it my fault? Pin
Jon McKee14-May-18 18:17
professionalJon McKee14-May-18 18:17 
GeneralRe: Is it my fault? Pin
Eddy Vluggen15-May-18 1:01
professionalEddy Vluggen15-May-18 1:01 
GeneralRe: Is it my fault? Pin
Jon McKee15-May-18 8:36
professionalJon McKee15-May-18 8:36 
Of course you shouldn't avoid using them when appropriate. Exceptions are for exceptional circumstances. Collection classes implement search/find methods because an item not being in the collection is not exceptional. Since searching or indexing into the collection has to occur anyways, the method saves time by returning a simple boolean rather than invoking the exception stack.

As you point out in your link though, if this is front-end code the user probably wouldn't notice even if they could generate 10,000 exceptions back-to-back. In that scenario it comes down to what you define as best practices.

With debugger: 42.4388 ms (contains) vs 69,055.0051 ms (exceptions)
Without debugger: 43.6290 ms (contains) vs 563.4023 ms (exceptions)
C#
static void Main(string[] args)
{
    int maxCount = 10000;
    Random rng = new Random();

    List<int> testList = new List<int>();
    for (int i = 0; i < maxCount; i++)
        testList.Add(i);

    TimeSpan containsTest = TestCode(() =>
    {
        for (int i = 0; i < maxCount; i++)
            testList.Contains(rng.Next(0, maxCount));
    });

    TimeSpan exceptionTest = TestCode(() =>
    {
        for (int i = 0; i < maxCount; i++)
        {
            try
            {
                rng.Next(0, maxCount);
                throw new Exception();
            }
            catch (Exception)
            { }
        }
    });

    Console.WriteLine($"Iterations: {maxCount}\nContains: {containsTest.TotalMilliseconds}\nExceptions: {exceptionTest.TotalMilliseconds}");
    Console.ReadKey();
}

static TimeSpan TestCode(Action code)
{
    Stopwatch sw = Stopwatch.StartNew();
    code();
    sw.Stop();
    return sw.Elapsed;
}

GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 0:46
professionalEddy Vluggen16-May-18 0:46 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 1:21
professionalF-ES Sitecore16-May-18 1:21 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 1:39
professionalEddy Vluggen16-May-18 1:39 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 2:33
professionalF-ES Sitecore16-May-18 2:33 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 3:03
professionalEddy Vluggen16-May-18 3:03 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 3:28
professionalF-ES Sitecore16-May-18 3:28 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 3:56
professionalEddy Vluggen16-May-18 3:56 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 4:09
professionalF-ES Sitecore16-May-18 4:09 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 10:35
professionalEddy Vluggen16-May-18 10:35 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 22:27
professionalF-ES Sitecore16-May-18 22:27 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 0:03
professionalEddy Vluggen18-May-18 0:03 
GeneralRe: Is it my fault? Pin
F-ES Sitecore18-May-18 0:10
professionalF-ES Sitecore18-May-18 0:10 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 0:16
professionalEddy Vluggen18-May-18 0:16 
GeneralRe: Is it my fault? Pin
Jon McKee17-May-18 18:21
professionalJon McKee17-May-18 18:21 
GeneralRe: Is it my fault? Pin
Eddy Vluggen17-May-18 23:57
professionalEddy Vluggen17-May-18 23:57 
GeneralRe: Is it my fault? Pin
Jon McKee18-May-18 10:13
professionalJon McKee18-May-18 10:13 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 13:09
professionalEddy Vluggen18-May-18 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.