views:

210

answers:

9

I have found myself designing a language for fun that is a cross between Ruby and Java, and as I work on the compiler / interpreter I find myself pondering using whitespace as a terminator, like:

class myClass extends baseClass
    function someFunction(arg)
       value eq firstValue 
       value2 eq anotherValue
    x = 2

The alternative would use a symbol or a word as a terminator like JavaScript's ";" and Ruby's "end."

function myFunction(arg){
   value = someVal;
}

With languages like Python seemingly preferred today, I wanted to see what the StackOverflow community thought about older style syntax. It seems much easier to write a parser for the older style syntax, so would it be better to stick to the Java / JavaScript style? If so why?

+2  A: 

Fundamentally, there's nothing wrong with using whitespace to format code. But honestly, is there really a one best way to format code? Why not leave it up to code formatter to come up with enforce style?

I find it amusing that for almost two decades there was consistent railing against the "bondage and discipline languages" for their lack of flexibility and mundane coding structures (pascal, cobol), yet we have seen them resurface in languages like Java and Python.

altCognito
+3  A: 

Given that any two people are as likely to disagree as agree on the amount of white space and indenting that makes the code readable, I'd suggest that whitespace is a poor choice of a contextual delimiter. I would make the language flexible with respect to whitespace rather than have the amount and placement of whitespace infer code organization.

tvanfosson
It seems that most people agree that basically the language should allow for an optional contextual delimiter, but also should use curly braces.
Heat Miser
+1  A: 

Flamewars aside, I prefer symbol using languages because they make code more readable and they are easier to maintain. The visual seperation by braces is a welcome delimiter of information and makes bugs easier to find when something will not compile if the braces don't match up, a line of code is not terminated by a semi-colon, et all. VB was a very annoying language that way simply because you had to be explicit in new line breaks in your code.

Wayne Hartman
I usually perfer them too, but I also like the @end in Cocoa for classes, and the end keyword in Ruby as opposed to the semicolon, although at times it does seem to make readibility an issue.
Heat Miser
A: 

Neither consistent indentation nor demarcation symbols (e.g. braces) completely replace the other. My personal preference is to have both.

That said, optional indentation does make one-liners easier (I think).

Lucas Richter
A: 

Scala has this, a semicolon is infered at the end of aline if it then becomes syntactically correct. I dislike it as it forces me to put the opening brace of a block at the end of the preceding line.

In Python I had problems when restructuring large chunks of code, as it was too easy to mess up the indentation and loosing the block structure.

starblue
+3  A: 

Since this is a very subjective question and there are so many styles out there, this is my taste:

Either "{}" JS style or "End Function", "end" VB style for blocks. Do anything but white space that's just freakin' annoying and reading the code is really hard. In my book white space or tab for that matter as a special meaning is just wrong.

Don't use a special terminator, I think new line is perfectly fine. You should think about "which case is the exception?". Putting an extra character at the end of every single line doesn't make a lot of sense, the exception is putting multiple statements in one line.

So use ";" as a separator to support multiple statements in one line but by default don't require it. That'd be my choice.

dr. evil
+ I write LL parsers for special languages, and this is pretty much what I end up doing.
Mike Dunlavey
+2  A: 

In .NET Rocks show #338, discussing "The Science of Great UI", Mark Miller highlights the principle of "smallest effective difference". (Illustrated in this blogpost.)

This implies that by adding unnecessary contrast, you are increasing the level of "noise" that you have to deal with. As I see it, from a "user interface" viewpoint, this means that using symbols to group code instead of white space represents an increase in the level of noise.

Ola Eldøy
one could argue, however, that the "noise" of a closing curly brace is actually signal rather than noise. The article you point to mentions the need for "visual elements that make a clear difference". I'd argue that a single visible character meets that criteria more than one or more invisible characters.
Bryan Oakley
And a solid thick black line makes a clearer difference between two cells on a spreadsheet than a subtle, grey one. Still, the principle is that of *smallest* effective difference. So the challenge is where to strike that balance.
Ola Eldøy
+1  A: 

White space/indentation is generally a style thing. As others have mentioned, as 3 people what spacing you should use, and you'll get 4 answers. I would definitely prefer a language construct (ie: {} or BEGIN/END).

As well, different editors may have different TABS defined. You may edit the code in your editor, with TABS set to 3, and the code would look fine. I may edit it in my editor, with TABS set to 8, and not understand the code. This limits the code to only being edited with specific tools, or makes setting up other tools to make a quick change painful.

abendigo
+2  A: 

I see lots of good answers. Let me just add another wrinkle.

When I'm writing code, this would appear to be a subjective issue.

However, if I'm writing a program that writes code, this can have a big impact.

In the past, I've had to write code-generators to generate Fortran 77, C, and C++.

Fortran 77 only allows one statement per line, lines are 72 characters long, the first 6 columns are reserved, comments start with a 'c' in column 1, continuation lines are marked with a character in column 6, etc. etc.

You can't believe how much I preferred generating C or C++, where whitespace is a matter of choice, not mandate.

Added: Also, I suppose preprocessor macros are out of favor in some camps, but they would be severely limited if whitespace had meaning.

Mike Dunlavey
But real macros work on the abstract syntax tree, not on the text representation :)
Damien Pollet
@Damien: I suppose C and C++ macros aren't "real", but they are useful, IMHO.
Mike Dunlavey