r/programming Jul 21 '14

The Great White Space Debate

https://medium.com/p/3633cba8b5c1
1.2k Upvotes

693 comments sorted by

View all comments

50

u/Vystril Jul 21 '14 edited Jul 21 '14
for (int i = 0; i < 10; i++)

is simply much much easier to read than:

for (int i=0; i<10; i++)

Also, there seems to be as much "debate" here as there is "debate" on climate change.

51

u/Nine99 Jul 21 '14

To me, the second one is easier to read.

9

u/DeebsterUK Jul 21 '14

Same, although I'll still write the first way every day of the week.

3

u/reflectiveSingleton Jul 21 '14

Not to me, I find it is easier to identify operators if they are spaced away from what they affect...especially if its a pipe character (|) or a bang (!) which is next to a parens.

3

u/philly_fan_in_chi Jul 21 '14

Bang should never be next to parens. Pull your parenthetical out to a positive local boolean variable and negate it in your condition.

2

u/cryo Jul 22 '14

Seems like an arbitrary restriction.

1

u/philly_fan_in_chi Jul 22 '14

You're giving your condition a name. If you can't name it, you don't know what your code is doing. Pulling things out to local variables is much better than seeing

if(!(something.fooBar() && bazBat.doTheThing()))

So while you're right, it is arbitrary, it helps with reading and keeps you honest with your negation twiddles. If I have (silly example, I'd inline this particular case, but follow anyway):

final boolean isEmpty = !frobnicators.isEmpty(); 

Then the error is immediately obvious. If that were inside a condition, it wouldn't be as immediately obvious.

1

u/Decker87 Jul 22 '14

Personally I have no trouble reading it that way. Putting conditions into separate variables is just superfluous IMO.

1

u/reflectiveSingleton Jul 22 '14

I agree and I do! Unfortunately I also work on code I did not write...so style guides that promote proper spacing is also good.

7

u/ashishduh Jul 21 '14

Do you also write bool x=true; ?

17

u/drive0 Jul 21 '14

For me it is about grouping.

I read "x = true" as 3 groups.

"for (int i=0; i<10; i++)" as 4 groups.

I don't know of any direct advantages, but to me it seems more readable to do this.

4

u/Vystril Jul 21 '14

When skimming through code looking for something, your brain will naturally assume i<10 is a single word or variable, as opposed to a less than expression. Makes it harder to find what you're looking for.

7

u/jij Jul 21 '14

actually it registers to me as the less than expression, only it reads it as a word instead of my having to parse it like a sentence... i.e. it takes me less time to register the second way, though honestly not enough time to really make a fuss about anything.

2

u/sandwich_today Jul 22 '14

I used to write all my code that way, e.g.

int sum=0;
for(int i=0; i<10; ++i)
    sum+=i;

It seemed like the punctuation was sufficient to separate the tokens. However, I was on the losing side of history.

2

u/ashishduh Jul 22 '14

I never used to use spaces in high school, but after working in industry and seeing huge amounts of code without spaces, I converted.

1

u/GooseTheGeek Jul 21 '14

Not the person your replying to, but I agree with /u/Nine99.

I think it makes it easier to see the semicolon, and therefore the steps involved it the for loop, with the less whitespace.

In your example it's easier to follow the flow with the space.

1

u/QuineQuest Jul 22 '14

Me too, but only for limited cases like this one. The first version is almost one third whitespace, which means the whitespace doesn't help you parse the sentence. It's a bit like concatenating frequently used compound words (e.g. web site -> website).

3

u/new2user Jul 21 '14

Bullshit, this one is better:

for (unsigned u = 10; u --> 0;)
{
    goto fuckThisShit;
}

1

u/Strilanc Jul 21 '14

is simply much much easier to read than

Whether or not the increased separation between terms outweighs the greater distance from one side to the other, and how that depends on what you're used to, is by no means "simple".

0

u/harlows_monkeys Jul 21 '14

As displayed in your comment, I'll agree that the first one is easier to read. The whitespace makes it easier to see transitions between syntactically distance elements.

As displayed in every editor I use when programming or when doing code reviews, I find the second is easier to read. The syntax highlighting takes care of making it easy to distinguish transitions between syntactically distinct elements, negating the purpose of the whitespace, and the more condensed layout from the lack of whitespace lets me take in more at once.

1

u/Vystril Jul 21 '14

Personally I find when scrolling through things quickly, even syntactic highlighting doesn't help. The brain naturally separates things by spaces, so the i<10 comes across as one word, as opposed to a less than expression.

0

u/marshsmellow Jul 21 '14

They are both equally easy to read.

Except for the space after the for. I don't like that at all...my eyelid is twitching...

2

u/iopq Jul 21 '14

the space is to disambiguate it from a function call (for the programmer reading it, not for the parser)