The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Whitespace: Just say No

After a brief discussion with the client's Lead Architect ("Every operator will have exactly *one* space before and after..."), I've realized something...

Source code should be devoid of any type of whitespace.  No carriage returns, no spaces, no tabs.  It should be the job of the IDE to render the source as the developer sees fit.

Immediate benefits include smaller files, easier diffs, and an absence of trivial changes showing up in file compares.  Of course, this means that file compare utilities will have to figure how to format and render source code.

Your thoughts?
Caffeinated Send private email
Wednesday, March 09, 2005
 
 
Should be an interesting work experience.
Fred
Wednesday, March 09, 2005
 
 
It is the job of the IDE. Visual Studio does it if you use Visual Basic.  Tools, Options, Text Editor, Basic, VB Specific.  Set the pretty listing option on (it's the default)

Code is automatically spaced and indented. Blocks are automatically indented, variables are cased to match their declaration, etc.
Joe Griffith Send private email
Wednesday, March 09, 2005
 
 
What about whitespace in comments?
comp.lang.c refugee
Wednesday, March 09, 2005
 
 
SomeofthatsoundslikeadecentideabutI'mabitskepticalofthenospacesidea.
Kyralessa Send private email
Wednesday, March 09, 2005
 
 
And just how does the compiler figure out what to do.
not my name
Wednesday, March 09, 2005
 
 
You need some kind of separator between operators and arguments? How else can the compiler parse the code?
DJ
Wednesday, March 09, 2005
 
 
Oops.  Good point.  Some spaces would have to remain, like after member types.

"string s=null;"
Caffeinated Send private email
Wednesday, March 09, 2005
 
 
Operators and arguments?

You mean like "x=y+z;" or "if(x<10){foo();}"?
Caffeinated Send private email
Wednesday, March 09, 2005
 
 
Oh!  I forgot about VB not using a semi-colon to end a line.  I'm talking C#, Java type languages.
Caffeinated Send private email
Wednesday, March 09, 2005
 
 
So where spaces are required, only one space would be needed.

This reminds me of how a language lexer works -- A C compiler squeezes out all the spaces during tokenization before the source is parsed.

It should be pretty easy to write a tool that essentially squeezes out all the unnecessary spaces from a source file.  The rules are simple: if there is spaces around a non-alphanumeric character, remove them.  Any other spaces convert to a single space.

Pretty printing the source in reverse actually the hard part but there are already tons of tools to do that.
Almost Anonymous Send private email
Wednesday, March 09, 2005
 
 
Another drawback.  You would be forced to use /* */ style comments.  Otherwise, how would you know when the // comments end?

I could live with it.

/// comments could be worked around, as it is an XML snippet.
Caffeinated Send private email
Wednesday, March 09, 2005
 
 
The purpose of any coding convention is to make reading the other guy's code easier. It doesn't matter what convention you follow, you'll get used to it. Worry about more important things like requirements or design. (and any decent file comparison utility has an "ignore whitespace differences" option).
Anony Coward
Wednesday, March 09, 2005
 
 
Write it however the heck you want and run it through a source code formatter before delivering it to the client.
Nate Silva Send private email
Wednesday, March 09, 2005
 
 
Pythonistas are screwed with no white space.

My IDE formats my code now anyways.
son of parnas
Wednesday, March 09, 2005
 
 
Remember that you won't always have a fancy IDE everywhere you need to look at code.

If you use CVS and a tool like CVS Web it is nice to have reasonably formatted code when looking for changes to a specific bit of source.

You could argue that all of the tools you use should understand source, but, I won't hold my breath.
White Space is your Friend Send private email
Wednesday, March 09, 2005
 
 
Maybe a version control system that stores code as object hierarchy and checkouts stuff formatted differently according to each developers preference.

Also there has been discussion about making an "underlying" programming language in XML, that each developer could edit as their favourite programming language: http://www.third-bit.com/~gvwilson/xmlprog.html
Samuel
Wednesday, March 09, 2005
 
 
One language that worked without whitespace was called Fortran, and it's not good.

"Language words separated by whitespace" is one of those simplifying concepts. We use it to make sentences.  It simplifies the parsers of Pascal, C, C++, Java, Ada, VB, etc. 

We can use extra line-feeds to indicate new thoughts, or a new slant at an old thought.  In programs, we can use extra line-feeds to set-off 'clumps' of processing.

I think this is the old baby-and-bathwater scam.  "Getting agreement on how to use whitespace is hard.  Let's get rid of it!".  This argument ignores all the good things you lose when you do this.  Very very dumb idea.
AllanL5
Wednesday, March 09, 2005
 
 
I would fire you. No serious. I do not want half-clever tools to mess with my carefully crafted layout automatically and without my consent.

There is only so much a pretty printer can do. There is more spacing, than just the spaces in the left margin. Manual prettifying is essential for good maintainable code, although automatic prettfication can provide the first cut.

Storage overhead is a non-issue. Source code control and file diffing are, but hey, use a tool that removes duplicate spaces before running such tools.

You approached the problem exactly from the wrong end.
Karel Thönissen Send private email
Wednesday, March 09, 2005
 
 
Great idea, but Allan is right about the blank lines.
Alex Send private email
Wednesday, March 09, 2005
 
 
>  I would fire you.

Your carefully crafted code must be really something to ruin someone's life over.

The idea that a program is just data and should be rendered according to a style sheet is an old one and valid one. It just hasn't been terribly practical until lately. I am not a big egoless programming fan, but a little less ego could be useful.
son of parnas
Wednesday, March 09, 2005
 
 
"Maybe a version control system that stores code as object hierarchy and checkouts stuff formatted differently according to each developers preference."

I've thought about this idea quite a bit over the years and tinkered with it, and it would save an awful lot of irritation caused by looking at other peoples insanely formatted (to you..) code.

But at the end of the day there are a lot more significant problems to invest all that energy in (eg. how your database schema is organized, etc.).
NetFreak Send private email
Wednesday, March 09, 2005
 
 
>>>The idea that a program is just data and should be rendered according to a style sheet is an old one and valid one. It just hasn't been terribly practical until lately. I am not a big egoless programming fan, but a little less ego could be useful.
son of parnas

Style rules, company-wide, and egoless programming all appeal to me. We practice this in our company rigorously. From the source code alone here one cannot see who had written it. Everyone here is convinced that the style rules make sense. They have proven themselves over time, yet were always adapted when necessary.

Only too bad that automatic style sheets do not work. There is too much information in column wise mark-up. In reviews errors are discovered because lack of symmetry is easily spotted. Show me one tool that will take care of horizontal spacing spacing in source code. I am not talking about simple indentation.

Automatic prettifying *decreases* the readibility of carefully crafted source code. That is any code that meets our style guide, not just my code written to my personal taste. Given this style guide using a prettifier not only deteriorates code quality it also raises false alarms in source control and diffing. Break these rules too often and you are out.

So someone not aggreeing with the style rules should either comply or explain why the other apporach is better.

BTW in other reasons to use spaces in source code, even in places where it is not strictly necessary for the compiler, is that it can facilitates systematic searching with simple tools. Using unnecessary spaces, it is straightforward to search for all occurences of object 'a' wihout getting too many false alarms.
Karel Thönissen Send private email
Wednesday, March 09, 2005
 
 
>  Show me one tool that will take care of horizontal
> spacing spacing in source code. I am not talking
> about simple indentation.

As spacing rules are normally tied to grammar you should be able to get pretty close to what you want. After that you need to be less picky. Perhaps make simpler code that doesn't need such fancy kerning to be readible.

The eclipse formatting, for example, isn't my style, but the benefits of using outweigh (mostly) my irratation.
son of parnas
Wednesday, March 09, 2005
 
 
Example of automatic inter "word" spacing.
Look at the SQL formater in Access, you drag and drop objects which make a 'text' sentence where the words are spaced according to its rules.
 
If you saw code as a bunch of objects in sequence, then it would be easy to format those objects based on a set of rules.
 
The problem such a system would create - as in MS Access - is that it takes a long time to fiddle with objects visualy and it makes it difficult to complex things.
It is alot quicker to type (when you can type well and know what your working with), with intellisence you can start typeing and have help finishing it, about the only useful time for a visual list (IMNSHO).

Wednesday, March 09, 2005
 
 
>> "Also there has been discussion about making an "underlying" programming language in XML, that each developer could edit as their favourite programming language."

Show me a way to represent everything you can do in Perl in Java, and everything you can do in Java in Perl.  Now prove that it also displays perfectly equivalent *and* idiomatic code when told to display Haskell.

Heck, even among languages compiling to .Net, there are things you can write  in Vb.Net that you can't write in C#, and vice versa - and that's languages that share the same libraries and execution environment!  How the hell do you propose to translate automatically between languages that weren't even designed on the same continent, let alone in the same building?
Iago
Thursday, March 10, 2005
 
 
"It should be the job of the IDE to render the source as the developer sees fit."

"Immediate benefits include smaller files, easier diffs, and an absence of trivial changes showing up in file compares.  Of course, this means that file compare utilities will have to figure how to format and render source code."

"Your thoughts?"

ABSOLUTELY CORRECT.
Peter Sherman Send private email
Thursday, March 10, 2005
 
 
1.  "As spacing rules are normally tied to grammar " -- Since WHEN?  If spacing rules were tied to grammar, we wouldn't HAVE all these 'style' spacing arguments -- there'd only be one way to do it.

Now, "one-or-more" spaces between language tokens IS a grammar rule -- but that's why we NEED spacing style, and that's why we can USE spacing style to add more meaning than is strictly in the grammar.  Grammar is Syntax, but it's Symantics (meaning) that we (as humans) need to be able to get out of the code.  That's how we verify that the code does what we intended it to.

2.  When going through a design review, with more than one developer present, it is CRITICALLY important that everybody be reviewing the same thing, looking the same way.  This includes spacing, page numbering, file names, open/close paren positioning, and indents.  Otherwise, when person A is pointing out a bug on line 155 (on page 3), everybody else has to flip pages to find what line and page THEIR copy has that bug on.  A BIG waste of time.

3.  Having more than one way to do it is critical to the programmer's ability to indicate what things belong with what other things.
AllanL5
Thursday, March 10, 2005
 
 
>  there'd only be one way to do it.

By that i meant your personal style sheet an be based on grammar, not that it is implicit in the grammar.


> When going through a design review, with more
>than one developer present, it is CRITICALLY
>important that everybody be reviewing the
>same thing, looking the same way.

Review software i have built formats the code and presents to all reviewers so it looks the same to everyone.
son of parnas
Thursday, March 10, 2005
 
 
In college I knew a guy who used 2 space tabs. Most of his variables were 1 chars. Is this okay? Well, if it's run once, throw away crap--I guess yeah. However if you have any suspicion you'll come back to the code a week from now, and you are looking at more than more than 50 lines of code--you gotta be kidding yourself if you don't just go full out documenting it and coding it right.
Li-fan Chen Send private email
Thursday, March 10, 2005
 
 
This reminds me of old and ongoing arguments about when to use hard tabs vs. using the equivalent number of spaces. And about arguments for and against HTML email...

Hmmm. No spaces except those that are required by the language, huh?

My $.02 is that those who don't know history are probably doomed to repeat it. There are tons of good reasons to use whitespace in computer source code, not the least of which has to do with the way most of us were taught to read and write. I've always marveled at the fact that, given an opportunity, we'll always find a way to have more than one way to do the same thing. As far as having different programming languages goes, this might be a good thing, but as far as presentation goes, I don't want anyone challenging my ability to read their crap. Present the stuff as if you want someone else to read it. Sheeeeeeeeeeeesh! Source code prepared in modern programming languages is NOT for the benefit of the compiler/interpreter -- it's for people. Unless, of course, you're only talking about stuff that you'll never show to anyone.

I'm also confused as to what problem is being solved by not using the spacebar or the tab key. Help? Anyone?

I agree with one of the other posters...I'd fire your ass too for not knowing why some things are done the way they are. Go be innovative elsewhere, please.

Schools these days. Hah!

Silly Me
Silly Me
Thursday, March 10, 2005
 
 
+1 Very, very dumb idea.

So, every time I want to do something interesting with _my_ source code I guess I'll call some API into the IDE.

Yeah, right.


See also [humor]
http://www.research.att.com/~bs/whitespace98.pdf
Abstract Typist Send private email
Sunday, March 13, 2005
 
 
"benefits include smaller files,"

By a trivial amount.

"easier diffs, and an absence of trivial changes showing up in file compares."

You can do that now -- lots of people have checkouts/checkins automatically run their code through 'indent' or some other pretty-printer.

Didn't Joel say that the Excel team had *no* guidelines at all, and that it was a *good* thing?  I can't say I've ever come across a style so bad that the intent was hidden.  I'll side with Joel on this one.  The problem isn't bad style guidelines, but bad code.

Friday, April 01, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz