Easily misused language features
In the comments of my previous post about my bad experiences with Java’s import static my colleague Carlos and several others pointed out that it is actually a useful feature when used properly.
The code base where I initially came across the feature misused it quite severely but it got me thinking about other language features I have come across which can add great value when used effectively but lead to horrific problems when misused.
Apart from import static some other features which I can see easy potential for misuse are:
C#'s automatic properties which seem to make it even easier to expose object’s internals than it already is but perhaps could be useful for something like Xml serialisation and deserialisation.
C#'s var keyword which helps to remove unnecessary type information and can lead to shorter methods when used well but completely unreadable code when misused.
Ruby’s open classes which give great flexibility when working effectively with 3rd party libraries for example but can lead to difficult debugging problems if overused. The same probably also applies to C#'s extension methods.
I have no doubt there are other features in other languages and probably more in the languages I listed but those are some of the ones that stood out to me when I first saw them.
I like this quote from Nate in the comments about import static, referring to being able to being able to tell what the code is doing just from looking at it:
The rule is simple (and somewhat subjective… if you aren’t sure, just ask me what I prefer. :) ) — if it helps the code to communicate better (especially contextually!) then use it.
Maybe a similar type of rule is applicable when using other language features as well and there is no feature that we should be using all the time - only if it makes our code easier to read and understand.