Monday, April 24, 2006

Tabs Are Evil, Part 2: The Remedy

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


A while ago I argued that you should completely avoid Tab characters in your Java source files. Use spaces for indentation. I won't repeat those arguments here. But even those arguing in favor of tab-indented source files agree that mixing spaces and tabs is evil.



The problem is that sometimes you're not aware that you're working on source with mixed tabs and spaces. This can happen easily if you edit the source files with an editor that has the wrong (as far as you're concerned) handling of Tabs.



To help avoid this you can use a simple NetBeans module I just committed into CVS, under contrib/fixtabs. You can grab the NBM file here and install via Tools | Update Center. I'll submit it to nbextras.org soon so you can get it there later.



Here's what the module looks like in action. If you for example open the JDK JTable class and look at the constructor, you'll see something like this (click for full size):








Ewwww!



When you encounter files like this, you can go to the edit menu and select the action to convert the tabs to spaces. You can also turn off Tab highlighting here.






(Update 4/28/06: I changed this such that the "checkbox" toggle is now in the View menu, along with other view states you can enable and disable, and I moved the Convert action to the Source menu along with other source transformation actions.)



The module is similar to one of my favorite modules (which I guess I haven't described yet) - the "stripwhitespace" module. That module highlights trailing whitespace in the current source file, and lets you remove these. Not only is the fixtabs module similar - it's heavily based on the same source code.



Let me know if you encounter any bugs.


7 comments:

  1. Mixed tabs and spaces are *not* necessarily evil. Tab to your current indentation level, and no further, then use spaces from then on, and no spaces. Never tab after using a space.
    Follow those rules and you'll be able to view your source code at any tab width AND keep all your formatting looking good.

    ReplyDelete
  2. One other way could be that Netbeans shows the whitespace characters. It could be in very light color, but it would allow the developer to know that he/she is mixing tabs and spaces.

    ReplyDelete
  3. I think I'll install those modules and give them a try, but is it possible to let them perform their work automaticaly? (convert tab to spaces and strip trailing spaces when I open and save the file?). Since I use the module I want to reformat the file, so why can't it do it for me automagically ;)

    ReplyDelete
  4. ah yes well i think anyone can agree with that. sorry i misunderstood.
    tabbing one line then spacing the other is something that a lot of my programmers do a lot. it might look fine on your machine (4-wide tabs), but on mine (two-wide) it looks like junk, and in CVS-Web it looks abysmal (8-wide).

    ReplyDelete
  5. hi tor,


    I use emacs/eclipse and have a shortcut to a batch file which strips whitespace.


    People who love their sourceCode have common ways of getting things done! :-)


    BR,

    ~A

    ReplyDelete
  6. HELLO,
    I'm a student in France. I'm working with NetBeans for the development on mobil. I have a question to ask. May I write a MIDlet which can run like a program working back of OS? That means it won't be visible after installing. At the same time, I want the other MIDlet to run.

    ReplyDelete
  7. That suggestion someone made about using tabs to indent and then spaces still complicates editing. You cannot differentiate between different types of whitespace so both cut-and-paste operations as well as general editing is a pain when ANY mix is used in source code. Tabs are inconsistent so why some people still insist on using them is bizarre. If you really, really want to change the look of someone else's code then just pass it through a beautifier. In all other cases it's better to use consistent characters: spaces.

    ReplyDelete