Writing - 2016

2 posts from 2016

April (1)

Apr 14, 2016

Why does the Nameof operator in VB.NET return the member name with the wrong case?

nameof is one of my favorite operators in C# 6 – it’s a little thing that makes life a little easier.  I use it a lot in Entity Framework 6 with the ForeignKey attribute and when throwing ArgumentExceptions so that I don’t have to have magic strings everywhere.

I discovered some interesting behavior regarding Nameof in VB.NET that I thought took VB.NET’s case insensitivity to the next level:

Class Program
    Sub Main()
        Dim firstNameOf = Nameof(YourClass.FirstName)
        Console.WriteLine(firstNameOf)
        'Output: FirstName
	
        Dim secondNameOf = Nameof(YourClass.fIRsTnAMe)
        Console.WriteLine(secondNameOf)
        'Output: fIRsTnAMe
    End Sub
End Class

Public Class YourClass
    Public Property FirstName As String
End Class

See the difference in outputs?  FirstName versus fIRsTnAMe?  I mean, c’mon, really?  The Nameof operator returns exactly what you type inside of it and not the actual properly cased member name??

This actually caused me a fair bit of grief the other night – I was using Nameof with the ForeignKey attribute in EF and kept getting these odd exceptions where it said it couldn’t find the member called Firstname – and all because I mistyped the casing for the property (FirstName.)

I’ve always had a soft spot for VB.NET – in fact I wrote a whole blog post about it a couple of years ago (lost to the ages).  I don’t use it nearly as much anymore – I only have one client who has a VB.NET app.  I gotta say though, it’s weirdness like this that makes me not miss it much, cause that sure is a strange design decision.  It’s one of the very few times that a VB.NET feature is truly not on par with a similar C# one.

(See also: unexpected behavior when using VB.NET’s If() with nullable types: http://stackoverflow.com/questions/10699740/mystery-of-the-if-function-in-vb-net)

Follow @schneidenbach on Twitter/X

February (1)

Feb 29, 2016

RESTful API Best Practices and Common Pitfalls

As a person who spends his day integrating systems, I’ve found that at least half of the APIs I use radically differ from REST semantics, make changes too often and too quickly, don’t validate enough, and/or don’t have proper documentation. There are tons of resources for making good RESTful APIs already, but I thought I’d add to the mix with some semantic rules and some technical ones that I see broken over and over.

Now, I’m not advocating that you should spend your time trying to implement a fully-compliant REST API — very few have and there’s not much benefit to doing so. I’m a believer in pragmatic REST — that is, you should do what makes sense and throw out what doesn’t. I’m being vague on purpose because it really comes down to your particular use case.

Honestly, good REST design practices could fill an entire book. For the sake of brevity, I’ve chosen ten– four related to technical implementation, six related to semantic. The examples are done using ASP.NET Web API, but the semantic stuff (and the technical, conceptually!) also applies to RESTful APIs made using other web frameworks and languages.

When it comes to RESTful API design, I have two main rules.

  1. Do what’s expected. No reason to get creative — a really creative API is probably a bad API. Follow established best practices.
  2. Be consistent. Use the same endpoint structure, HTTP status codes, HTTP verbs, etc. for your requests, for the same reasons. A poorly formed request should return 400, not 404.

Continue reading on Medium.