Friday, April 17, 2009

On tabs vs spaces

If I understand the debate correctly, the problem of tabs vs spaces arise only when there are both in the same file:

1 if (a_condition &&
2     another_condition)
3 {
4     do_something(variable1,
5                  variable2,
6                  variable3)
7 }

The indentation doesn't really matter. It works either way. But the alignment would break in spectacular fashion if we aligned lines 2, 5 and 6 with tabs.

So always tabbing won't work.

People like to be unique, and everyone has their own favorite tabstop settings. I've seen different co-workers use 2, 4 and 8, for example. Tabs make the files automatically look nicely indented to you in your favorite editor(tm). Spaces make it look nice only to some people (doesn't that remind you of the three bears fairy tale?). Also, having a key insert not only a different character than it states, but also several of them, is weird. So pure spaces don't quite work either.

No editor I'm familiar with can smartly figure out what portion of the leading space is supposed to be tabs and what is supposed to be spaces AND do the conversion automatically when you press tab and/or spam the spacebar. And we're assuming we can't rely on the programmer to remember to check (a fair assumption imho).

So tabs for indentation and spaces for alignment doesn't work either.

Solution?

Well, if there was one, we'd all be using it. What if we just avoid aligning things altogether?

//with alignment
doSomething(var1
            var2
            var3)

//indented
doSomething(
  var1,
  var2,
  var3
)

//with alignment
if (foo == true &&
    bar == true &&
    baz == false)
{
  //bla
}

//refactored
var isNameValid = foo == true
var isEmailValid = bar == true
var isDateValid = baz == false
if (isNameValid && isEmailValid && isDateValid) {
  //bla
}

Then again, that's javascript. Probably doesn't work so well for C or lisp.

But the question is: why do people align stuff, rather than indent it? I can see that it'd look ugly in the case of the if statement, but time and time again I've seen huge parameters lists for some OpenGL call being aligned to some random column for no particular reason when they could just break the line after the opening parens and indent the parameters (and they'd even fit in the screen!).

For things like huge if statements, I find that the refactored pattern above is nice especially when the code is dense (e.g. when expressions are queries to data structures, like in LINQ or Python comprehensions), since the variable name describes what information is being pulled out of the data structure.

Back to the topic

Anyways, coding patterns aside, I still don't know what's a good way to tie spatial code organization more closely to the abstract syntax tree (and in the case of alignment, to the metagroups of data) that provides a consistent coding style convention that works everywhere and for everyone.

Is bickering about which ASCII character is better the best we can do?

Thursday, April 16, 2009

Lisp without parentheses

So apparently, every lisp newcomer and their dogs has had the fabulous idea of making a version of Lisp that uses indentation instead of the derided parentheses.

The most common proposal I've seen is to make each line with a deeper level of indentation be a new form.

(0 (1 2))
becomes
0
  1 2

Simple enough. The problem is that the idea breaks when we have a form that looks like (0 1 (2 3) 4 5 (6 7)) or (if condition some-long-form another-long-form). The first breaks because once you indent, there's no syntax to close the current form and return to the parent, since outdenting requires moving to a new line, which closes both forms.

The second form is more subtle: in Algol-like languages, you'd normally some-long-form on a new line and indented. We can't do that with the lisp-without-parens proposal unless we add another symbol to escape line feeds or something like that.

if (foo bar) \
  do-something \
  do-something-else

That just looks plain stupid. Let's scratch that.

Another idea would be to make outdenting return to the parent form, i.e. a line feed does not close a form.

(0 1 (2 3) 4 5 (6 7))

0 1
  2 3
4 5
  6 7

(if condition some-long-form another-long-form)

if condition
some-long-form
another-long-form

Bla. Looks dumb and we now need something to denote when a form closes and a new one opens.

(if condition (+ 1 2) (+ 2 3))

if condition
  + 1 2
  =====
  + 2 3

Or something equally bizarre. Scratch.

Ok, let's stop with the toying around a bit

In case you haven't noticed the pattern, the problem here is that even though regular lisp has two pieces of syntax (open-parens and close-parens), and python-like syntax has three (line feed, indent and outdent), indent and outdent can't always be written out (e.g. in the last example they cancel out and thus we need a fourth symbol to go around that edge case).

So can Lisp without parentheses work without bolting on weird characters to make edge cases work? I don't see how.

Piracy

Is it just me or is everyone obsessed with pirates these days?

Thursday, April 9, 2009

Java is so not dead

So since Java is now supported on Google App Engine, a gazillion languages become supported too. We can now play with Javascript, Ruby, Lisp and even Lua in GAE. Talk about bang for a buck.

Tuesday, April 7, 2009

The most dangerous person in the world

Pretty sure I've seen the numbers before. I just like to read about this topic for some reason. Putting things in perspective I guess.

Nukes

Scary, the US radius is so large it doesn't even fit in the map.

Friday, April 3, 2009

The User

I was working on an internal app today and caught myself saying "when the user does x". Dude. I *AM* the user.

Wednesday, April 1, 2009

Ignore teh Intahtubez day

Habits question: do we really need a spammy day to ignore the Internet? I've been noticing that I've been spending a bit more time than I probably should online. Interestingly, that realization only really clicked because of the wacky nature of April's Fools. Hmm.

Competence

Jeff's question: Should competent programmers be mathematically inclined?

My question: what does one have to do with the other? Does writing a 3D engine automatically make you a competent programmer? Does working almost exclusively with databases make you not competent? Mental exercise: Let's swap these two hypothetical programmers for a day and see how it goes.