image found at: https://dev.to/__shadz_/tabs-vs-space-49l5

Code Style does not affect your code behavior, so why should I care about it? As a developer you will spent much more time reading than writing code, Robert C. Martin said that the ratio is over 10 to 1. If everyone uses the same style your codebase becomes more readable.

More important than defining a code style is to make sure that everyone follows it. Dart has several tools to help you code better, one of them is dartfmt command line tool used to check and format dart code. First thing is amazing about dartfmt is that is part of dart SDK, this means that everyone using dart already has it available, no installation is needed.

You can pass to the command line a directory or file to check the code format, per example you can use dartfmt ./ to verify the format in all files in the current directory (and subdirectories). The tool will output all file formatted in the console, this isn't very helpful, right? To override your files and apply the style automatic you just need to pass the -w flag to the command: dartfmt ./ -w . In order to validate files you can use the -n flag, it will output all files that need to be formatted. For those interest in checking code style in the CI, here is a simple script to fail the build if the code is not following the format:

#!/usr/bin/env bash set -e set -x output=$(dartfmt ./ -n) if [ -z $output] then exit 0 else exit 1 fi

Right now it’s not possible to define custom code style, the tool uses the style based on the Effective Dart. Ready to starting coding with style? 😎

If you like this post, please follow me on twitter: @guitcastro