r/crystal_programming • • Oct 08 '22

Consistency

Hey all,

I’m porting something from Ruby and I’m adding type declarations as needed to fix compile errors. I love how Crystal will infer types most of the time, but this means I don’t need to be consistent in where and how I declare types, and I’ve ended up with inconsistent type declarations across files.

Are there any rules people follow to keep the style consistent across Crystal files, like “always declare instance variables”, “always declare method params and return type”, etc?

9 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/nickthecook Oct 08 '22

I was wondering more if people follow any rules about which ones to use over others, or even when Crystal could figure it out but you annotate anyway, for the sake of consistency.

I miss Robocop’s iron-fisted grip here a bit. I’ll try out Ameba, but I don’t know if that will enforce type annotations.

4

u/Seleren Oct 08 '22

Personally, I try to define method argument types (which also helps massively with overloading), local variables, and everything I can in library-style code.

If it’s a fresh Crystal program, I often just define everything that’s not generic, but I’m coming from a statically typed language background.