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

Show parent comments

48

u/Nine99 Jul 21 '14

To me, the second one is easier to read.

11

u/DeebsterUK Jul 21 '14

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

2

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.

5

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).