3 ms·
The reason it's more accessible is because it's The Right Way, in the same way that a text file is more accessible than a screenshot of that text file. Indentat
by dTal 7y ago
The reason it's more accessible is because it's The Right Way, in the same way that a text file is more accessible than a screenshot of that text file. Indentation is a semantic property, not a visual one.
- viraptor 7y ago> Indentation is a semantic property, not a visual one. Depending on style in can be both. +-- semantic v def something(with aligned, list of, parameters): ^ visual --+
- Pyxl101 7y agoThat’s a good point. If you need specific visual alignment with an arbitrary number of characters on the previous line, then spaces are appropriate. Tabs should be used for the initial semantic indentation then spaces for additional visual indentation beyond that. (Whether visual alignment of this sort is desirable is a different question. If you want it then spaces are appropriate.)
- krilly 7y agoThere is a difference between indentation and alignment. Unfortunately, python chose to enforce spaces only both via the language itself and pep8.
- ChrisSD 7y agoHow does the language enforce spaces only? PEP8 was only meant as a style guide for the python standard library. Although many have unfortunately taken it as a religious text.
- scrollaway 7y agoOh gods please don't align like this, it's just so unreadable. Example of what that does in practice: Lines that end up wrapping almost at the end of the soft-character-limit and look like this: https://github.com/pennersr/django-allauth/blob/c9b31ddee81d2cde06c1f686c4eb7703f916681f/allauth/account/views.py#L629-L632 https://github.com/pennersr/django-allauth/blob/c9b31ddee81d...
- skohan 7y agoIMO it depends on the language. With named parameters it is very readable, and is my preference when you have functions with lots of parameters.
- dTal 7y agoIt breaks a fundamental assumption, which is that indent depth strictly reflects code nesting depth. If you're going to do that, the "correct" way would be to indent each parameter with tabs to the same depth as the function declaration, and then use spaces to take up the rest of the space (i.e. the width of the function declaration). Which is admittedly ugly - but it's ugly because it reflects the ugliness of misusing indentation for alignment. It's slightly less ugly if you use an editor that displays tabs and spaces differently - Kate/Kdevelop use a nice discreet » character for each level of indentation: def hello_spam(): » while True: » » print ("Hello", » » "World") The last line is visually aligned, but the semantic depth is the same.
- 11235813213455 7y agoI'd go with def hello_spam(): » while True: » » print( » » » "Hello", » » » "World" » » )
- pjc50 7y agoEh? This is fairly normal in most languages. Although in the example given I might have moved the .dispatch to the next line instead.
- bikeshaving 7y agoI think all code styles which use arbitrary vertical alignment are strictly inferior to ones which use newlines and indents. The simple fact that changing `something` to `somethingElse` causes a 3-line diff is enough to convince me, but when we add the idea that hard tabs are better for a11y, it makes arbitrary vertical alignment seem completely untenable to me.
- tinus_hn 7y agoFor that you would use tabs to align to the ‘def’ statement and spaces for the parameters. That way it keeps lining up if someone else uses different tab widths.
- flukus 7y agoMove "with aligned," to it's own line and you don't need visual alignment at all, it's all semantic. Surely the extra line feed is worth elimination a whole category of alignment in code.
- raverbashing 7y agoYou are 100% right "but editors will give you 4 spaces when you press tab" - yes, this one is a BS argument But I've given up on discussing with people completely refractory to discussion, the cult of space indentation is strong There is one consistency I'll concede: the consistency between indentation and tidying up the code, mixing tabs and spaces there is awful (as any mix of tabs and spaces are)
- teekert 7y agoYeah, it's pretty bad, as a novice I once opened a file in Vim, I used tabs for indenting, as I do in vscode (for Python code) and I get all sorts of errors, very annoying. Turns out, I never really was using tabs in vscode. And it's already in everything I wrote (I found out about this debat really late in my self taught coding career). Guess the choice was made for me.
- enriquto 7y ago> " editors will give you 4 spaces when you press tab" but what kind of editors do people use? Any normal editor supports autoindentation and you never get to press the damn tab key. In vim for example, if you set autoindent then you can map TAB to ESC, which is much closer.
- danbruc 7y agoWell, as always, it is more nuanced. If you are simply indenting code, tabs are certainly the semantically correct way, one level of identation, one tab. But sometimes indentation by one level just doesn't cut, sometimes you want finer control for aligning things, for example to highlight common structures in expressions that just differ by some varaibles with unequal lengths. And this is where using tabs and mixing them with spaces falls apart and it fall apart in a spot where you think it is important to have good alignment in order to make the code easier to understand and modify in order to avoid bugs.
- deleted 7y ago[deleted]
- dTal 7y agoI don't see the issue. You shouldn't need to align things with spaces at the beginning of lines, since they're already aligned. And you shouldn't be using tabs for alignment anywhere else in the line; they're indents. So where's the "mixing"? Tabs until the first non-whitespace character on a line, spaces anywhere else you like.
- danbruc 7y agoThe mixing usually happens by accident, you copy and paste bits and pieces, you need a lot of spaces for aligning things and just hit tab, you want to break the indentation rules by a space or two for readability, ... I am not saying that it can not work if you are careful enough, proabably with visible whitespace on all the time, in practice however it will become messy most of the time, especially as people working on a code base come and go.
- Grue3 7y agoIndentation is semantic only in Python (which incidentally uses spaces). In any other language it's a purely visual property - the code works the same regardless of indentation.
- jnty 7y agoI think you've misunderstood the use of semantic here - it's semantic for all text files. Instead of describing very exactly 'put 8 spaces here' you're saying 'for some reason this line needs to be indented once, take this information display this as you wish.'
- maeln 7y agoIndentation is semantic has in it carry a meaning. We are not talking about how a specific language use it. As for Python, it doesn't use space. It can use both spaces and tabs (but not mixed within the same file). It's just that PEP-8 require spaces, but not every project/library follow PEP-8.
- majewsky 7y ago> Indentation is semantic only in Python And in Haskell. And in Nim. And in Haml. And in Sass. And in YAML. https://en.wikipedia.org/wiki/Off-side_rule#Off-side_rule_languages https://en.wikipedia.org/wiki/Off-side_rule#Off-side_rule_la...
- jlg23 7y agoIn those languages where it is not semantic, proper indentation still gives visual clues to the semantics: With proper indentation one can read programs in lots of languages even without their specific semantic markers. A simple way to see this is to configure your editor's syntax highlighting to use the background color for parenthesis, semicolons or whatever the markers are in your language of choice.