FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Tabs vs. Spaces






Tabs or spaces?
Tabs
50%
 50%  [ 5 ]
Spaces
50%
 50%  [ 5 ]
Total Votes : 10

Peterssidan
What do you prefer to indent your code? Spaces or tabs?

I always used tabs but after I worked on a project that used spaces I have used spaces since then. Lately I have considered going back to tabs but I'm not sure.
Fire Boar
Always two spaces. Four spaces is overkill, especially when you have a lot of nested blocks, while one space is unclear. Two spaces is a very nice balance between clarity and compactness.

The problem with tabs is that it's inconsistent. Some people have tab width set to 4, some to 2, some to 8... what looks right for one person looks wrong for another. Especially if you mix tabs and spaces, a practice which is quite common if, for example, a line runs on a long way and you want to break it up a bit.
Marcuzzo
I use 2 spaces aswell.
most of my coding is done in scite and I have my indentation settings set like so:
Code:
# Indentation
tabsize=2
indent.size=2
use.tabs=0

This way I can use the Tab key when typing code and when the file is opened in, let's say, notepad, it doesn't completely mess up the indentation
davidv
Tabs and spaces aren't something I worry much about. It's faster to not use spaces (unless you decide to change the default, to which I say, it's still faster not to use spaces).

Noobie coders tend to use spaces more often due to their lack of understanding in basic visual concepts such as the indentation of code to indicate control flow. Just sayin'.

If you're implying that you spam the space bar to indent then I say, you're wasting time and should just use tabs. Loss of productivity, my friend. Think of an iteration that runs in O(n^3) complexity. Good luck spamming your space bar. If you've decided to change the default setting on whichever text editor you use then I don't see a problem with 4 or 2 spaces. However, 8... you're doing it wrong.

Alignment of assignment operations when you have several decorations for a significant contiguous set of lines... waste of time.

Having a brace after your conditional expression to encapsulate a body code, stupid. You're wasting an extra line of code, and for what? Clarity? Surely you can distinguish each brace's significance. IDEs practically do it for you.

More comments than the amount of code, am I writing a report or am I programming? Not to mention, stupid comments /* This line returns the value */, /* Loop through 10 elements in an array */. Awful.

All this said, at the end of the day this doesn't matter (unless you're the poor bastard that has to maintain the code). What's important is your program's time complexity and as long as your code is clearly laid out with helpful comments and meaningful variable names there shouldn't be such a fuss about this crap. FOCUS ON THE LOGIC not the silly formatting.
Fire Boar
davidv, I must respectfully disagree with everything you have written in that last post.

The problem with tabs is inconsistency. Spaces are always consistent. Also, any good text editor will allow you to use the tab key to indent the selected or current line(s) by a user-defined number of spaces, with shift-tab unindenting the line(s). This is very standard. Also standard is automatically indenting a line to match the previous when you start a new line. So indenting by 8 widths using 16 spaces is no problem at all, especially since your last line will have itself been indented 7, 8 or 9 widths.

Alignment of assignment operations tends to look much neater, especially when the result of the assignment is similar (e.g. the result of a method with a slightly different parameter in each case). It can also make it easier to track down bugs. It's not an essential formatting style, personally I don't use it unless I'm writing code in a project where it is used, or unless it is actually clearer.

Adding a brace on its own line? It's a matter of preference, nothing more. Who cares if it's one more line? Lines in source code don't matter at all - it's compiled exactly the same, and spreading lines out in a logical way is easier on the eyes.

As for comments, I'd much rather see code which comments on every little detail than code with no comments at all. By far.

The whole point of all of this "silly formatting" is to make your code easier to read and understand, for everyone. That doesn't sound silly at all to me.
jcreus
Peterssidan wrote:
What do you prefer to indent your code? Spaces or tabs?

I always used tabs but after I worked on a project that used spaces I have used spaces since then. Lately I have considered going back to tabs but I'm not sure.

Tabs are really inconsistent and many times incompatible with Windows. I tend to write in Linux, but if I have to port my software to Windows, it can be really compex...

Plus spaces are always the same and not-so-editor/platform dependent.
davidv
Hahaha. Express an opinion and expect to be disagreed with. I love the forums. Fireboar, I have to disagree with what you have said too. Laughing

We all have different coding styles and it really depends on what works best for us (BLAH no). I've never had a problem with intrinsic documentation, my friends and colleagues have all commented on my style and have said it's perfectly readable without spending that extra time on silly aesthetic changes.

