6 ms·
> Optimize for the reader, not the writer > Use only spaces, and indent 2 spaces at a time. These conflict in the C++ guide (as well as shell). As I’ve aged,
by toastal 3y ago
> Optimize for the reader, not the writer
> Use only spaces, and indent 2 spaces at a time.
These conflict in the C++ guide (as well as shell). As I’ve aged, 2-spaced code doesn’t provide the necessary contrast for me to easily read & scan code, but rather than picking a larger number or using tabs so users can configure the value, a low, hard-to-read number was chosen.
- jdrek1 3y agoIt's not just age, plenty of people find 2 spaces hard to read. Thus the only logical choice for indentation is tabs. That way every reader can pick the size that is good for them.
- JaumeGreen 3y agoTabs for indentation, spaces for alignment. This is my take. I only can use it on personal projects, as most places I worked in use spaces.
- vbezhenar 3y agohttps://flinhong.github.io/images/201607/tabs-spaces.jpg https://flinhong.github.io/images/201607/tabs-spaces.jpg
- toastal 3y agoAlignment is pretty overrated tbh
- deleted 3y ago[deleted]
- rkagerer 3y agoGreat if nare the two shall meet, but what happens when alignment is dependant on spacing (eg. fancy code comments)?
- TheAceOfHearts 3y agoWe can continue using 2 spaces and configure editors to show those 2 spaces as your desired indentation. You can also use tabs for that, but it feels like that ship sailed over 30 years ago.
- xmcqdpt2 3y agoHow do you count tabs towards line length for auto formatting?
- jdrek1 3y agoI don't, I think that line length limits are typically not a good idea and thus I don't like hard limiting them. Now obviously arbitrarily long lines are no good, I'm not advocating for people to write 300+ char lines. I'm just saying that sometimes there's good reasons to (slightly!) go over the "limit", for example shortening the variable name "users" to "usrs" to make a n+1 line fit into the n char limit would be a terrible idea. And also when this topic comes up then typically people implicitly think about the 80 char limit, which is just way too restrictive and has no reason to exist anymore, it's not 1980. The limit on what constitutes a reasonable line length is a bit subjective but I'd say that well written code rarely goes above ~120 or so. Depends a bit on the language, some just have more boilerplate than others. But instead of making that a hard limit I'd warn above that number so actual people can decide if it makes sense there or if a rewrite is in order. Auto-formatting is great and all, but ultimately a human should have the last decision. Though if you force me to pick a value, then it's 4 because that's the sanest choice for like 95-99.9% of all people.
- cjfd 3y agoYes, I also find 2 spaces for indentation to be completely unreadable. I am not sure it has anything to do with age.
- lima 3y agoIt's a Google-specific legacy mistake. IIRC, they even do (used to do?) two-space indentation for Python.
- Hamuko 3y agohttps://google.github.io/styleguide/pyguide.html#34-indentation https://google.github.io/styleguide/pyguide.html#34-indentat... suggests that they use four spaces.
- auscompgeek 3y agoThe ChromiumOS Python style guide notes the difference between Google's public Python style guide and their internal one. > New projects should use that unmodified public Python Google style guide (4 space indent with methods_with_underscores). Historically, we adopted a style that was congruent with Google internal Python style guide (2 spaces with MethodsAsCamelCase). https://chromium.googlesource.com/chromiumos/docs/+/HEAD/styleguide/python.md https://chromium.googlesource.com/chromiumos/docs/+/HEAD/sty...
- kotborealis 3y agoSome Google python-based projects, like oss-fuzz, are still indented with 2 spaces, which is a huge pain.
- nonethewiser 3y agoI find 2 spaces far more readable so it’s subjective and we can’t just declare that these rules necessarily conflict.
- vbezhenar 3y agoFor me 2 spaces is enough to read code and 4 spaces make code unnecessary wide which makes it hard to read.
- Gabrys1 3y agoI think we can all agree to 3 spaces then?
- madeofpalk 3y agoWhich is exactly the case for tabs, so everyone can render the tabs as wide (or narrow) as they want!
- youngtaff 3y agoYes! Use tabs and allow people to pick their own spacing Also good for accessibility
- alkonaut 3y agoCan’t a sufficiently elever editor render leading spaces with any (e.g) 200% width and thus solve the problem of configurable indent width without the need for tabs?
- garaetjjte 3y agoNot really, because it would screw up alignment (tabs can only be used for indentation, alignment needs spaces).
- alkonaut 3y agoYes I’m only considering indentation now. Tabs vs spaces for indentation feels like it has reasonable arguments both ways but tabs for alignment just feels wrong tbh. Never even considered it a possibility in source code (and never saw it in the wild).
- 3y ago
- Findecanor 3y agoI use that indentation style for labels, including public:, private:, protected: and case labels. For everything else, I indent twice, but I have a habit to compress double-indentation in my source code into a single character code by pressing the Tab key only once.
- TheAceOfHearts 3y agoThis is such a tired issue, why can't editors be configured to display two spaces as your preferred indentation? Why are we still having this meaningless discussion in 2023? Adding this functionality to editors shouldn't be that difficult and we can end this discourse.
- tracker1 3y agoWhat's old is new again, it feels like 1998 and I'm sure times before that.
- frankfrank13 3y agoofc the number 1 comment is a tabs/spaces comment lol
- j1elo 3y agoLike most of us, I've had this discussion an infinite number of times, never reaching an adequate agreement. I myself have always used 4 spaces for C++ codebases. Spaces provide fixed width and consistency, regardless of the code editor. Tab proponents say that the power of tabs is that they can be configured. Thing is, I don't want to configure them everywhere I go, and I change stations a lot. Think also of code viewers: non-logged in GitHub defaults to a 8-spaces tab width, which is enormous and code becomes too wide to read without horizontal scroll in a non-ultrawide screen. Also, people are clumsy. I'd wish this was not the case, but that's the world we live in, and people (I'd even say MOST people) are clumsy as hell and will somehow mix spaces with tabs. Long live the glorious and sweet dictatorship of opinionated, unconfigurable code formatters. Whether they impose 2 spaces, 4 spaces, or tabs, I don't care. Code getting automatically formatted in a consistent style across all teams and contributors, that's bliss. (I myself actually believe that tabs for indentation + spaces for alignment would be the best of both worlds, but never tried to enforce it in my projects)
- toastal 3y agoI’ve set my preferred `tab-width` as a user style & it works just fine. I’ve also raised the issue on Bugzilla to make configuration a lot more visible/easy. This is still preferred IMO to being stuck to specific values.
- rkagerer 3y agoCan I ask how old you are? I pose this question sincerely, as I'm over 40 and still prefer 2-space tabs myself.
- adrian_b 3y agoI do not believe that anyone can argue that 4-space indented code is not easier to read than 2-space indented code, when that is the only difference in formatting between them. On the other hand, when the style rules also impose a very short maximum code line length, e.g. of 80 characters, then I agree that in such cases 2-space indenting is a superior choice, because the slight decrease in readability is compensated by a greater increase in readability due to a much less frequent need to break long lines. When I write for myself, I prefer 4-space indenting, but together with a greater maximum line length.
- rkagerer 3y agoIndeed, the reason I switched to 2 spaces decades ago is for density - to fit more on the screen at a time, particularly if there's lots of nesting (which is often the case in the code I read). BTW I haven't seen code that diligently sticks to 80-character lines since the days of dot matrix printers :-).