HydroCS Wiki

Pioneering Hydrologic Computational Science

User Tools

Site Tools


howtos:how_to_format_code

This is an old revision of the document!


How to format code

We use the following code formatters to maintain consistent coding styles across different programming languages. These tools automatically format source code without changing its behavior.

Python: Black

«https://black.readthedocs.io/|Black» is an opinionated Python code formatter with minimal configuration.

Install:

pip install black

Format:

black script.py

Check without modifying:

black --check script.py

R: Air

«https://posit-dev.github.io/air/|Air» is a fast R code formatter written in Rust and developed by Posit.

Install:

pip install air-formatter

Format:

air format script.R

Check without modifying:

air format --check script.R

Shell: shfmt

«https://github.com/mvdan/sh|shfmt» formats POSIX shell, Bash, and other supported shell scripts.

Install:

go install mvdan.cc/sh/v3/cmd/shfmt@latest

Format:

shfmt -w script.sh

Check without modifying (list files needing formatting):

shfmt -l script.sh

C/C++: clang-format

«https://clang.llvm.org/docs/ClangFormat.html|clang-format» automatically formats C, C++, and related languages. Formatting rules can be configured using a .clang-format file.

Install:

pip install clang-format

Format:

clang-format -i source.c

Check without modifying:

clang-format --dry-run --Werror source.c

JavaScript: Prettier

«https://prettier.io/|Prettier» is an opinionated formatter for JavaScript, TypeScript, JSON, HTML, CSS, and other web languages.

Install:

npm config set prefix ~/.local
npm install -g prettier

Format:

prettier --write script.js

Check without modifying:

prettier --check script.js

Notes

  • Formatters should be run before committing changes.
  • Use the default formatting rules unless project-specific requirements justify configuration.
  • Ensure that user-installed executable directories, such as ~/.local/bin, are included in PATH.
  • Formatting checks can also be integrated into CI workflows.
howtos/how_to_format_code.1792276495.txt.gz · Last modified: by hcho

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki