Will Tabs Be 4 Or 8 Spaces? (Beginning Java Forum At Coderanch)

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
building better world book advertise on coderanch meaningless drivel thread boost book review grid
this forum made possible by our volunteer staff, including ... Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders: Forum:
  • Beginning Java
Will tabs be 4 or 8 spaces? Mikey Chen Greenhorn Posts: 29 posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
In http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html says that tabs must be 8 spaces. But I remember that tabs is 4 spaces from one training course. I am confused. Is it right that indent will be 4 spaces and tabs will be 8 spaces? But in which condition will use tabs or indent? Thanks!

Rome was not built in a day.

Marilyn de Queiroz Sheriff Posts: 9109 12 posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
Generally, you indent code 4 spaces e.g. class MyClass{ public static void main( String[] args ){ // do stuff } } but if the line is too long (more than 80 characters), you indent it 8 spaces (tab) class MyClass{ public static void aVeryLongMethod( new MyClassThatDoesSomething myClassThatDoesSomething , new MyOtherClassThatDoesSomething myOtherClassThatDoesSomething , int aVeryLongIdentifer ) // do something } }

JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Mikey Chen Greenhorn Posts: 29 posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
Thanks!

Rome was not built in a day.

William Barnes Ranch Hand Posts: 1067 2 I like... IntelliJ IDE Spring Java posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
Tabs are to be avoided at all costs. Since different editors treat tabs differently. Always use spaces to indent your code. (If you are just working my yourself this isn't as important.) And there is no universal standand for how many spaces to indent. But it should be agreed upon if you are working with a group of people.

Please ignore post, I have no idea what I am talking about.

Thomas Paul mister krabs Posts: 13974 posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
Many editors have an option to insert spaces when using the tab key. You should always have that turned on.

Associate Instructor - Hofstra University Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog

Bert Bates author Posts: 9050 21 posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
Sun sez: each indent should be four spaces from the last level, and tabs should be eight spaces. So your first indent has to be four spaces, your second indent can be either eight spaces or a tab. The third can be 12 spaces or a tab and four spaces, and so on... So, everyone has a different style, but this is Sun's recommended style - and if you ever take the developer's exam it's the one you better use, even if your company uses a different style. Same is true for things like where to put your curly braces. Don't shoot me I'm just the piano player.

Spot false dilemmas now, ask me how! (If you're not on the edge, you're taking up too much room.)

paul wheaton Trailboss Posts: 24197 I like... IntelliJ IDE Firefox Browser Java posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
In the old and moldy days, all tabs were 8 spaces. Then some editors said you could specify how many spaces equaled a tab. So many geeks went with "4", that a lot of editors started making that the default. In the mean time, lots of programs stubbornly insist on 8! So then some poor slob gets a source file where the author has his stuff set up for, say, 4. Half the line are moved with tabs and half with spaces. If the slob in question has all of his stuff fixed at 4 spaces equals a tab, everything is cool! But if he has stuff set for 8, everything comes out all screwy and hard to read. My impression is that everybody is slowly moving back to a tab being the original 8 spaces and people aren't using tabs like that anymore. They use strictly the space character so nobody will see any goofiness.

my new kickstarter is live!

Dave Landers Ranch Hand Posts: 401 posted 23 years ago
  • Mark post as helpful
  • send pies Number of slices to send: Optional 'thank-you' note: Send
  • Quote
  • Report post to moderator
Indents are not Tabs and Tabs are not Indents. Indents in code should be done using spaces. Some style guides say 4, some 2, and I even know some who insist on 3. But the "implementation of an indent" uses some number of the SPACE character (ASCII 0x20 or "\u0020" or just " "). A Tab is a single character (known as "HT" or ASCII 0x09 or "\u0009" or "\t"). Often when that character is displayed, it is rendered as some ammount of blank area. It has traditionally been rendered as the equivalent of 8 SPACE characters. More often, it is rendered as "enough SPACE characters to reach the next column that is a multiple of 8 (or N) spaces". And sometimes it is some sort of arrow-like character. Because of the ambiguity in rendering, these Tab characters are worthless for code indention. However, there is a nice big key labeled "Tab" on your keyboard. It would be a shame to let a nice key like that go to waste, especially since it is easier to type than lots of spaces. For this reason, most code editors interpret your pressing of the Tab key as some sort of "Indent" signal. Good editors will take that tab and convert it into the correct number of initial spaces to make a proper indent. PS - Note that a Tab can also be used as a data delimiter (as in something to go between columns of a spreadsheet-to-text output or something. PPS - Tab is also a diet drink marketed by the Coca-Cola company. You might risk copyright/patent violations if you release code containing lots of Tab. US$ 0.02, Dave
Consider Paul's rocket mass heater.
reply reply
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread! Similar Threads
  • code indentation - anonymous class
  • Assignment code indentation...help!
  • Coding conventions
  • code convention tools
  • Indentation for Throws clause of a method
More...

Tag » How Many Spaces Are In A Tab