Tabs Are Evil, Part 2: The Remedy

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.

Comments:

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.

Posted by Jeremiah Johnson on April 24, 2006 at 11:34 AM PDT #

By "mixed tabs and spaces" I was referring to indenting some lines with spaces, other lines with tabs. This is what the JTable screenshot shows. I think most people agree this is a bad thing.

I definitely don't think people who choose to indent using Tabs should be "consistent" and use Tabs instead of spaces to separate tokens within a line.

Posted by Tor Norbye on April 24, 2006 at 12:10 PM PDT #

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.

Posted by Shashi on April 24, 2006 at 02:59 PM PDT #

I use the StripWhiteSpace module and it is great. Very handy indeed. Although the highlighting is getting in my way, I'd rather have the module strip the whitespaces as soon as the file gets saved. Something like that. Maybe you could put that into your module as well.
There is a bug though, and I hope it is not in your module as well. I occasionally run into the problem that I get an NPE when I start NetBeans and no editor file is open, at that occurance I get an NPE in the StripWhiteSpace module.

Iwan

Posted by Iwan on April 24, 2006 at 04:23 PM PDT #

I use the StripWhiteSpace module and it is great. Very handy indeed. Although the highlighting is getting in my way, I'd rather have the module strip the whitespaces as soon as the file gets saved. Something like that. Maybe you could put that into your module as well.
There is a bug though, and I hope it is not in your module as well. I occasionally run into the problem that I get an NPE when I start NetBeans and no editor file is open, at that occurance I get an NPE in the StripWhiteSpace module.

Iwan

Posted by Iwan on April 24, 2006 at 04:24 PM PDT #

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 ;)

Posted by Trond Norbye on April 24, 2006 at 06:20 PM PDT #

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).

Posted by jeremiah johnson on April 24, 2006 at 07:31 PM PDT #

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

Posted by anjan bacchu on April 25, 2006 at 12:17 AM PDT #

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.

Posted by kunhua on April 26, 2006 at 12:15 AM PDT #

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.

Posted by guest on April 27, 2006 at 08:04 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

Tor Norbye

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today