Quote:
The problem with tabs is inconsistency.


I'm not quite sure with what respect you are talking about with concerns of inconsistency when using tabs. If you're talking about using different text editors, I agree. There are inconsistency (sometimes) but the only text editor I ever use is vim and we all know, vim is god Smile There's a simple rule I follow when it comes with indentation, that is (best shown by example)

Code:
def findall(file_name, email_table, ph_table, web_table):
    for line in open(file_name, 'rU'):
        # Do not include incomplete emails e.g. <d...@pretaweb.com>
        # Strickly speaking '...' has no technical meaning
        match = re.finditer('[a-zA-Z0-9\-_\.]+[^\.\.\.]@[a-z\.]+\.+[a-zA-Z\.]+', line)
        for email in match:
            email_table[email.group()] += 1


Code inside functions = 1 tab
Code inside classes = 1 tab
Code inside iterations = 1 tab
Code inside binary selection = 1 tab

Text editors sometimes automatically do this for you and from experience the default is 1 tab hence why I've used tabs. Using my 4th finger from my thumb is much faster than using my thumb or index (spaces).

Quote:
Alignment of assignment operations tends to look much neater


Alignment of assignment operations do look neater but it's something that you might do at the end, after you've finished that function or that class that's bugging you. It's not something you'd waste time on at the start or during the middle of writing that piece of code is what I'm saying.

Once I've finished a project, I don't go back on it other than to increase performance and maybe add some useful comments in areas where I thought might need them. Of course with addition to boring documentation.

Quote:
Adding a brace on its own line? It's a matter of preference, nothing more. Who cares if it's one more line? Lines in source code don't matter at all - it's compiled exactly the same, and spreading lines out in a logical way is easier on the eyes.


Well, this thread was talking about the aesthetics of our code so I thought I'd poke the ogre and begin another war. It's not about compiled time, it's about scrolling your mouse wheel.

Quote:
As for comments, I'd much rather see code which comments on every little detail than code with no comments at all. By far.


This. I strongly disagree with this and I have much to say about it too. However, I have an algebra exam on Monday and I'd really like to get back to study so I'll leave this for another day. All I want to say is. I hate shit like this:

Code:
   public static void fixInput(String in, HashMap<Integer,Integer> map){
      // Splitting a string on a space and making it an array
      String[] separated = in.split(" ");
      // Looping through all the elements in the array I've just created
      for (int i = 0; i < separated.length; i++){ // start of the loop
         try { // beginning of the try block
            // Converting it into an integer
            int x = Integer.parseInt(separated[i]);
            // If the hashtable already contains the integer
            if (map.containsKey(x)){
               // Updating the k, v by adding 1 to the v
               int y = map.get(x) + 1;
               // Inserting it back to the hashtable
               map.put(x, y++);
            // Else if it's not in the hash table
            } else {
               // Making a new entry and putting it in the hashtable
               map.put(x, 1);
            } // end of the try block
         // Catching the exception
         } catch (Exception e){ // start of the catch block
         
         } // end of the catch block
      } // end of the loop
      // end of the function
      return;
   }


And whoever argues that looking at that is easier than looking at this:

Code:
   public static void fixInput(String in, HashMap<Integer,Integer> map){
      String[] separated = in.split(" ");
      for (int i = 0; i < separated.length; i++){
         try {
            int x = Integer.parseInt(separated[i]);
            if (map.containsKey(x)){
               int y = map.get(x) + 1;
               map.put(x, y++);
            } else {
               map.put(x, 1);
            }
         } catch (Exception e){
         
         }
      }
   }


There is something wrong with you.
Peterssidan
Wow this topic is now about much more than just indentation.

One reason why I didn't like tabs was that if you use different tab size it easily gets misaligned. What I didn't realize was that if tabs are used only for indentation and spaces are used for other formatting there is no problem. Using tabs different programmers can use whatever tab size they want, so for that reason I will prefer tabs in the future.

About the comments, I think it's important to have comments describing things that are not obvious and make the code more easily understood. Too many comments is not good either because it gets hard finding the important ones.

davidv wrote:
Alignment of assignment operations do look neater but it's something that you might do at the end, after you've finished that function or that class that's bugging you. It's not something you'd waste time on at the start or during the middle of writing that piece of code is what I'm saying.
I have to disagree. It is much easier if you make the code nice and tidy from the start. Otherwise the code easily turns into a mess that you don't want to touch when you got it working. Pity if you later want to change something. Alignment of assignment is maybe not the most important thing but if it's not done from the start it will never get done.

