Convert tab to spaces - coding-style

Usually we replace tabs to spaces in editors while coding.
What is the exact need for that?
Is it for because the OS handles \t in the files?
I am really not very sure of this. Kindly clarify

Tabs don't have fixed width, its width depends on IDE (editor) settings. It is usually set to have width 4 spaces, but it doesn't have to be. There probably wouldn' be any problem if you use only tabs and don't use spaces, but this will never happen. You can bet, you will always want to move some text by only one extra space to the right. Then you will be mixing spaces and tabs together and that's what brings trouble. It might look correct in your IDE, but then you will want to do some change in vim editor for example, where there will be different tab size set. Indentation of the code won't be what would you expect. Also when you send some code to your co-worker, he might use different IDE with different settings and he will again see wrongly aligned code. On the other hand with spaces only you see what see the others.

Related

Is it possible to make double space appear as tabs?

I'm working in a project that uses the standard javascript syntax. It's nice, but it uses spaces for indentation. Is there some way to make double spaces work as tabs, with the ability to set tab-space etc?
As far as I know, this is not possible. Spaces and tabs are different by nature and they're also used differently in different projects. Some projects prefer to maintain the standard of only spaces, or tab and spaces or only tabs.
What you're asking is basically: is it possible to show all letters A as a letter O?
However, something that might help for your case is automatic indentation / code beautifier. Most code editors have this feature and it basically indents your code based on a standard (which you most of the time can customize).
If you're using PHP Storm for example, the shortcut ctrl + alt + L will indent and beautify your code. On the ATOM editor you can install a package to add that feature.
Keep in mind that messing about with indentation might cause conflicts with git if you're part of a team. And, if you're part of a team it's always better to keep their standards anyway.
As a final note, spaces vs tabs is a religious war. Try to keep your team's or library standard is a good recommendation most of the time.

Notepad++ convert leading spaces to tabs upon entry

Very close to reverse of this question. I prefer coding with 2-whitespace indentation, but need to have files indented with tabs to align with project convention. What I would like to do is preferably automatically convert 2 spaces upon entry to tab symbol in Notepad++ and have the editor configured to tab length of 2.
A possible manual way for doing this could be Edit->Blank Operations->Space to TAB but this converts all of my spaces to tabs, even those of length 1 - which are, for example, spaces between function arguments, not just leading spaces.
In a perfect case scenario I'm trying to achieve formatting style as described in this question, but with typing just spaces and the editor taking care of the rest.
I'm on Notepad++ 6.0, but willing to upgrade if this helps
Let me complete the answer of Ari Okkonen to add a workaround to the problem commented by Sergii Zaskaleta of mixed tabs and spaces at the beginning of the line.
Settings->Preferences->Tab Settings->Tab size: 2 (if not already)
Edit->Blank Operations->Space to TAB (Leading)
Select a block of lines of text with the problem of mixed spaces and tabs. Press [Tab] and [Shift]+[Tab] to add and remove a tab from each line. In the process, the leading spaces had been converted to tabs.
A manual way that seems to work: After having edited the file before saving you may try (Works in Notepad++ v6.8.3):
Settings->Preferences->Tab Settings->Tab size: 2 (if not already)
Edit->Blank Operations->Space to TAB (Leading)

In Xcode4, can you change the characters used to display invisibles?

