The Great White Space Debate

Published: 2015-05-13 ← Very, very old article! Take its ramblings with a grain of salt.

FRIDAY

for (int i=0; i<totalButtons; i++  

“Creeping Jesus, you fool! Are you out of your mind!? I have the Style guide right here prepared for an occasion such as this. It would be a mistake to debate this now. It would be truly dreadful for your ego.”

I quickly tabbed to Chrome and clicked on the Style Guide I keep linked in my main bookmarks toolbar. I was foolhardy before — inexperienced and unprepared for battle, but I have since learned to be ready for these kinds of brutal attacks on character.

“I’m telling you. That’s not Java standar—”

I shushed him with all of my might. No point in debating, yet. I have the answer right here. I’ll justify my white space use with a hefty and cheap appeal to authority.

The page loads and we both lean closer to the screen. Victory is stored somewhere on this website. Along with the gloating rights of one individual over another.

But there’s no mention anywhere. Panic sets in. Without a definitive source, a code style debate could burn uncontrolled for years. I can’t let another Tabs vs Spaces happen. God knows we’re still bruised enough.

I frantically Google for related search terms. “Java loop style guide,” “java white space guide,” “Java white space guide for loop” Nothing helpful is returned. Could it be that this issue is too minute to warrant a single comment on the whole of the internet (or approximately 3 Google searches)? No. No this must be solved. If not by others, then by me.

Quitting time is rapidly approaching, so we agree to put our debate on hold till the following week. But before we could move on as gentlemen, a vicious threat is issued by my opponent: “I will look into this more over the weekend,” he says.

Vile scum.



SATURDAY




I can’t relax. While I sit here, he’s out there getting stronger. Monday will be a massacre if I don’t prepare — the type of things that get’s CC’d to the whole team — a real public flogging. I can’t let it happen! Christ, I've got to find something! Something that no sane man would be able to argue against. But what?

Think, man! Think! I Google again: “Java style guide.” Page after page, and deep into the internet, the grim hours ticked by and I was no closer than when I started. But then it hits me: no style guide is needed. I have the whole of the java source at my finger tips. My answer has been right in front of me.

I quickly launch the java directory and open the first source file I see. I scan the text as quickly as I can for the first loop. At last!

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


Eureka! My silver bullet was right under my nose the entire time. I leaned back in my chair to soak in my victory. That poor brute. He has no idea what’s in store for him come Monday. However, my Euphoria quickly faded when another for loop at the bottom of the screen caught my eye. What’s this?

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


“The white space…”

I scroll a little further.

i = 0  


“Oh no…”







DESPERATION




That first file told a sour tale. By the time I made it to the bottom of the document the score read a grim 4 to 1 in the oppositions favor. But I was unshaken. “Just an outlier,” I convinced myself. The works of a disgruntled employee. Some lunatic with no taste for good style. Nothing more than noise in the data. Hell, we’d write this guy off in real statistics, for sure. It only confuses the real result that I know is hidden in there.

I need more ‘relevant’ data, but checking by hand isn't viable. The Java source files are far more numerous than expected.

Python:

  
import os  
import re  
from functools import partial  
  
def get_src_files(root, extension='.java'):  
  for root, _, files in os.walk(root):  
    yield from (os.path.join(root, f) for f in files  
                if f.endswith(extension))  
     
def read_files(filenames):  
  for filename in filenames:  
    with open(filename, 'r', encoding='utf-8') as f:  
      yield from f.readlines()  
  
def extract_forloops(lines):  
  yield from (  
      line.strip() for line in lines  
      if 'for (' in line  
      # One of those god forsaken 'enhanced' for loops. Ignore  
      and ':' not in line  
  )  
  
if __name__ == '__main__':  
  spaced_apart = partial(re.search, '[a-zA-Z]\s\=')  
  grouped_together = partial(re.search, '[a-zA-Z]\=')  
   
  java_src_path = r'C:\Program Files\Java\jdk1.8.0_20\src'  
  guava_src_path = r'C:\Users\Chris\Dropbox\JavaSources\guava-libraries'  
  gwt_src_path = r'C:\Users\Chris\Dropbox\JavaSources\gwt'  
  forloops = extract_forloops(read_files(get_src_files(gwt_src_path)))  
  
  counts = {'spaced': 0, 'nonspaced':0}  
  for loop in forloops:  
    if grouped_together(loop):  
      counts['nonspaced'] += 1  
    elif spaced_apart(loop):  
      counts['spaced'] += 1  
  print(counts)  


I’ll open every last source file, parse out every for loop, and finally — empirically — know which style is more in line with the “java standard.” I take a deep, self important, breath. Once I press this key, the argument ends forever.

RESULTS


“Dear god!” The inflated sense of importance I assigned to this task had suddenly and sharply disappeared. The only thing discovered was that I may be on the wrong side of history. This is looking like a brutal defeat. 86% of the Java source uses the padded style. Only 14% — my brethren — had the presence of mind to use good style.

Doom is on the horizon, but I still refuse to accept it. I mean, I’m a reasonable person, I have accepted that I’m wrong, but what I haven’t accepted is admitting it and losing the argument. These are two wholly different things. And I’m not above cheating. So I’m going to do what any rational man does when the data doesn't fit the message: Cherry Pick.

I grab a copy of GWT. It’s a well respected Google library. This is a good place to start.

I run the code analysis again.

RESULTS










Clearly an issue with the data set. 0.01? I‘m not entirely convinced that’s even a number, let alone a valid result for the data.

I jump to Guava’s source. Their devotion to clean code and good style is the stuff of legend. My confidence is still such that I figure I’ll be in good company. At the very least, it couldn't possibly get worse than 0.001.

 
I run the analysis.

 

...Processing

 


…….Processing

 



……….Processing



 







MONDAY



My coworker, with the smuggest face that has possibly ever existed: “Did you get a chance to look into the syntax thing over the weekend?”

“Wha?— N—No. Completely slipped my mind.”