davidv wrote:
Fire Boar wrote:
Adding a brace on its own line? It's a matter of preference, nothing more. Who cares if it's one more line? Lines in source code don't matter at all - it's compiled exactly the same, and spreading lines out in a logical way is easier on the eyes.
Well, this thread was talking about the aesthetics of our code so I thought I'd poke the ogre and begin another war. It's not about compiled time, it's about scrolling your mouse wheel.
This is just ridiculous. There are faster ways to scroll than your mouse wheel if you want to scroll large amount of text. Personally I prefer to have the opening brace on it's own line because that helps my eye find what opening and closing braces belong together.
davidv
Peterssidan wrote:
I have to disagree. It is much easier if you make the code nice and tidy from the start. Otherwise the code easily turns into a mess that you don't want to touch when you got it working. Pity if you later want to change something. Alignment of assignment is maybe not the most important thing but if it's not done from the start it will never get done.


If you have difficulty distinguishing which value is assigned to which variable then... okay. Laughing

My worries are more concerned with the implementation's complexity, not the alignment of variables... If I have a chain of thought, I'd rather follow that chain, type it up than stop. Change my cursor position. Press my space bar a couple times and perfectly align my assignments.

Quote:
This is just ridiculous. There are faster ways to scroll than your mouse wheel if you want to scroll large amount of text. Personally I prefer to have the opening brace on it's own line because that helps my eye find what opening and closing braces belong together.


It was a joke, my friend. I thought "poke the ogre" would have gave that away /sarcasm

Although I have to say, I've never had a problem with braces (not knowing which one closing goes with which opening).

I take it you're opposed to lines such as these?

Code:
a = b = c = d = null;
public int blah { return blahblah; }
if (condition) return


How about not having braces when there's only one line (I have a few friends that are quite picky on this). Or maybe having a return at the end of every function when you don't need it but just want it for the sake of being explicit?
Peterssidan
davidv wrote:
Peterssidan wrote:
I have to disagree. It is much easier if you make the code nice and tidy from the start. Otherwise the code easily turns into a mess that you don't want to touch when you got it working. Pity if you later want to change something. Alignment of assignment is maybe not the most important thing but if it's not done from the start it will never get done.

If you have difficulty distinguishing which value is assigned to which variable then... okay. :lol:

My worries are more concerned with the implementation's complexity, not the alignment of variables... If I have a chain of thought, I'd rather follow that chain, type it up than stop. Change my cursor position. Press my space bar a couple times and perfectly align my assignments.

I guess my answer got a bit out of proportions. The lack of assignment alignment will not cause messy code on their own. It was more about not leaving all formatting until later or it will become a mess.

davidv wrote:
Quote:
This is just ridiculous. There are faster ways to scroll than your mouse wheel if you want to scroll large amount of text. Personally I prefer to have the opening brace on it's own line because that helps my eye find what opening and closing braces belong together.

It was a joke, my friend. I thought "poke the ogre" would have gave that away /sarcasm
I wasn't familiar with the expression poking the ogre.

davidv wrote:
Although I have to say, I've never had a problem with braces (not knowing which one closing goes with which opening).

It's not really a big problem and it's more about preference than anything else.


davidv wrote:
I take it you're opposed to lines such as these?

Code:
a = b = c = d = null;
I think this one is fine. It is clear that you set all 4 variables to null.

Code:
public int blah { return blahblah; }
I don't know what I think about this. It feels a bit wrong making the function 1 line instead of 4 (or 3 depending on the style used).

