Coding Style – How to Warn About Forbidden 3rd Party Methods

ccoding-styleidejava

Note: This question refers to code written in Java or C#.

I am managing a couple of large projects where we have discovered issues (not necessarily bugs) with some 3rd party/SDK methods and have written our own extensions that should be used instead. We want developers to remember that using those methods is not recommended for this project.

If we had been using our own libraries we could easily remove that method or mark it obsolete/deprecated but we cannot do so for libraries that we didn't write.

As an example, we use a library that provides us with two overloads:

acme.calculate(int quantity_, double priceInUsDollars_);
acme.calculate(int quantity_, string currencyCode_, double priceInCurrency_);

We want developers to always use the first one and get price in US Dollars from our own standard FX rate systems. And it'd be nice to have the IDE (Eclipse/Visual Studio) warn the developers when they use the first one. A compiler warning will suffice too.

Right now, as it stands, we have to rely on the code reviewers to spot such errors and as you can see that is not a reliable approach.

One possible way I am prepared to go is to write my own check style check (http://checkstyle.sourceforge.net/writingchecks.html). But I was wondering if there was something simple that I could use. Does anyone know of ways to achieve an IDE/compiler warning of the sort I have described?

Non IDE/compiler solutions are most welcome.

Best Answer

Using a tool like NDepend / JavaDepend, you could write custom CQL queries to generate warnings for these very specific cases.

You said in the question you wanted the IDE/Compiler to warn the developers. I think because NDepend/JDepend integrate closely with the IDE this may solve your problem.

Related Topic