[Grace-core] Syntax: Blocks and indentation

Michael Kölling M.Kolling at kent.ac.uk
Mon Nov 8 03:19:51 PST 2010


Sorry for going back to basics here (feel free to tell me to shut up if you've been over this too often already).

In short, I don't like the decision to be able to use brackets and/or indentation for grouping. I have several reasons.

1: The language should be optimised for readability. And that means readability of someone reasonable used to reading Grace. Not writability. Not convenience of someone converting from another language. 

For readability, having two entirely different ways of writing the same thing adds cognitive load. I may use one style myself, and a program written by someone else then looks unfamiliar to me if they use a different style. Allowing arbitrary alternatives reduces readability.

2: It's bad for teaching. It is always nice if I can tell my students "This is how you do it." And then have the compiler back me up. I hate having to say "This is how we do it here, but the compiler would also allow that way. But we don't like that because of X. And our style guide here says Y. But when you read code from other people you may see Z." Messy. 

I have this, for example, with public fields in Java. We very much tell people not to use them. A much simpler example than the layout question here. And do all students get it? No.

3: The wiki doesn't define what a level of indentation is. Can I choose? I.e. is 

while 
     file.hasNext
   do 
     println(file.readLn)

the same as

while 
  file.hasNext
 do 
  println(file.readLn)
?

If is is, then what about this one:

while 
        file.hasNext
    do 
   println(file.readLn)

That is, what if I half back up in my indentation? Is that still indented? Or not? Or is that a syntax error?

4: What happens if I combine indentation and brackets:

while 
     file.hasNext
   do {
     println("Hello") 
     println(file.readLn)
   }

I assume that this is fine. But how about this:

while 
     file.hasNext
   do {
     println("Hello") 

println(file.readLn)
}

What takes precedence: indentation of brackets? And what does that do to readability? What if the closing bracket is far down, off screen?

The wiki states (LayoutBlocks) that 

    "the eye believes the indentation, even though the compiler does not. Making indentation significant means that student programs mean what they appear to mean."

This is not true if brackets trump layout. I can still have layout that is not meaningful then. Or is this a syntax error? (I wouldn't want to have to explain this to my students!) In essence, I have two language constructs competing with each other.

The wiki also states

	"The reasons for this [making layout meaningful] is that it is really important for students to learn to use indentation correctly."

In modern systems I have to question whether this is really still true. If we assume use of a modern IDE, layout can easily be automated (or violations highlighted). The system knows exactly how things should be laid out and can give an arbitrary amount of help and support with this. The assertion that doing this manually is important diminishes very quickly when we assume modern editors.

In short: I think the combination of the two introduces more problems and pitfalls than it generates advantages. I would prefer settling on one of the two. (And my personal preference is very much with explicit blocks (brackets) than with indentation.)

Michael

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailhost.cecs.pdx.edu/mailman/private/grace-core/attachments/20101108/9c6cf1e0/attachment-0001.html>


More information about the Grace-core mailing list