Code:
if (condition) return
I always use braces because that make it more consistent and minimises the risk of mistakes. For me it's not that important to only have one return at the end, if it is not needed I will not add one. If I have a function like this
Code:
if (x)
{
    return a;
}
else
{
    return b;
}
I sometimes leave out the else part and just write return b; after the if statement, but I am not at all sure what is more clear. What do you think? (Imagine the function is complicated enough so you don't want to write it on one line like return (x ? a : b);)
jcreus
Peterssidan wrote:
davidv wrote:
Peterssidan wrote:
I have to disagree. It is much easier if you make the code nice and tidy from the start. Otherwise the code easily turns into a mess that you don't want to touch when you got it working. Pity if you later want to change something. Alignment of assignment is maybe not the most important thing but if it's not done from the start it will never get done.

If you have difficulty distinguishing which value is assigned to which variable then... okay. Laughing

My worries are more concerned with the implementation's complexity, not the alignment of variables... If I have a chain of thought, I'd rather follow that chain, type it up than stop. Change my cursor position. Press my space bar a couple times and perfectly align my assignments.

I guess my answer got a bit out of proportions. The lack of assignment alignment will not cause messy code on their own. It was more about not leaving all formatting until later or it will become a mess.

davidv wrote:
Quote:
This is just ridiculous. There are faster ways to scroll than your mouse wheel if you want to scroll large amount of text. Personally I prefer to have the opening brace on it's own line because that helps my eye find what opening and closing braces belong together.

It was a joke, my friend. I thought "poke the ogre" would have gave that away /sarcasm
I wasn't familiar with the expression poking the ogre.

davidv wrote:
Although I have to say, I've never had a problem with braces (not knowing which one closing goes with which opening).

It's not really a big problem and it's more about preference than anything else.


davidv wrote:
I take it you're opposed to lines such as these?

Code:
a = b = c = d = null;
I think this one is fine. It is clear that you set all 4 variables to null.

Code:
public int blah { return blahblah; }
I don't know what I think about this. It feels a bit wrong making the function 1 line instead of 4 (or 3 depending on the style used).

Code:
if (condition) return
I always use braces because that make it more consistent and minimises the risk of mistakes. For me it's not that important to only have one return at the end, if it is not needed I will not add one. If I have a function like this
Code:
if (x)
{
    return a;
}
else
{
    return b;
}
I sometimes leave out the else part and just write return b; after the if statement, but I am not at all sure what is more clear. What do you think? (Imagine the function is complicated enough so you don't want to write it on one line like return (x ? a : b)Wink


The style of braces I like the most is the one like:
Code:

if (condition) {
  do whateveR;
} else {
  do something else
}

I find it quite elegant.

I tend to write the return inside the Else, that way it is more clear to me.

And I love completely Python's list comprehensions.

return True if x not in [y**2 for y in range(0,10)] else False

or even more complex things. Maybe not readable, but it is something I feel good when I achieve in a line of code,
davidv
I tend not to have a 'return' in my 'else'. I think it's more tidier and easier on the eyes. Plus, it saves few extra lines Smile

Code:
/* setting the children of a node */
public void setParent(ThreeNode<E> v) { parent = v; }
public void setLeft(ThreeNode<E> v) { left = v; }
public void setMiddle(ThreeNode<E> v) { middle = v; } // null in a 2-node
public void setRight(ThreeNode<E> v) { right = v; }


At first I thought this was terrible. Having a non-anonymous function in a single line but after a while, I've found it much more readable than if I were to have it on separate lines (especially when you have 30 - 40 getters/setters methods).

Speaking of Python range() function. I only use range() for when I'm building lists. It's much more memory efficient to use xrange().
Fire Boar
davidv wrote:
Code:
/* setting the children of a node */
public void setParent(ThreeNode<E> v) { parent = v; }
public void setLeft(ThreeNode<E> v) { left = v; }
public void setMiddle(ThreeNode<E> v) { middle = v; } // null in a 2-node
public void setRight(ThreeNode<E> v) { right = v; }


At first I thought this was terrible. Having a non-anonymous function in a single line but after a while, I've found it much more readable than if I were to have it on separate lines (especially when you have 30 - 40 getters/setters methods).


This is where C# innovates. Such a shame it's not open, because C# is a genuinely good language.

Code:
private int size = 0;

public int Size
{
  get { return size; }
  set { size = value; }
}

public boolean Empty
{
  get { return Size == 0; }
}


C# properties are a great invention, and this system makes writing getters and setters really neat. You can (if you like) write more complicated methods for getters and setters, but for obvious properties, just the simple thing will do. Indeed, the above could be shortened to this:

Code:
public int Size { get; set; }

public boolean Empty
{
  get { return Size == 0; }
}


This is the same as providing public access to a member variable, except that if you ever need to do more with a getter or setter, you can rewrite the code to do so without changing the interface.
Related topics
Philosophy
[soft]Firefox más inseguro que IE
Windows Vista Beta 1
Mac vs Linux
Smackdown Vs Raw 2006
layout spaces.
EXPLOSION AT CRICKET MATCH!(ENGLAND VS INDIA)
science vs. religion
Football Manager 2006 vs. Fifa Manager 2006!
Far Cry vs. Half Life 2
Buying Link Spaces
Visual Basic Tutorial - File I/O in VB: Text Files
Thumbnail Generation !
XPad -- A Desktop Note-Taking Tool
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.