Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

if-statements #5

Closed
mekeor opened this Issue Apr 8, 2012 · 12 comments

Comments

Projects
None yet
4 participants

mekeor commented Apr 8, 2012

How should I indent if-statements?
How should I indent them in do-blocks?

This works everytime for me. The key (for me anyway) is the then and the else at the same level of indentation.
Especially guaranteed to work if you just make the then and else statements one function as you can then be sure nothing funny is happening indentation wise.


if (booleanExpression)
    then trueFunction x y
    else falseFuntion x y

mekeor commented Apr 9, 2012

Unfortunately, in do-blocks, this indentation is illegal according to Haskell98, as far as I know. If I understand correctly, the purpose of this Haskell'-ticket http://hackage.haskell.org/trac/haskell-prime/wiki/DoAndIfThenElse is to make it legal. Howbeit. HLint prints an error with the following code:

foo = do
  if condition
  then doThis
  else doThat

Personally, I prefer this layout which is illegal as well:

foo = do
  if   condition
  then doThis
  else doThat

Other people use

foo = do
  if condition
     then doThis
     else doThat

while some other like to indent as much as possible with two whitespaces like this:

foo = do
  if condition
    then doThis
    else doThat

And even this layout is used for simple and short conditions:

foo = do
  if condition then
      doThis
  else
      doThat

which is illegal as shown by the link above.

So, I'm desperate. Which layout should I use (for IF-statements in DO-blocks)?

By illegal do you mean, stuff won't compile. Or are you just worried about style?

Sorry I forgot code block.

mekeor commented Apr 9, 2012

it's simply (or was?) not allowed. so, the compilers actually shouldn't compile it.
i'm not sure, actually. at least, it compiles fine in current GHCs. but HLint still says it's invalid.

As far as I know any except the first two representations you've shown should consistently compile.
Just don't use tabs.

Contributor

jaspervdj commented Apr 9, 2012

My 50 cents, to maintain consistency with the rest of the style guide (4-space indent), and compatibility with older GHC versions, I think this should be the preferred formatting:

foo = do
    someCode
    if condition
        then doThis
        else doThat

If I correctly understand the case, someCode is always present, if it is not, you should probably use guards?

Here is a longer example -- note how I maintain 4-space indenting, even in the nested do blocks.

Additionally, I think that one should be allowed to format it on a single line (if line length is not greater than 80 columns):

foo = do
    someCode
    if condition then doThis else doThat

Naturally, guards and pattern matches should be preferred over if-clauses, whenever possible.

mekeor commented Apr 9, 2012

I agree. That's a nice suggestion.

I definitely prefer that.
I'd imagine quite a few one-liner if-else's could become guards

Owner

tibbe commented Apr 9, 2012

I do what @jaspervdj said. It's consistent with how we align expressions in general. Is it worth writing down?

Contributor

jaspervdj commented Apr 9, 2012

It think it's worth mentioning -- I've seen some weirdly formatted
if-clauses in the wild. I'll rewrite my reply a bit and drop you a pull
request.

Owner

tibbe commented Apr 9, 2012

Sounds good. In addition to your examples perhaps it's worth noting that

f = if ...
    then ...
    else ...

is also fine in expression contexts (just as you could format any other expression like that.)

Owner

tibbe commented Apr 9, 2012

Fixed in 6999aa0 and 744e395.

@tibbe tibbe closed this Apr 9, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment