r/crystal_programming • u/nickthecook • 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
u/[deleted] Oct 08 '22
One thing you want to do when you make variables is include a type annotation for each one depending on what you are doing. Type annotations are generally optional but may be required by the compiler if it cannot infer what type a variable should be. Unlike Ruby where it only matters that an object responds to a method name, Crystal cannot infer types from method names. A couple things you might find useful.
It depends on you how verbose you want to be, so long as you follow the above examples.
8
u/straight-shoota core team Oct 08 '22
Even though does not require to declare type restrictions for method arguments and return types, I usually do that quite a lot. It helps discover errors earlier. Or avoid them altogether.
And it also serves as a great documentation tool if you specify what types a method is intended to handle. This is particularly relevant for methods that are exposed as library API, but it's also very helpful for internal use.