Indentation

Pages: 12
Do we have shortcut key for indentation?
It depends entirely on what IDE or text editor you are using. 99% of the time it is the tab key, and 90% of the time indentation is done for you.

There's also a huge debate about tab characters vs multiple space characters.
Last edited on
closed account (10X9216C)
There isn't really a huge debate, space supremacy viewing code the way it's meant to be viewed.
myesolar wrote:
space supremacy viewing code the way it's meant to be viewed.
I don't know what you mean by this.
closed account (10X9216C)
Means no matter which text editor, ide, or settings you roll, the code will always look the same. The way it was intended to look.
the code will always look the same
If tab characters are used, the text may appear different depending upon how wide the tab is interpreted as being. That's a disadvantage. On the other hand tabs may be easier to type and subsequently edit.
closed account (10X9216C)
Please read more than one post before making a comment.

On the other hand tabs may be easier to type and subsequently edit.

Most provide tab as spaces, where pressing the tab button, or shift+tab or backspace adds/removes the appropriate spaces.
Last edited on
I don't understand why it's so important that it looks the same everywhere. Using tabs allow each programmer to use the tab width he wants.
Just to add, I don't have any strong opinion as to which approach is best (I use both - but not at the same time).

Another difference, if tabs are used intermixed with spaces then the appearance can change in unexpected ways if the tab size is altered for any reason, so using tabs may require some degree of self-discipline.


Tabs are for indentation. Spaces are for alignment. If you don't like tabs, then spaces do both and you can't tell them apart.
Last edited on
Tells you how different it is from when I started. When I started, the programmers I knew simply did three spaces at the start of each line and three more for each nested item. I never bothered with tabs until I started using IDEs like Geany and such that did it for me, before that I simply stayed to 3 spaces.
> I never bothered with tabs until I started using IDEs

Before the advent of IDEs, every real programmer was made painfully aware of the difference between tabs and spaces. It is only after IDEs have come into widespread use that hobbyist/amateur programmers can survive, living in blissful ignorance of makefiles.

No discussion of make(1) would be complete without an acknowledgement that it includes one of the worst design botches in the history of Unix. The use of tab characters as a required leader for command lines associated with a production means that the interpretation of a makefile can change drastically on the basis of invisible differences in whitespace.

Why the tab in column 1? Yacc was new, Lex was brand new. I hadn't tried either, so I figured this would be a good excuse to learn. After getting myself snarled up with my first stab at Lex, I just did something simple with the pattern newline-tab. It worked, it stayed. And then a few weeks later I had a user population of about a dozen, most of them friends, and I didn't want to screw up my embedded base. The rest, sadly, is history. -- Stuart Feldman

http://www.catb.org/esr/writings/taoup/html/ch15s04.html
closed account (10X9216C)
I don't understand why it's so important that it looks the same everywhere. Using tabs allow each programmer to use the tab width he wants.

Sure they can use whatever tab space they want, that doesn't mean it'll look as good.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 4 space tab
Some function(  void name,
                void name,
                void name,
                void name,
                void name    );

// 8 space tab
Some function(  void name,
                                void name,
                                void name,
                                void name,
                                void name       );
// etc 


In either case, it is also more error prone to have both spaces and tabs unless you are using a decent editor that has dots or dashes to distinguish whitespace. It adds un-needed complexity to the format of your code, the only way to fully make sure is to switch tab space size every time you write or reformat code to make sure it looks correct at a different setting. I personally don't see why it's such a big deal what someone else prefers, if tab space size is within reason (2-10) then i really don't care what it is. If i see code that's written as tabs, that means if i want to write or add code into it, i'll have to write using with tabs (i usually don't put the effort into making sure the code aligns at any other tab size anyways) which is much more effort than reading my not-so-preferred-yet-reasonable tab space size.

Tabs are for indentation. Spaces are for alignment.

You are just making your life that much harder mixing them. You can't achieve an aesthetic format with tabs alone as you'll just end up with a mess at any other tab size, you can with spaces alone, hence space supremacy, simplify your code today ~ make the switch.

Tells you how different it is from when I started. When I started, the programmers I knew simply did three spaces at the start of each line and three more for each nested item. I never bothered with tabs until I started using IDEs like Geany and such that did it for me, before that I simply stayed to 3 spaces.
Too lazy to remap tab key huh.
Last edited on
@meysolar: Why would you use tabs for alignment? That's an invalid argument.

Who reported myesolar's post?

EDIT: To clarify, using tabs for alignment is an invalid argument against using tabs for indentation.
Last edited on
closed account (10X9216C)
@meysolar: Why would you use tabs for alignment? That's an invalid argument.

Why would you hit space 16+ times rather than tab twice+ ?

Who reported myesolar's post?

Wouldn't doubt BHX.

To clarify, using tabs for alignment is an invalid argument against using tabs for indentation.

If you are ok with mixing spaces and tabs, which i honestly wouldn't do. If i end up with some sort of alignment problems, i don't have to worry about whether i have to use spaces or tabs, i don't have to go check that everything i already did conforms to a standard, that if not followed, will result in misaligned code. I hit space or tab as needed and know that it doesn't matter, it'll be aligned no matter what i do.
Last edited on
I know with my IDE if I hit space 4 times instead of tab it just converts them to a single tab in.
Jeff Atwood talked about this topic back in 2009:http://blog.codinghorror.com/death-to-the-space-infidels/


Choose tabs, choose spaces, choose whatever layout conventions make sense to you and your team. It doesn't actually matter which coding styles you pick. What does matter is that you, and everyone else on your team, sticks with those conventions and uses them consistently.

That said, only a moron would use tabs to format their code.
myesolar wrote:
Why would you hit space 16+ times rather than tab twice+ ?


Who does that? You hit tab and have your editor auto-convert to spaces. They all have the option.

I hit space or tab as needed and know that it doesn't matter, it'll be aligned no matter what i do.


Yeah it'll be aligned for you. When someone else with a different tab setting opens it, it'll look totally screwed up.



Spaces are superior.
Disch wrote:
> Why would you hit space 16+ times rather than tab twice+ ?

Who does that? You hit tab and have your editor auto-convert to spaces. They all have the option.
I have yet to see an editor which features automatic differentiation of indentation vs alignment.
1
2
3
4
5
6
namespace n
{
	void f(int   a,
	       short b,
	       long  c);
}
Notice the differing locations of tabs (for indentation) and spaces (for alignment) - no tab character exists past the v in void. As rasd was saying, you have to manually control alignment even if your IDE controls indentation - it's a pain but I prefer it.
Last edited on
closed account (10X9216C)

Disch wrote:
Who does that? You hit tab and have your editor auto-convert to spaces. They all have the option.

Rather than repeating myself in the same thread.
mysolar_about_10_posts_up wrote:
Please read more than one post before making a comment.

I was talking to LB, who mentioned using both tabs and spaces, in this case you would have to press space 16 times.
1
2
tab tab SomefunctionName(void name,
tab tab <-  16 spaces  ->void name);


Disch wrote:
Yeah it'll be aligned for you. When someone else with a different tab setting opens it, it'll look totally screwed up.

Did you even read more than one sentence? Seriously now. I'm for spaces for that exact reason, gum gum dum dum:

me wrote:
If you are ok with mixing spaces and tabs, which i honestly wouldn't do. If i end up with some sort of alignment problems, i don't have to worry about whether i have to use spaces or tabs, i don't have to go check that everything i already did conforms to a standard, that if not followed, will result in misaligned code. I hit space or tab as needed and know that it doesn't matter, it'll be aligned no matter what i do.
Last edited on
Pages: 12