News for hackers. Stuff that matters.
Re:Python with Braces by necovek on Sunday July 07, 2024 @05:44PM (#40900684)
This is a great discussion with many a differing point of view.

To some, significant indentation is better.

Others — too used to braces — miss them dearly in Python.

Next ones, vie for the non-text source code, something to get us past these discussions altogether (editors working on .pyc files directly?).

For programs to be maintained, they need to be read, understood and improved. One—often undervalued—skill in programming is to write beautiful code, because that is more art than craft. And unfortunately, tools like Black prohibit the true artists from expressing themselves clearly with code formatting too. And to those, white-space or braces matters on a different level, and everything else is attempting to make up excuses for why one is better than other.

And while conceptual operations we do on the code seem simple on the surface, devising an editing tool that would do semantic operations on the AST is fricking hard and likely to be very non-ergonomic. Look at all the attempts to make code refactoring tooling: it's crazily complex and confusing that it's simpler to just go and grep for a string and fix anything you find.

As long as it's faster to use regular editing operations to shuffle code around, indent or unindent it (or wrap it with braces), tweak one thing here or there, simple text editors will mostly rule the world of programming.

Re:Python with Braces by mjevans on Sunday July 07, 2024 @05:47PM (#40900701)
go fmt is really nice, that there is A standard for the language which is just included. No more arguments, just do what the including tooling desires.

Python... I'd love if it shipped with a formatter that converted indents to braces, and then had an option for expressing indent as spaces (with number of spaces per indent) OR tabs (same, default 1); then still kept the braces.

Re:Python with Braces by sigzero on Sunday July 07, 2024 @09:20PM (#40901879)
I really hate the "tab width = 8" though.
Re:Python with Braces by moonlion_eth on Monday July 08, 2024 @12:34AM (#40902628)
There are kids out there with tab width 16 you should be grateful
Re:Python with Braces by winocm on Monday July 08, 2024 @07:28AM (#40904427)
And there’s also people who use tabstops of 3.
Re:Python with Braces by arp242 on Monday July 08, 2024 @12:30PM (#40906824)
You can use a tabstop of 4, or 2, or whatever you want. It's not a problem. Don't need to use 8.
Re:Python with Braces by xigoi on Monday July 08, 2024 @05:06PM (#40909708)
But the formatter will still line-break the code as if the tab width was 8, which will lead to lines with several lines of indentation being too short.
Re:Python with Braces by arp242 on Tuesday July 09, 2024 @02:42PM (#40919430)
When does that happen with gofmt?
Re:Python with Braces by duncan-donuts on Sunday July 07, 2024 @09:44PM (#40901974)
I think people are actually searching for Lisp far more than we are willing to admit.
Re:Python with Braces by kazinator on Monday July 08, 2024 @12:34AM (#40902630)
Python encodes structure in only one way, using indentation. There is no redundant signal that can be checked for a discrepancy that could indicate a problem. GCC and Clang can diagnose when indentation is "misleading" because it doesn't match what the braces are saying.

Python's choice of representation is such that two different Python programs show zero differences under a white-space-suppressed diff.

The white-space suppressed diff is a useful tool for comparing programs when some sections of code have changed indentation but are otherwise the same; yet we cannot rely on it if we are using Python.

Python's syntax design is objectively poor on several purely technical points. In its favor, there are only handwaving pop psych arguments.

Re:Python with Braces by necovek on Monday July 08, 2024 @04:04AM (#40903488)
> Python encodes structure in only one way, using indentation. There is no redundant signal that can be checked for a discrepancy that could indicate a problem. GCC and Clang can diagnose when indentation is "misleading" because it doesn't match what the braces are saying.

I've seen both misplaced braces (akin to mis-indenting blocks in Python), indentation not matching braces and other problems of the same sort in non-Python code. Readers of the code would misunderstand the code when badly indented, and might introduce bad braces as well (not everybody uses automatic formatters and linters either, esp as they will sometimes "quickly" edit code in their non-usual dev environment). Not to mention that some "braced" languages allow having single-line blocks without braces.

While I am fond of applying many of double-entry-accounting principles in programming to increase trust in what we write, I believe that's much better done with unit-tests, which can more clearly demonstrate the expectations for any code and read more like documentation.

Do you think all syntax in programming languages should have some sort of extra validation built-in? Eg. you should type in a constant twice (declare it as `const int a = 5` and then you have to set the value later as `a = 5` or you get an error?)?

As I said above, people will always find an "objective" excuse why their preference is better. But I've seen bad-block-boundaries in Python as much as I've seen it in other languages which use explicit block boundaries like braces (and I've done more Python over the last ~20 years). I've heard this argument a gazillion times, but hundreds of bugs due to that have simply failed to materialize while working on large projects with tens and hundreds of people.

Getting indentation right is _really_ not that hard, just like getting braces right is not that hard. I've yet to find someone who prefers their code to not be indented at all, and only rely on braces — at least not in a team setting where you can't simply reformat all of it.

Re:Python with Braces by kazinator on Monday July 08, 2024 @06:05AM (#40904075)
> But I've seen bad-block-boundaries in Python as much as I've seen it in other languages

You have to consider ones you have not seen: the ones that are valid syntax, and thus invisible.

> (declare it as `const int a = 5` and then you have to set the value later as `a = 5` or you get an error?)?

For that, it would be more like assert (a == 5). It's not commonly done for constants. Neither the definition nor assertion are perturbed by significant whitespace, so it isn't the same.

If the constant only exists for clarity and not configuration (so the code cannot conceivably work with any other value), then the assertion would be warranted:

  assert(a == 5); // Did you read the comment about not changing a?
Re:Python with Braces by necovek on Monday July 08, 2024 @05:39PM (#40910012)
> You have to consider ones you have not seen: the ones that are valid syntax, and thus invisible.

Sure, that's a fair point, but the same holds true for braces. One of those bugs I found in non-Python code was there for a couple of years (badly placed braces).

For the other topic, you were specifically praising the duality of braces and indentation to understand block boundaries. It's hard to come up with an example where everyone does the non-obligatory thing anyways (indentation in {} languages), since obligatory thing is, well, obligatory.

Re:Python with Braces by zzo38computer on Monday July 08, 2024 @03:53PM (#40908896)
I prefer using braces to delimit blocks (although the code will be indented, too), and semicolons to separate lines (the "automatic semicolon insertion" is, in my opinion, one of the worst features of JavaScript). Sometimes I will deliberately indent code in an unusual way, e.g. temporary debugging code might be not indented at all (although such things are not usually present in the final code; they will usually be quickly removed after being added, and such temporary debugging code is often are not committed to the version control system).

The ability for GCC and Clang to diagnose misleading indentation can be helpful; some programmers may wish to use it (and others may prefer to disable it). However, apparently it treats a tab as a number of spaces; I think that it should not do that, and using tabs on some lines and spaces on other lines to measure indentation within the same block, should always be considered misleading indentation (so if -Wmisleading-indentation is enabled, then it should always display a warning message in that case).

Re:Python with Braces by necovek on Monday July 08, 2024 @05:44PM (#40910064)
Do you deliberately "misindent" the code or is it mostly because it's hard to reindent and then back the change out? If the latter, that's obviously a different use case and one can use syntax tricks to achieve the same with Python if you don't want to have your editor help with that for whatever reason.
Re:Python with Braces by zzo38computer on Monday July 08, 2024 @06:55PM (#40910655)
Deliberately, both in order to mark it in a special way to stand out clearly, as well as to avoid having to add indentation for such a code where it is unnecessary and not helpful. That line of code will soon be deleted anyways, so it does not matter much if other people dislike it.
Re:Python with Braces by necovek on Wednesday July 10, 2024 @01:57PM (#40929565)
Can you share an example where you non-temporarily add braces around a code block but then keep the indentation the same level as before the block? What do your collaborators think of it?
Re:Python with Braces by kazinator on Tuesday July 09, 2024 @12:28AM (#40912536)
I also sometimes misindent some temporary debug print statements. However, indenting them right is just a matter of typing == in vim, while the cursor is somewhere on the misindented line.

Moreover, the ] modifier before p or P (paste below or above current line) adjusts the paste to the indentation of that current line. It takes next to no effort to copy and paste some code to the correct indentation.

Backing changes out nowadays is done with "git checkout --patch".

Re:Python with Braces by necovek on Wednesday July 10, 2024 @01:55PM (#40929549)
Getting indentation right with editor support is trivial in Python too (in Emacs, TAB does what you describe anywhere on the line, and when there are multiple valid options, it cycles through those).

Pasting blocks into right indentation or reindenting entire blocks too. So I am glad you agree it's simple to do any of these even with significant whitespace ;)

I wouldn't know about other editors, but I'd be surprised if it's not so in every one of them.

Where indentation really matters for temp debug statements — you want to enter a block unconditionally or skip it unconditionally — there are other syntactic tricks like add `True or`, `False and`, return, continue, break, semicolon, etc. But these are all temporary examples you are giving, and this only matters if editor is not supporting you.

And that's my point: these are trivialities and I can't stop being perplexed that people get so fussy about it.

I am not sure how git plays into this? It works exactly the same for Python, it's not like Python is bf with space as the control character.

Re:Python with Braces by kazinator on Monday July 08, 2024 @10:07PM (#40911883)
That's a tricky requirement because some GNU programs do exactly that: use mixtures of tabs and spaces. For instance, the first three logical tabs are groups of 2 spaces, so 6 spaces. The fourth tab, however, turns into single hard tab.

I think that to do this right, there would have to be a complex option in which you specify the tabbing style. -Widentation=<notation> where <notation> is some way of specifying the style.

There has to be allowance for alignment also. Any line, regardless of indentation method, can terminate in one or more spaces that exist for aligning the fist non-whitespace character with something in the previous line.

Re:Python with Braces by thoutsnark on Monday July 08, 2024 @01:05AM (#40902744)
I strongly agree with your main point that pythons syntax design is objectively poor, but content that even the grace you seek to extend to it is wrong

>Python encodes structure in only one way, using indentation

Except when you write a multiple line string using """ notation.

Or when you put things inside parenthesis, which i have seen as the preferred solution for method chaining.

Point is, python claims indentation is all that is needed, and then very quickly breaks it's own rule.

Re:Python with Braces by necovek on Monday July 08, 2024 @04:18AM (#40903556)
Open parentheses and "multi-line" strings are simply Python's way to allow a single statement to span multiple lines in a nicer way: incomplete expressions continue across the newline (you can also do that with a common Unix way of escaping the newline — backslash right before you start the new line). FWIW, even """ notation does not require a string to be multi-line — it mostly allows using less escaping for things like newlines but also quotes.

A preferred way to chain methods is to store values in descriptively named intermediate variables: it would also ease debugging as you'll get a direct pointer to the line that is problematic. Though, TBH, that's probably bad API design (I know it's common with DB query syntax, but that's attempting to turn SQL query construction into objects when they really aren't).

Python, like most languages, doesn't really stop you from doing crazy stuff. But also like most languages, it can be done really well.

Re:Python with Braces by sverhagen on Monday July 08, 2024 @01:24AM (#40902817)
> two different Python programs show zero differences under a white-space-suppressed diff.

This must surely be a bad thing? How would this signal an unfortunate indentation change between:

  if foo:
    bar()
  bar2()
And:

  if foo:
    bar()
    bar2()
???
Re:Python with Braces by arcxi on Monday July 08, 2024 @11:24AM (#40906236)
>The white-space suppressed diff is a useful tool for comparing programs when some sections of code have changed indentation but are otherwise the same; yet we cannot rely on it if we are using Python.

yes, because in Python two programs with different indentation are not the same. what's the problem that you can't use tools intended for code without significant whitespace with code in Python?

Re:Python with Braces by xigoi on Monday July 08, 2024 @05:01PM (#40909650)
> There is no redundant signal that can be checked for a discrepancy that could indicate a problem.

What’s the problem with having no redundancy? By the same logic, we should require numerals to be spelled out in words so the compiler can check if the programmer didn’t accidentally write a different number.

  def fahrenheit_to_celsius(x): {
      return (x - 32 [thirty two]) * 5 [five] / 9 [nine];
  }
Re:Python with Braces by kelipso on Monday July 08, 2024 @06:39PM (#40910522)
Redundancy reduces likelihood of bugs. Your numbers example would too but it's obviously too much work for not enough benefit.
Re:Python with Braces by xigoi on Tuesday July 09, 2024 @03:10AM (#40913301)
Braces are also too much work for not enough benefit.
Re:Python with Braces by kelipso on Tuesday July 09, 2024 @10:45AM (#40916747)
Given the chronic inconvenience of significant whitespace, absolutely not.
Re:Python with Braces by necovek on Tuesday July 09, 2024 @12:48PM (#40918185)
That's an opinion that is largely disproven by the amount of working Python code in the world.

Why is it so hard to accept that this is your personal preference and is really not an "objective" statement?

Re:Python with Braces by kelipso on Tuesday July 09, 2024 @02:26PM (#40919270)
The reason for the popularity of python is mainly due to advertising it to programming newbies and non-programmers, not due to significant whitespace.
Re:Python with Braces by necovek on Wednesday July 10, 2024 @01:40PM (#40929362)
I never brought popularity up: that mostly leads to lots of small snippets but not a big amount of working code. You can have an unpopular language with a few productive actors producing lots of code.

I argue that "chronic inconvenience" is disproven by so many working programs. Even if one argues it's an inconvenience, it is a minor one or it would be hard for non-newbies to produce so much with Python.

Re:Python with Braces by kelipso on Wednesday July 10, 2024 @06:04PM (#40931975)
It’s a minor inconvenience, sure, I use python myself, but it’s definitely a chronic one. It annoys me every single time I code in python.
Re:Python with Braces by xigoi on Wednesday July 10, 2024 @03:08AM (#40924405)
It’s thee braces that are inconvenient. I have to indent my code anyway, so why add another redundant marker that I’ll have to mentally filter out all the time?
Re:Python with Braces by necovek on Tuesday July 09, 2024 @12:50PM (#40918208)
They are certainly more work and why I prefer significant whitespace, but I wouldn't really say "too much".

They are a preference, but neither is objectively better or worse.

Re:Python with Braces by kazinator on Monday July 08, 2024 @06:15PM (#40910338)
If we primarily relied on a whitespace representation of numbers, it would help to have digits there for the machine to look at and possibly diagnose against the whitespace.
Re:Python with Braces by necovek on Tuesday July 09, 2024 @12:53PM (#40918226)
(Almost) Every programmer in the world depends on whitespace to understand code: are you claiming that's not true?

If humans can do it, why wouldn't computer programs like interpreters do it?

Re:Python with Braces by Too on Tuesday July 09, 2024 @12:23AM (#40912519)
Most diff tools default to not suppressing leading whitespace. Trailing whitespace should be automatically removed and enforced by your code formatting tools, leaving no reason to suppress it.

As someone who has been diffing endless amounts of python through various diff tools, this is totally a non issue.