25

What is considered best practice for code indentation? When I'm working with HTML and Javascript my rule of thumb is 2 spaces for indentation, and with Java and Python it's 4 spaces.

Also I'm trying to break my bad habit of mixing tabs and spaces. It's messing with the laws of physics.

Comments
  • 4
    Auto-formatting?
  • 2
    Same here. Also, Prettier all the things!
  • 1
    @m12i Looks like I'm going to be working on a prettify plugin install for my editor then.
  • 1
    It shouldnt be something that should prevent you from being productive. If there is a standard in your company to stick to. Do so, else find a preference and setup your environment to handle it for you. That question will invoke too many split and biased preferences.
  • 1
    Auto-format or what is needed to make it look pretty, preferably tabs.
  • 4
    I love the evil little timebombs you can create mixing tabs and spaces in Python. Since they only have to be consistent at the block level you can totally make a working function that uses both tabs and spaces but as soon as someone tries to modify it they are in for a world of pain. Also in Python 2.x 8 spaces = 1 tab, so you can do even more evil shenanigans.

    Mind you, I wouldn't actually do this (probably) but it's fun to know you can.
  • 3
    @catadoxa you sound like a crazy chemistry teacher "[...]and this is how you would build a bomb. We won't do that though! But just knowing how to does feel powerful, right?"
  • 2
    I always use tabs. Unless otherwise specified by company or something. It’s easy, makes code readable and pretty.
  • 2
    I personally use 2 spaces for javascript and tab for Python but as i always say.. Whatever floats your boat man. 🤷🏻‍♂️
    Also check out https://editorconfig.org
  • 1
    For c/c++ 8 spaced hard tabs 😅😅

    But when I use rust, go, then 4 space hard tabs

    For lisp, Nim, elixir, crystal, 2 space tabs (not hard)
  • 0
    2 or 4 spaces, depending on language.
  • 1
    4 for everything. And spaces over tabs. Consistency makes reading any language feel familiar. Spaces allow any IDE/compiler/interpreter to parse without an issue
Add Comment