Colorzilla Developer Add-On for Firefox

I found this amazing tool today:  The ColorZilla developer add-on tool for Firefox.  If you’re a web developer, and like browser F12 tools, you’ll love this.  It has an Eyedropper tool that lets you easily get the color of anything in the browser window, then automatically copies it to your clipboard.

EyeDropper

There are plenty of other features too, such as a color palette browser, and a webpage DOM color analyzer, which its pretty cool.  It extracts all the colors for the page in your current tab.

DomAnalyzer

My advice?  Definitely worth installing!

Using Json.NET Serialization Attributes with clean classes

Json.NET is a wonderful library for serializing and deserializing JSON in .NET.  It is immensely popular, and is included in the project template for ASP.NET applications.  Often when deserializing JSON, the JSON data does not map to your classes neatly.  You can use serialization attributes to decorate your class members, which will control how Json.NET serializes and deserializes your JSON.  If you use interfaces to define your objects, you can decorate the implemented class while keeping your core definitions clean.

[snippet id=”15″]

[snippet id=”13″]

The decorated MySuperStation class above allows us to deserialize JSON containing different property names by mapping the JsonProperty attribute while keeping our interfaces free of decoration.

 

Abstract Classes vs Interfaces in C#

When should you use an abstract class, when should you use an interface?  Here’s the lowdown on some of the reasons to choose one over another.  First, take a look at a very simple example of an abstract class and two classes that inherit from it.

[snippet id=”8″]

Setting aside technical reasons, one of the strongest benefits to using an abstract class is the ability to provide an invariant implementation of some functionality across all sub classes, while leaving other implementations up to the subclass.  In our example above, all types of toast must first be toasted in a toaster.  Then, depending on the type of toast, the subclass can add whatever type of spread it wants.

On to the technical side.  An abstract class cannot be instantiated.  It is meant to be inherited.  If a method is not marked as abstract in an abstract class, the implementation belongs to the abstract class.  Abstract methods are implicitly virtual, and are meant to be overridden by the subclass.  They have no implementation in the base class, and therefore no method body, just a signature.  An class can inherit from multiple interfaces, but only one abstract class.  Here’s a more real world example.  Imagine a website back end where we want to collect and process credit cards for online purchases.  We might implement an abstract class such as the following:

[snippet id=”9″]

The abstract PaymentBase class dictates that no matter what way we collect card information in the front end website, we must always process the payment the same way.

Interfaces are like contracts that dictate all the functionality a class must have that implements it.  When you implement an interface, you must define a method body for all the method signatures in the definition.  Interfaces are great for dependency injection, which states that we should always program to an interface, not an implementation.  This is beyond the scope of this article, but warrants further reading if you’re not familiar with it.

Interfaces enable you do decorate additional classes of the same concept, adding functionality.  Consider the example below.  We have a Customer and a LoyalCustomer class.  They both implement the ICustomer interface.  While we can place an order for each type of customer using the same interface, we can apply a discount to a loyal customer.  Meanwhile, all throughout our application, we can use the ICustomer interface anywhere we refer to a customer, and pass either Customer or LoyalCustomer to the method.  This also allows us to extend Customer to additional custom types down the road.

[snippet id=”10″]

That’s all for now.  I hope you enjoyed this post!

 

 

 

 

 

 

 

 

Why is a manhole cover round?

If anyone ever asks you this in an interview, as I’ve had asked of me, they’re probably testing how you think.  I was a bit shocked at first, what does this have to do with code?  Much, as it turns out.  How do you answer this question?  Many of you know the answer, but if you were like me, sadly I did not.  I gave it my best guess.  Reason out loud.  That’s the most important strategy here.  Turns out manhole covers are round so they don’t fall in the hole, and possibly on someone working in the hole.

Edit in peek definition Window!

This is the coolest thing since Spreadable Bacon.  In Visual Studio 2017, you can view a method definition, and edit the code right in a pop up window with the Peek Definition command.  In the screenshot below, I’m actually editing the code right in the pop up window itself.  This will save you a bunch of time by not having to dig down into other folders where your classes or methods are defined, just to make a minor adjustment.

peekDefinition