I much prefer Visual Studio's way of displaying invisibles... a simple dot for spaces, and an arrow for tabs. When you change the color to be a light-light-gray, they provide excellent help when viewing code alignment and such but they're barely noticeable so you can leave them turned on all the time without really getting in the way.
Xcode4 however (and maybe other versions as well) instead display some truncated-'U' shape for a space and don't appear to show anything for a tab, Worse, as I mentioned in another post, Xcode doesn't respect its own setting for invisibles color.
Still, this question is about changing the default character used. I don't care if it's a hack of a plist file or even digging around in the contents of Xcode's app bundle (knowing any updates would revert it if it was) but as they are now, they're just too unusable because of how much they obstruct whitespace and thus skimming of code. (VS really nailed that.)
The only way (I could figure out) to do it is to find the symbol that it uses (note they change along with font) open the font in a font editor and copy paste the glyph you want to the one in spot occupied by the character you want to change. There are a few free editors and some really expensive ones I was able to use ttfedit http://mac.softpedia.com/get/Utilities/TTFEdit.shtml to find and change the character XCode used for spaces on one of the more silly fonts I have to make sure it worked before I answered. Saved font to desktop, double clicked and installed it, osx complains that it is duplicate font, say resolve differences and pick your new file and say resolve to turn off the old font. Next time you open xcode you should see your new symbol for space.
Probably another way to do this, but may be the only way without getting deep into XCode source to find where it makes decision for symbol to use (note many use white diamond but helvetica for instance uses a kinda floor bracket thing, you may see the pattern but I didn't).

How to manipulate the contents of CEdit?

I have a situation with edit control and I need some guidance. The text editor functions normally in most cases but in other cases, depending on the last few characters before typing and based on the characters typed, the last few characters must be replaced with different characters.
The solution that looks obvious to me is to have a character buffer, GetWindowText() just before the contents are changed, add the characters typed into the buffer, manipulate the buffer if necessary and then SetWimdowText().
I know the edit control has its own buffer. So is this the right approach to have my own buffer or are there ways I can share the buffer with it etc? The editor might not have more than 4MB worth of characters.
I need this to work on Windows 7 and XP, not keen on older ones.I use MFC.
Thanks for your help.
You don't need your own buffer and indeed it would be dangerous to have one since it will likely get out of synchronisation.
But you don't need to set the entire edit text at once. From the documentation:
Also, if an edit control is multiline, get and set part of the control's text by calling the CEdit member functions GetLine, SetSel, GetSel, and ReplaceSel.
ReplaceSel is what you are looking for I think. Although this text talks about multiline edit controls, SetSel, ReplaceSel etc. work fine with single line edit controls.

Indenting with white spaces, tabs, and how many spaces or the tab width [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I know that this is more of a coding style, instead of a one right way of doing things. But, I'm a bit frustrated if I came across different indenting formats.
But, I would like to hear the reasons by various people on these issues:
Do you use spaces or tabs? Tabs with spaces? Any difference with "Tab insert space", instead of using the space key directly?
How many spaces to indent each line of code? Why?
Does different code has different style that is more suitable for each of them?
Is there a way to "visually" indent code without actually writing the indent? So it won't ruin the original indenting? It seems unlikely.
I'm using Xcode, so it's better if you have advice for Xcode projects.
You should always use the coding style that the project is already using if you are modifying an existing project.
That being said, if you are able to choose your own coding style for a new project I suggest that you use tabs to indent code and not spaces -- here is why. By using spaces you force all of the other developers working on the project to conform to your indentation preference whether that be 2 spaces or 4 or 8 (or whatever). But by using tabs each developer can view the code using their own preference. You should uses spaces and not tabs to format text (to line up variable names one per line, or to line up multi-line comments) because that will work with any tab width preference. However, when you indent code use tabs not spaces. In short, indent with tabs because indenting with spaces is rude.
spaces because they render the same everywhere
indent-width is context-dependent. runaway indentation is bad. generally, languages where you cannot easily reset the indentation to sane levels through named abstraction items (functions or methods) need narrow indentation.
yes, see previous bullet
the claim that tabs allow individual developers to use different tab widths is false. consider
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
if this was produced with tabwidth of 2, it would look like this with tabwidth = 4:
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
conversely, if it was produced with tabwidth = 8 and displayed with tabwidth = 4, the result would be
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
so if a project style guide requires tabs and says function parameters need to align as in the first code display, then there's a single correct tab width.
(another answer presents the same argument.)
a long-overdue edit:
i wholeheartedly agree with the opening sentence of the accepted answer, which is
You should always use the coding style that the project is already using if you are modifying an existing project.
the rest of that answer is, excuse me, rubbish, and i tried to explain why above.
the question of indentation style should only come up at the very beginning of any project, once it's settled it's done. i do believe that spaces are better than tabs, and have (again, i believe) rational arguments in support of my position, etc. but i'm not going to start discussions on this topic because they're useless: most programmers i met support their preferences with irrational arguments, and the rest have settled for a style which works best with their tooling (like me: "spaces because they render the same everywhere"), which i have no influence over in general.
anecdote: i once worked on a project which underwent a change of newlines, from \r\n to \n, and it turned out to be a pain in the ass any time we needed to go past the revision in svn blame. s/newlines/indentation/, and you have a nice argument against massive indentation changes in a running project.
An advantage to spaces vs tabs is alignment when wrapping long lines. If you use tabs, no matter what you do, the lines below will most likely not align, unless the editor has the same settings.
For example:
result = variable_one + variable_two + variable_three +
variable_four;
If you use tabs, how to ensure that 'variable_four' will show up aligned if tab indentation changes?
There are different programming languages with very different syntax. The syntax is crucial in choosing a coding style. Especially the tab size (number of spaces) that you will chose will depend on how many levels you need to have. In HTML for instance there are many levels, and there it makes no sense to have a big tab size, it even makes no sense in using tabs.. you just use spaces. Another very important aspect is the IDE you use. In some IDE's you are encouraged to use tabs (in Visual Studio you have the nice facility to add/remove tabs to more than one line (TAB to add, SHIFT+TAB to remove) and so on.
Projects have different indent sizes because people use different indent settings (even in the same code) or because they use different editors or because the project includes code from other projects or even simply because people have different preferences or even different sized monitors.
I work in Visual Studio C++ and VB.NET, indent is tab based(3 lines) and tabs do not insert spaces.
I simply use the defaults of the editor I'm currently working in, but if I have the choice I use tabs because it means less invisible characters to manage. I have one editor that wholly manages indentation for me (REALbasic), another editor that manages indentation for me, allows for spaces as prefix but formatting gets wonky if I leave them in (Applescript), and there's Xcode.
I see 4 spaces more than another number for space-based indentation, so I go with that if I have to.
I've found that every community more or less has a standard that has been placed in example code, so I just go with that otherwise tabs then 4 spaces. Simple.
Honestly, I gave up on the indentation fight a long time ago and expend my brain power on other, what I feel are more important, code-related issues like good variable and functions names. Any reasonable code editor can help you manage poorly-placed or complex indentation but none of them can help with sorting out poorly-written or complex code.
Depending on your IDE and your "tab inserts spaces" option, it's a nice shortcut over using the spacebar. I.e. at 3 indentation levels with 3 spaces per tab, you press the Tab key 3 times instead of the spacebar 9 times. The net effect is the same - you inserted 9 spaces - but with 1/3 the number of keystrokes.
A benefit of using spaces instead of tabs can make printing your code a bit nicer, especially if you're using a non-typewriter style font. I usually use a width of 3 spaces for each 'level' of indentation.
Because I prefer proportional fonts at a specific 15pt size, I set a TAB to be 37 pixels.
I don't mind that you use FOUR SPACES as long as you also use a font where a SPACE is 37/4 pixels wide. Or TWO SPACES as long as you also use a font where a SPACE is 37/2 pixels. Or even THREE SPACES, as long as you also use a font where a SPACE is 37/3 pixels.
Otherwise, a TAB works great for both us.

Resources