Formatting Code

The best way to format and lay out your code has been an important (and not always friendly) question in every programming language since the dawn of coding. Learning from the success of Go’s gofmt and Rust’s Rustfmt, Crystal has its own built-in code formatter to format your code to the recommended coding style.[42] It automatically formats all .cr files in the current directory and even formats code samples included in documentation blocks. You apply it like this:

$ crystal tool format file.cr

If you omit a filename, it works on all source files in the folder. It’s easy to forget it, though, so more and more text editors, such as Sublime Text, and IDEs, such as Visual Studio Code, include it and do the formatting automatically when you save the file. Try some weird code restructuring on a somewhat bigger code file (such as classes_and_structs/classes.cr), and see how the format tool restores its beauty.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset