Developer Roadmap
/Code Formatting
TopicStep 102 filesOpen folder on GitHub

Code Formatting

code-formatting.md
View on GitHub

CODE FORMATTING

Tujuannya

  • Membuat kode Python rapi, konsisten, mudah dibaca dan otomatis diformat tanpa perlu bingung dengan style

Black

  • Paling populer
  • "Formatting is not a matter of opinion"
  • Dengan black, maka:
    • Memberi style yang konsisten
    • auto-fix indent, spacing, quotes
    • mengubah code menjadi standar PEP8
    • sangat cepat
    • dipakai oleh perusahaan besar & open-source

Install

pip install black

Format satu file

black app.py

Format seluruh folder

black .

Contoh black

Sebelum

def add( a,b ):
  return a+b

Setelah

def add(a, b):
  return a + b

Alasan disukai

  • tanpa konfigurasi
  • standar industri
  • stabil
  • sangat cocok dengan Pyright, Mypy, Ruff, Poetry

YAPF

  • Yet Another Python Formatter
  • Formatter by Google
  • "Write code as if a human would format it"
  • Dengan YAPF:
    • lebih fleksibel dari Black
    • bisa dikustomisasi sesuai style
    • cocok jika ingin kontrol lebih banyak

Install

pip install yapf

Format file

yapf -i app.py

Format folder

yapf -r -i .

Konfigurasi di .style.yapf

[style]
based_on_style = pep8
column_limit = 88

Yang menggunakan YAPF

  • Internal google
  • Tim yang ingin custom formatting

RUFF

  • The new all-in-one Python linter + formatter
  • Tool baru yang sangat cepat
  • Fungsinya
    • linter
    • import sorter
    • formatter (Black-compatible model)
    • rules seperti flake8, pylint, isort, pycodestyle
    • sangat cepat (10-100x lebih cepat)

Install

pip install ruff

Pakai pipx

pipx install ruff

Format

ruff format .

Lint code

ruff check .

Auto-fix

ruff check . --fix

Konfigurasi (pyproject.toml)

[tool.ruff]
lline-length = 88
select = ["E", "F", "W"]

Perbandingan

ToolTypeStrength
BlackFormatterStandar industri, tanpa konfigurasi
YAPFFormatterBisa custom style
RuffLinter + formatterSuper cepat, modern, recommended

Contoh konfigurasi pyproject.toml

[tool.black]
line-length = 88

[tool.ruff]
line-length = 88
select = ["E", "F", "W", "I"]
fix = true

[tool.ruff.format]
quote-style = "double"
indent-style = "space"