Javascript patterns

In our days Javascript appears to conquer all . So I have decided to read a book about patterns in Javascript. And I definitely recommend this book: it is good written and full of practical details. It is a must read for anyone to unleash their javascript programming potential.

Non (Coding Standards)

 

What do you say when you see this code?

 
Console.WriteLine(((ThirdEnum)(new Task<string>(() => 
{ 
    return ((FirstEnum)((Func<string>)(() => 
    { 
        return "Hello World"; 
    })).Invoke().Invoke()).Invoke(); 
})).Await().ContinueWith((@string) => 
{ 
    return ((SecondEnum)(@string.ContinueWith((@return) => 
    { 
        return ((FirstEnum)(@return.Result.Invoke() + 3)).Invoke(); 
    }).Result.Invoke())).Invoke(); 
}).GetAwaiter().GetResult().Invoke()));

Console.ReadKey();
 
namespace ConfusingCode
{
    public static class Things
    {
        public static int Invoke(this string invokedItem)
        {
            lock (Program.key)
            {
                Console.Write(invokedItem.Length == 1 ? invokedItem + (invokedItem == ((SecondEnum)invokedItem.Length - 1).Invoke() ? invokedItem : "") : "");
            }
            return invokedItem.Length > 0 ? 0 : 0;
        }
        public static Task<string> Await(this Task<string> taskToAwait)
        {
            taskToAwait.Start();
            return taskToAwait;
        }
        public static string Invoke(this FirstEnum val)
        {
            return ((Enum)val).Invoke();
        }
        public static string Invoke(this SecondEnum val)
        {
            return ((Enum)val).Invoke();
        }
        public static string Invoke(this ThirdEnum val)
        {
            return ((Enum)val).Invoke();
        }
        public static string Invoke(this Enum @enum)
        {
            return @enum.ToString();
        }
    }
    public enum FirstEnum
    {
        P = 0,
        I = 3,
    }
    public enum SecondEnum
    {
        Z = 0,        
    }
    public enum ThirdEnum
    {
        A = 0
    }
}

And what should be added to the coding standards in order to not permit this … code ?
( Credits to Stefan Petrini for the code )

IDisposable and Marshal.ReleaseCOMObject and dynamic in C#

 

If you have worked with COM, you know that for every COM object that you access is not only necessary to null the reference, but also call Marshall.ReleaseCOMObject .

This is a very error prone task, because you have all try / finally blocks

 
// declare a, ws, w 
try{ 
a=new Application(); 
ws = a.Workbooks; 
w = ws.Add(); 
//code 
}
 catch(...) {
 if( w != null){ 
Marshal.ReleaseComObject(w); 
w = null; 
}
 if(ws !=null){ 
Marshal.ReleaseComObject(ws); 
ws = null; 
} 
if( a!= null) { 
Marshal.ReleaseComObject(a); a= null; 
} 
} 

All of this could be done easy with IDisposable, if

1. IDisposable calls the Marshal.ReleaseCOMObject

2. Find a way to call methods / properties on the COM object from the class that implements IDisposable

 

I have done this and the result is like this:

 
using (dynamic a = new ComDisposable(new Application())) { 
using (dynamic ws = a.Workbooks) { 
using (dynamic w = ws.Add()) { 
using (var shs = w.Sheets) { 
using (var s = shs[1]) { 
//Worksheet a; 
using (var r = a.Range("A1")) { 
r.Value2 = "http://ignatandrei.github.io/ToolsAndUtilities/";; 
using (var f = r.Font) { 
f.Bold = true; 
}
 }
 }
 }
 w.SaveAs(fileName); 
w.Close(); 
} 
}
 a.Quit(); } 

 

Video at https://youtu.be/2qbAcSjL1gU

NuGet package at https://www.nuget.org/packages/ReleaseComObjectDisposable/

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.