Maintaining coding guidelines: naming convention vs syntax highlighting

Posted on by Egor Malyshev

Naming conventions were subject to many local corporate wars, and probably any developer heard phrases like “These underscores before private field names just look ugly!” One more problem with naming conventions is constant changes in the code (if you convert a local variable to a field, will you rename it?). And of course, developers often simply forget about all these conventions and cannot remember whether it is necessary to use upper or lower case first letter for a constant.
However, it is necessary to make the code readable by different team members. And of course, it is always useful to be able to quickly distinguish local variables from fields, classes from abstract classes and interfaces, static methods from non-static ones; and so on.
With IntelliJ IDEA, the solution for all these problems is code highlighting. If you still think that code highlighting only makes keywords bold, just look at IntelliJ IDEA highlighting settings (Settings | Colors and Fonts | Java).

Here you can define the color schema for:

  • Class
  • Abstract Class
  • Interface
  • Local Variable
  • Parameter
  • Instance field
  • Static field
  • Method call
  • Static Method call
  • And more

So, you can easily understand the code of your team mates even if no naming convention is used within the team. All you need is to adjust your color schema!
In fact, syntax highlighting can be an even more powerful tool for code analysis than naming convention. For example, in IntelliJ IDEA you can highlight unused symbols, so you can easily find the potential dead code. One more useful trick is to highlight reassigned parameters to see whether parameters change their values within the method body (that is often considered as “bad coding style”). Also you can highlight reassigned local variables.
Well, suppose you still think that naming convention is a must for your team (let’s say you have different IDEs in your team and not all of them have so flexible syntax highlighting). And while you consider violation of the naming convention a serious coding error, the compiler doesn’t. So everybody keeps violating them without a notice.
A good IDE should not force you to use naming convention, but of course it should help you with following the convention if you choose one. In IntelliJ IDEA, there is a set of code inspections that allows you to define naming patterns and then check whether the code violates them. You can configure naming convention settings in the Settings | Errors dialog.

If you set the constant naming conventions as shown on the screenshot above, when incorrectly named constants will be highlighted right in the editor.

The yellow light bulb shows that IntelliJ IDEA can help you with solving the problem. Just press Alt + Enter, and select to rename the symbol. All the usages of this symbol in the code will be also renamed. This way you can quickly make the code to follow the necessary convention.
Of course, IntelliJ IDEA inspections can help your not only with naming conventions, but also with other coding guidelines (comments, annotations, parenthesis, and more) and static code analysis. But this can be a subject of a separate topic.
So, now before establishing naming convetions in your team, think whether you really need one. And if it’s true, you can at least make its maintenance easier.

Note   This tip&trick was originally posted at www.javalobby.org.

Comments below can no longer be edited.

3 Responses to Maintaining coding guidelines: naming convention vs syntax highlighting

  1. Jared MacDonald says:

    February 15, 2007

    First off, we love IntelliJ here and in particular we love the massive list of code inspections (“method can be made static” and “field can be local” are some of my favorites).

    We’re wondering, though, if there’s a way to get this big list of checks and run it against files from the command line, specifically, to integrate with part of a build process. There are other tools out there that to do this (CheckStyle, PMD), but they don’t seem to have the breadth of messages that IntelliJ has; in addition, it’s clearly convenient if the tool that developers have built in to the IDE is the same tool that’s built in to the source control system.

    Thanks for the great work!

    Jared

  2. Egor Malyshev says:

    February 27, 2007

    First of all, you can use TeamCity solution which includes all of IntelliJ IDEA inspections integrated for batch running plus nice Web based reports and instant results viewable through IntelliJ IDEA plugin.
    Also you can try running the inspection.bat file from BIN folder under IntelliJ IDEA installation folder. It will generate the XML file with all inspections’ results. The small drawback is that you’ll have to manually correct all errors it reports.

  3. Ajit says:

    July 17, 2007

    Hi,

    Idea’s ability to run customized code inspections is simply great ! Thanks for the same.

    I am looking at ways so that my inspection profile can be shared across teams. How do I export my Code Inspection Profile, and import the same on another instance of Idea?

    Regards,
    Ajit

Subscribe

Subscribe for updates