NPM & left-pad: Have We Forgotten How To Program?
Intro
Okay developers, time to have a serious talk. As you are probably already aware, this week React, Babel, and a bunch of other high-profile packages on NPM broke. The reason they broke is rather astounding:
A simple NPM package called left-pad that was a dependency of their code.
left-pad, at the time of writing this, has 11 stars on GitHub . The entire package is 11 simple lines that implement a basic left-pad string function . In case those links ever die, here is the entire code of the left-pad package:
To Node.js Or Not To Node.js
Intro
Node.js – it has rapidly become the “new hotness” in the tech start-up realm. With each passing day, the fan base of Node lovers grows larger, spreading their rhetoric like a religion. How do you spot a Node.js user? Don’t worry, they’ll let you know.
One day you’re at a regular user group meeting, sipping soda and talking with some colleagues, when the subject turns to Node. “Have you guys tried Node.js?” asks one of the people in your group. “It’s all the rage. All of the cool kids in Silicon Valley are using it!” “What does it do?” you ask, only to be bombarded with a sales pitch worthy of the best of used car lots. “Oh, it’s amazing!” they reply, sipping their diet coke and shuffling their hipster fedora and backpack with MacBook Pro in it (or something like that), “It’s server side JavaScript. It runs on a single thread and it can do 100,000 web requests a second!” They glance at the group for the oohs and ahhs, but most people just stare back with amazement in their eyes. Then, your hipster Node-loving friend drops the words that start wars: “It’s way better than .NET” – and just like that, your group is hooked. They go home, download the Node.js tools, write “Hello World”, and suddenly they’re on their way to the next user group meeting to talk about how great Node is.
C# Probably Getting New “Safe Navigation” Operator “?.”
It looks as if the Visual Studio dev team may be implementing a new operator in a future .NET release. This is due in large part to community demand, which is pretty cool because it shows that the VS team is listening to their customer base; a key part of a successful product.
This new operator is likely going to take the syntax of ?.
and is known as the Safe Navigation Operator.
Trigger IValidatableObject.Validate When ModelState.IsValid is false
I recently came across an ASP.NET MVC issue at work where the validation for my Model was not firing correctly. The Model implemented the IValidatableObject
interface and thus the Validate
method which did some specific logic to ensure the state of the Model (the ModelState
). This Model also had some DataAnnotation
attributes on it to validate basic input.
Long story short, the issue I encountered was that when ModelState.IsValid == false
due to failure of the DataAnnotation
validation, the IValidatableObject.Validate
method is not fired, even though I needed it to be. This problem arose due to a rare situation in which ModeState.IsValid
was initially false but was later set to true in the Controller’s Action Method by some logic that removed errors from the ModelState
.
Interview with InfoQ on SimplSockets
Jonathan Allen of InfoQ conducted an interview with me about one of my open source initiatives, SimplSockets. We discussed the value of TCP over HTTP and why Sockets are still relevant to programming.
I’d like to thank Jonathan and InfoQ for the opportunity – it was a great discussion. Check it out here: http://www.infoq.com/news/2013/12/SimplSockets
MVC4 Conditional HTML Attributes
MVC4 made one simple and yet awesome improvement to View rendering that I don’t think many people are aware of.
Have you ever had to conditionally add an attribute to an HTML element in your MVC View based on the presence of a variable? The typical use case is applying a CSS class to a div. Most of the time that code looks something like this:
<div @(myClass == null ? "" : "class=\"" + myClass + "\"")></div>
What a pain – not only to write but to read… This destroys the View’s readability and clutters the HTML up big time!
Automatically Generate POCOs From DB With T4
Web API Mapping QueryString/Form Input
If you’re using the Web API as part of the MVC4 framework, you may encounter a scenario in which you must map parameters of strange names to variables for which characters of the name would be illegal. That wasn’t very clear, so let’s do this by example. Consider part of the Facebook API:
Firstly, Facebook servers will make a single HTTP GET to your callback URL when you try to add or modify a subscription. A query string will be appended to your callback URL with the following parameters:
Generic Comparer
Have you ever had to write a comparer for a specific type, only to be frustrated when you needed to write a second and third comparer for other types? Fear not, a generic comparer can take care of this for you!
/// <summary>
/// Compares two objects of any type.
/// </summary>
/// <typeparam name="T">The type to be compared.</typeparam>
public class GenericComparer<T> : IComparer<T>
{
// The compare method
private readonly Func<T, T, int> _compareMethod = null;
/// <summary>
/// The constructor.
/// </summary>
/// <param name="compareMethod">The compare method.</param>
public GenericComparer(Func<T, T, int> compareMethod)
{
// Sanitize
if (compareMethod == null)
{
throw new ArgumentNullException("compareMethod");
}
_compareMethod = compareMethod;
}
/// <summary>
/// Compares two objects.
/// </summary>
/// <param name="x">The first object.</param>
/// <param name="y">The second object.</param>
/// <returns>Less than 0 if x is less than y, greater than
/// 0 if x is greater than y, 0 if they are equal.</returns>
public int Compare(T x, T y)
{
return _compareMethod(x, y);
}
}
Just pass a method to the constructor that takes 2 objects of type T and returns an int, and you’re all set!