Verifying what an 'end' goes with

I have a class, in which there’s a method, and that contains a do-end block of code and in that there’s an if-else-end. There are a bunch of ‘end’ keywords near the end of the file. The compiler isn’t happy. Is there one too many? Oh yeah, I’m in ’ module MyStuff’ and forgot. No, compiler still not happy…

What I’d like to do is write ‘end if’ or ‘endif’ instead of just ‘end’ for the one I believe is ending an ‘if’ statement. Likewise, ‘end class’ or ‘and_thats_all_there_is_to_this_class’ or some such. ‘end module’. I sometimes put comments, as in ‘end # class’ for my future reference, but the compiler isn’t going to make any use of that.

If such a feature exists, 
end if

Otherwise, are there any tricks in Crystal for making sure one’s ‘end’ statements are in proper shape?

1 Like

and nested if?

Interesting idea…hmm…

FWIW you can use crystal tool format to try and make everything all line up but…hmm

Usually if you have a bunch of “end”, it’s probably that the cyclomatic complexity of your code is high hence making it difficult to read.

You might want to export and generalize some blocks into their own functions, in a helper module etc…

It’s not always possible, and I won’t judge your code, so here my 2 cents:

def func
if a
  if b # check whether b is true or not
  end # check of b
end #if a
end #def func

Just adding some comment after each end block tell you where you are :slightly_smiling_face:


That’s one way to solve the problem. I’ve done that before in various languages. For the present problem in Crystal, I’ve discovered that it’s “while a<b” not “while a<b do”. I bounce between several languages all the time, forgetting the syntax details.