News for hackers. Stuff that matters.
Re:Avoid Else, Return Early (2013) by Merad on Monday March 26, 2018 @09:22AM (#16678632)
> Same way braces go on the end of the method/class name to reduce LOC.

Screw that. I've been writing code for 15 years, and Allman style braces make it so much easier to mentally parse code into blocks that they're worth every single LOC. I can't speak for anyone else but I'm not working on an 80x24 terminal anymore.

Re:Avoid Else, Return Early (2013) by optimuspaul on Monday March 26, 2018 @09:26AM (#16678650)
Totally disagree with you.... it's funny though that I read LOC as "level of complexity" not "lines of code". I've been writing code for 30 years and I think it's jarring when the braces are on the next line, so much easier for me to parse that when it's on the same line. But everyone is entitled to their own opinion.
Re:Avoid Else, Return Early (2013) by ivanhoe on Monday March 26, 2018 @10:26AM (#16679125)
In last 20+ years I've changed the coding style many times, usually to fit the current team style. And in my experience it takes a few weeks for the brain to rewire to the new style. First you hate it, and then you get used to it, and then you're like: wow it's great. Then you switch the standards for a new project and again, the same steps. It's just a matter of habit...
Re:Avoid Else, Return Early (2013) by ohazi on Monday March 26, 2018 @01:01PM (#16680431)
Just give me an opinionated ${LANG}fmt utility to do it for me so I don't have to care and don't have to waste time arguing with other zealots. Let the tool be the zealot.
Re:Avoid Else, Return Early (2013) by georgemcbay on Monday March 26, 2018 @05:34PM (#16682835)
This is exactly how I feel, as someone who has been coding for 22 years (professionally, closer to 32 if you count when I started hacking in BASIC and 6510 assembly on the C64).

I just defer to the code formatter. That way everything is consistent looking internally, which is all I care about. I couldn't care less whether the braces are on the same or a different line or other things like that outside of the fact that I do like internal consistency within projects.

All that aside, as soon as I saw the OP title I knew (via years of exposure to bikeshedding programmers) that this thread would end up having a ton of comments on it and wasn't surprised to see it was up to 500-something and still rapidly climbing.

Re:Avoid Else, Return Early (2013) by npsimons on Monday March 26, 2018 @05:33PM (#16682827)
> Just give me an opinionated ${LANG}fmt utility to do it for me so I don't have to care and don't have to waste time arguing with other zealots. Let the tool be the zealot.

So totally this. I despise indenting with tabs with a passion, but when I heard gofmt did that I was actually pleased. There's no arguing with the official formatting utility: https://golang.org/doc/effective_go.html#formatting

Re:Avoid Else, Return Early (2013) by josephv on Monday March 26, 2018 @10:57AM (#16679359)
Pragmatic programming has lost. If you aren't a zealot about being pedantic, just move into the old-folks home.
Re:Avoid Else, Return Early (2013) by Zyst on Monday March 26, 2018 @12:59PM (#16680401)
Not at all, I'd argue this whole Tabs/Spaces, bracket placement, single/double quote discussion should be getting killed by code formatters.

At the end of the day you can do whatever you want with the code you're writing, and some pre-commit hook can run go fmt/prettier/whatever and then it'll all get standardized.

I've become a very heavy proponent of code formatters (I added prettier to the entire team) and I truly believe that talk about code formatting will never really die off, but at the end of the day you can do whatever you want on your side but still have a standardized looking codebase, and that feels extremely liberating to me.

As a PS: If you are a diehard tab proponent/different bracket placement/whatever but your team formatter configuration uses spaces instead then you can also use code formatters to do spaces -> tabs locally, say, on file open. Once you commit your file your precommit falls back to the team configuration, which removes your tabs. Everyone is happy.

Re:Avoid Else, Return Early (2013) by cortesoft on Monday March 26, 2018 @01:20PM (#16680614)
I think the person you are responding to was being sarcastic...
Re:Avoid Else, Return Early (2013) by always_good on Monday March 26, 2018 @01:03PM (#16680446)
I'd say it's the opposite. Code formatters are winning. It's becoming increasingly trite to bikeshed over formatting when projects are using code formatters.
Re:Avoid Else, Return Early (2013) by cestith on Monday March 26, 2018 @01:36PM (#16680777)
As Andy Lester recently tweeted (but said it wan't his): Should "bikeshedding" be hyphenated?

I should see if he remembers where he got it.

Re:Avoid Else, Return Early (2013) by petdance on Tuesday March 27, 2018 @10:07AM (#16688480)
I don't know. If I remembered I'd have attributed it. I noted that it wasn't mine as the next-best-thing.
Re:Avoid Else, Return Early (2013) by twic on Monday March 26, 2018 @04:11PM (#16682087)
Well, i'm a pedant about being zealous, and i am afraid we are now doomed to war eternally.
Re:Avoid Else, Return Early (2013) by ravitation on Monday March 26, 2018 @12:50PM (#16680309)
I enjoy zealous pedantry. I have strong personal opinions about something that I do a lot (especially the pedantic parts), but most of it is relatively lighthearted.
Re:Avoid Else, Return Early (2013) by brann0 on Monday March 26, 2018 @11:09AM (#16679460)
I always adapt to the style used in the project I land. I can understand people liking one over another but to reach holy wars levels of disagreement about this kind of subject is something I don't get.

Same goes to spaces vs braces, editor wars, etc.

Downside: my projects are usually a bit of a mess with mixed indentation styles :D

Re:Avoid Else, Return Early (2013) by psyc on Monday March 26, 2018 @10:02AM (#16678935)
I've also been coding 30 years. I have to use same line at work, and I use next line in my (very large) side projects. Honestly, I don't know what the difference is.
Re:Avoid Else, Return Early (2013) by JackFr on Monday March 26, 2018 @11:02AM (#16679409)
> (very large) side projects

How big once you remove all the extra newlines?

Re:Avoid Else, Return Early (2013) by ccvannorman on Monday March 26, 2018 @11:41AM (#16679680)
LOL. This is the part of the Silicon Valley episode-bar-scene where the fight breaks out.

Thank god OC didn't mention "Just like you move from tabs to spaces."

Re:Avoid Else, Return Early (2013) by torstenvl on Monday March 26, 2018 @03:01PM (#16681502)
I never understood this part of the show. I'd think that the genius inventor of a breakthrough compression algorithm whose WHOLE POINT was high quality lossless compression would prefer spaces over tabs. After all, the compression algorithm can deal with it... without the issues that result from embedding hard tabs in a source file.
Re:Avoid Else, Return Early (2013) by YSFEJ4SWJUVU6 on Monday March 26, 2018 @12:05PM (#16679877)
Well, he shouldn't have, considering it's the other way around.. :-)
Re:Avoid Else, Return Early (2013) by DanHulton on Monday March 26, 2018 @11:31AM (#16679615)
I've actually evolved a hybrid style in my personal projects - next line for functions/classes, same line for blocks.

To me, it looks weird when functions don't have that extra line of space to set off their definition, but if/while/whatever aren't special enough to need that call out.

That said, the most important factor is simple consistency. In my person projects, I have the hybrid style. At work, I use same-line-only. If I'm on a project that has next-line, we all use next-line.

Re:Avoid Else, Return Early (2013) by nitrogen on Monday March 26, 2018 @01:10PM (#16680510)
I think the Linux kernel style is similar. That was the first coding standard I somewhat adopted, but naturally have adapted to many others on different teams over time.
Re:Avoid Else, Return Early (2013) by erik_seaberg on Monday March 26, 2018 @02:03PM (#16681011)
I think this style dates back to K&R C where you (maybe) declared argument types after the parens.

  int max(a, b) 
  double a, b;
  {
      return a > b ? a : b;
  }
Re:Avoid Else, Return Early (2013) by alunchbox on Monday March 26, 2018 @09:48AM (#16678820)
python fans are going like "what are braces?"
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @11:01AM (#16679399)
The irony is that Python actually has open-braces, but they're spelled ":" instead of "{". And the syntax effectively enforces K&R style.

When I write Python I end every block with a "pass" statement so that emacs can auto-indent my code properly. The "pass" statement thus effectively becomes a close-brace. It drives Pythonistas into conniptions, but I never have to worry about reverse-engineering a block of code to figure out how to restore the proper block boundaries after a cut-and-paste has screwed up the indentation.

Re:Avoid Else, Return Early (2013) by kamaal on Monday March 26, 2018 @12:00PM (#16679846)
That's the bad part of not having braces.

If boundaries exist they need to be clear. One shouldn't have to count the tabs that make up the level of indentation.

Its already a challenge reading code. Counting invisible tabs makes it even worse.

Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @12:16PM (#16679973)
Well, you don't have to literally count them, you use the spatial reasoning hardware built in to your retina and visual cortex to just see that this bit of code is further over to the right than that bit of code. But of course that only works for pieces of code that are small enough to fit in your field of view.
Re:Avoid Else, Return Early (2013) by p1esk on Monday March 26, 2018 @03:07PM (#16681550)
All code pieces should be small enough to fit in your field of view :)
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @03:16PM (#16681619)
Try to tell that to my current client :-(
Re:Avoid Else, Return Early (2013) by dorgo on Monday March 26, 2018 @01:56PM (#16680959)
On the other hand high counts of invisible tabs may be a hint that the code needs refactoring..
Re:Avoid Else, Return Early (2013) by tomn on Monday March 26, 2018 @05:49PM (#16682954)
Enforces K&R style? I don't know what you mean...

    def foo(bar)\
    :
        if (bar > 5)\
        :
            return bar + 1;
        #
        else\
        :
            return bar - 1;
        #
    #
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @05:59PM (#16683031)
I'm not sure whether to laugh or cry
Re:Avoid Else, Return Early (2013) by true_religion on Monday March 26, 2018 @12:40PM (#16680196)
Python specific IDEs will smartly indent code upon copy and paste.
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @12:58PM (#16680398)
Sure, but if you somehow manage to screw up the indentation then no IDE can possibly restore it for you because the information needed to do it is lost. And there are a LOT of ways that indentation can get screwed up. If your code ever leaves the Python ecosystem (e.g. gets published on the web) then all bets are off because the entire digital world outside of the Python-sphere is built on the assumption that whitespace is fungible.
Re:Avoid Else, Return Early (2013) by true_religion on Tuesday March 27, 2018 @12:18AM (#16685195)
I don't know about that. I've never screwed up identaton so much that I couldn't correct it simply by selecting everything I just pasted and hitting tab a couple times in my IDE (PyCharm).

Similarly, copying code from StackOverflow, Github and random blogs have all worked without any issues.

So it can't be as easy as you imply.

Re:Avoid Else, Return Early (2013) by lisper on Tuesday March 27, 2018 @01:16AM (#16685461)
What can I say? It happened to me a lot before I started using the pass trick.
Re:Avoid Else, Return Early (2013) by pleasecalllater on Monday March 26, 2018 @02:22PM (#16681166)
Nope. Just try insert code formatter with 8 spaces into code formatted with 4, or 2. IDE will make something that compiles, it doesn't mean that it works properly.
Re:Avoid Else, Return Early (2013) by true_religion on Tuesday March 27, 2018 @12:24AM (#16685227)
Just tried that---typing like mad in a wordpad, with 12 spaces of indentation, then copying into PyCharm and it worked.

Mixed spaces and tabs as a next go, and it worked too---converting the pasted tabs into spaces of my style.

Smart-tabbing is pretty smart.

Granted, I knew that so long as the indents were proper (i.e it compiles), that pasting at that point would give valid code but... I can't see how using braces would have been different. Just because it compiles, doesn't mean it works. I've pasted javascript incorrectly multiple time to produce valid, but incorrect for my needs code.

Re:Avoid Else, Return Early (2013) by jonny_eh on Monday March 26, 2018 @01:09PM (#16680495)
How does an IDE know if a given line belongs in an if block or not?
Re:Avoid Else, Return Early (2013) by true_religion on Tuesday March 27, 2018 @12:21AM (#16685211)
It'll indent up to the most relevant valid block as possible, given your cursor at the moment. If you click within an if block, it'll indent into that if-block.

The indentation strategy also looks to produce syntactically valid code, so long as what you are pasting lacked indentation errors.

Re:Avoid Else, Return Early (2013) by your-nanny on Monday March 26, 2018 @10:21PM (#16684628)
That is clever. Ugly, yes. But clever.
Re:Avoid Else, Return Early (2013) by kabdib on Monday March 26, 2018 @09:52AM (#16678846)
That's fine -- the Python folks have their own share of religious wars (starting with: tabs, or spaces?) :-)
Re:Avoid Else, Return Early (2013) by tom_mellior on Monday March 26, 2018 @10:25AM (#16679116)
Do they? All the Python folks I've ever seen express an opinion on style have said "follow PEP 8".

Unsurprisingly, PEP 8 does have a rule for tabs vs. spaces: https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces (spaces, of course)

Re:Avoid Else, Return Early (2013) by sireat on Monday March 26, 2018 @02:17PM (#16681125)
I love Python but as a long time C programmer I can't understand the preference for spaces in PEP 8.

Tabs are semantic and only take one key press for movement back and forth and to delete.

If you see 1 tab you know it meant one indentation level. With spaces you have to think.

Plus with spaces you are stuck with 2/4/8 spacing(unless you reformat), with tabs you can configure your editor to your preferences.

Re:Avoid Else, Return Early (2013) by slaman on Monday March 26, 2018 @04:01PM (#16681996)
> with tabs you can configure your editor to your preferences.

Configure 1 tab to be 4 spaces.

Sometimes it's easier to go with the flow. I like tabs for the reasons you mentioned, but fixed-width spaces are a bit better for some reasons too. IDE's can do the heavy lifting of re-formatting indentation levels and converting tabs to spaces for me, and it means if I cat a file on a remote server regardless of the bash tab width settings or if I'm in your code or the stdlib it will all make sense.

Re:Avoid Else, Return Early (2013) by tom_mellior on Tuesday March 27, 2018 @04:44AM (#16686502)
> with tabs you can configure your editor to your preferences

Except that you can't because people using tabs will invariably start mixing tabs and spaces because they can't separate indentation from layout. So the code will be messed up unless you configure your editor to someone else's preferences. Also, I'm sure there is some obscure git setting to de-uglify tab users' diffs, but I'd rather not find out.

Re:Avoid Else, Return Early (2013) by e12e on Monday March 26, 2018 @05:14PM (#16682661)
Tabs are semantic. But, when you start down that route, why not dictate record-separotor as replacement for closing brace/block etc?
Re:Avoid Else, Return Early (2013) by pleasecalllater on Monday March 26, 2018 @02:25PM (#16681199)
The PEP8 myth is quite funny. Almost all people I've met who were repeating "follow the pep8" like a mantra, have never read it. The main idea behind pep8 is: be consistent... however even the python library is not consistent. And it looks like no one cares. There was a great moment to make it nice and consistent - creating the python3, where many incompatible changes were introduced. Instead, the mess is like it was, and people repeat the mantra "follow pep8".

Before you say that I'm crazy... please, go and read pep8.

Re:Avoid Else, Return Early (2013) by tom_mellior on Tuesday March 27, 2018 @04:45AM (#16686503)
> please, go and read pep8

OK. What am I looking for?

Re:Avoid Else, Return Early (2013) by jwilk on Monday March 26, 2018 @11:07AM (#16679444)
As a data point, I don't follow (all of) PEP-8.

But tabs vs. spaces is indeed a bad analogy, because of course it's 4 spaces. ;-P

pycodestyle, the Python style checker, has a few checks disabled by default (because there's no consensus they're good ideas), and even some mutually exclusive ones:

https://pycodestyle.readthedocs.io/en/latest/intro.html#erro...

Re:Avoid Else, Return Early (2013) by gambiting on Monday March 26, 2018 @10:06AM (#16678963)
As a C++ programmer I can assure you, I know plenty of individuals with very strong opinions on tabs vs spaces debate.
Re:Avoid Else, Return Early (2013) by drawkbox on Monday March 26, 2018 @10:48AM (#16679276)
And spaces sub factions/opinions on 2 vs 4 spaces.

The correct answer is whatever the project is already using, unless it is new and then you luckily get to choose.

Re:Avoid Else, Return Early (2013) by bunderbunder on Monday March 26, 2018 @10:39AM (#16679205)
These religious wars can largely be swept away with one simple, practical question: What is going to result in less noise in pull requests and the commit history, given that nowadays everyone uses a different editor with different configuration defaults, and is going to result in less time in pull requests squabbling over these kinds of formatting issues, given that nowadays everyone uses a different editor with different configuration defaults?

Tabs vs. Spaces has a fairly good answer that flows out of this way of looking at things. There is a counter-argument to that answer, but it is invalidated if you move toward conventions for argument lists and chained method calls that are also designed to limit noise in the source control system.

IMO, the argument about bracing also has a practical, non-religious answer once you start looking at your coding conventions this way.

Re:Avoid Else, Return Early (2013) by TomMarius on Monday March 26, 2018 @10:06AM (#16678959)
Tabs, everyone can configure them however they want. :-)
Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @10:53AM (#16679321)
I hate being able to configure stuff, I want other people to boss me around from anywhere in the world! Their opinion matters on my computer!
[dead]
Re:Avoid Else, Return Early (2013) by YSFEJ4SWJUVU6 on Monday March 26, 2018 @12:08PM (#16679903)
Why would you ever half-indent a block? For alignment you should of course use spaces (for whole alignment, not just the end, to be very clear).
Re:Avoid Else, Return Early (2013) by kevin_thibedeau on Monday March 26, 2018 @02:14PM (#16681095)
The war is over. Py3 doesn't allow mixed spaces and tabs in the same file. You choose your side and never get to skirmish with the other one.
Re:Avoid Else, Return Early (2013) by jjgreen on Monday March 26, 2018 @11:21AM (#16679544)
Both!
Re:Avoid Else, Return Early (2013) by sethgecko on Monday March 26, 2018 @11:20AM (#16679539)

    >>> from __future__ import braces
    SyntaxError: not a chance
Re:Avoid Else, Return Early (2013) by zerostar07 on Monday March 26, 2018 @10:47AM (#16679265)
but biased fans of both would argue that opening and closing a block in the same indent level (allman-style) is closer to python.
Re:Avoid Else, Return Early (2013) by laythea on Monday March 26, 2018 @02:33PM (#16681261)
Tell them braces are visual indicators of blocks of logic, enabling reasoning more easily on a multi-statement level.
Re:Avoid Else, Return Early (2013) by threatofrain on Monday March 26, 2018 @02:40PM (#16681318)
Lambdas shouldn't be more than one line anyway.
Re:Avoid Else, Return Early (2013) by Bombthecat on Monday March 26, 2018 @02:49PM (#16681406)
I remember in my programming classes at school my teacher was always like: guys!there is space on the right! Use it!

PS: he coded at Hercules the graphic cards back in the day before he got into teaching.

Re:Avoid Else, Return Early (2013) by z3t4 on Monday March 26, 2018 @04:26PM (#16682233)
What is "Hercules the graphic cards" (I only found GPU's when trying Google)
Re:Avoid Else, Return Early (2013) by mr_toad on Monday March 26, 2018 @05:04PM (#16682583)
The Graphics Card (HGC) was essentially an MDA-compatible monochrome card with an added all-points-addressable graphics mode.

https://en.wikipedia.org/wiki/Hercules_Graphics_Card

Re:Avoid Else, Return Early (2013) by kstenerud on Monday March 26, 2018 @09:53AM (#16678855)
I've done a number of informal tests on friends and family over the years regarding brace placement, and it's always been the same: For someone with NO experience programming (i.e. looking at what to them seems like a bunch of gobbletygoop - and what's a "text editor"?), adjacent braces make it appear more readable than same-line braces.

It's only people who cut their teeth on adjacent bracing that find it more readable.

Re:Avoid Else, Return Early (2013) by falcolas on Monday March 26, 2018 @10:18AM (#16679058)
Why optimize for the lowest common denominator?

I've seen both, and the reduction in vertical whitespace matters more to me than the readability to an untrained individual.

Re:Avoid Else, Return Early (2013) by hailk on Monday March 26, 2018 @11:03AM (#16679412)
I'll be flipping the point around by saying that if you are concerned about vertical whitespace reduction, you should probably look at the granularity of your files (debatable based on programming environment). Also we could be catering to the lowest common denominator by worrying about people with untrained symmetric sensibility.
Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @10:54AM (#16679328)
>Why optimize for the lowest common denominator?

You seem to be erroneously equating a coincidence with a preference.

Re:Avoid Else, Return Early (2013) by z3t4 on Monday March 26, 2018 @04:33PM (#16682312)
Tip: Tilt your wide screen so you get more vertical real estate.
Re:Avoid Else, Return Early (2013) by ainiriand on Monday March 26, 2018 @10:59AM (#16679376)
'I've done a number of informal tests on friends and family'

You should have a pretty cool family!

Re:Avoid Else, Return Early (2013) by boomlinde on Tuesday March 27, 2018 @10:22AM (#16688653)
The level of complexity obviously doesn't change with a line break
Re:Avoid Else, Return Early (2013) by JKCalhoun on Monday March 26, 2018 @10:37AM (#16679193)
I agree. I'm not looking to increase code density. I leave empty lines between logical sections within functions to allow the code "breathe". It helps me parse my own code.
Re:Avoid Else, Return Early (2013) by zanny on Monday March 26, 2018 @10:47AM (#16679263)
I used Python enough that braces are completely meaningless to me in languages with them (ie, everyone else). Indentation is my mental delineation of code blocks. I just have to write these {} things all over the place because every other language is a gramatically bloated mess.

The advent of automatic code formatting (and Python pioneered this in many ways with pep8, but even huge C++ projects are realizing the value in clang-format, clazy, etc) put the nail in the coffin of arguments against whitespace blocking. You can have an enforced uniform style throughout a project now, with no ambiguity, and in such contexts using whitespace as a block delimiter also has no ambiguity. It just takes advantage of formatting already being done to avoid redundant glyph use.

Re:Avoid Else, Return Early (2013) by btilly on Monday March 26, 2018 @11:04AM (#16679414)
Let's see. The Unix indent utility was what, mid-1980s? GNU indent came out in 1989. Python was released in the mid-1980s. PEP-8 was released in 2001.

Yup, Python clearly pioneered automatic code formatting. Nobody had done it before them...

Re:Avoid Else, Return Early (2013) by Someone on Monday March 26, 2018 @12:07PM (#16679902)
Lisp pretty printing was available in February 1973 (http://www.softwarepreservation.org/projects/LISP/MIT/AIM-27...).

That document states Bill Gosper wrote one of the first pretty printers, but doesn’t give a date for it.

I would think it is much older, as writing a simple lisp pretty-printer is easy and reading lisp without autoformatting is “less than ideal”.

Re:Avoid Else, Return Early (2013) by sedachv on Monday March 26, 2018 @04:41PM (#16682380)
> I just have to write these {} things all over the place because every other language is a gramatically bloated mess.

In what way are context-free languages "a grammatically bloated mess?" Whitespace delimited languages like Python have context-sensitive grammars. Now that is a mess.

Re:Avoid Else, Return Early (2013) by WorldMaker on Monday March 26, 2018 @05:23PM (#16682742)
Most bracket languages aren't strictly context-free either (the Dangling else problem [1] alone somewhat guarantees that most programming languages are not strict, unambiguous CFGs in a mathematic sense). There's no right/wrong/superiority in context-free versus context-sensitive, they are tools in the toolbox and not really "levels in a superiority hierarchy".

[1] https://en.wikipedia.org/wiki/Dangling_else

Re:Avoid Else, Return Early (2013) by sedachv on Monday March 26, 2018 @09:06PM (#16684258)
> There's no right/wrong/superiority in context-free versus context-sensitive

Dismissing the whole field of formal language theory with that statement is so bogus, it "is not even wrong." There are tools and techniques for parsing context-free languages that are impossible with context-sensitive languages. Parser generators, structured editing, metacompilers, composable grammars; those are all impossible with context-sensitive languages. Context-free languages are faster to parse, easier to write compilers/interpreters for, and much easier to write tooling for (editors, linters, etc).

Ambiguous context-free grammars are a very different thing from context-sensitive grammars. You can't point to the former and say "so whitespace sensitivity is ok."

Re:Avoid Else, Return Early (2013) by WorldMaker on Tuesday March 27, 2018 @11:14AM (#16689260)
I'm not dismissing anything. I'm pointing out the boundaries are fuzzier than hard cut-offs and it's weird to assign moral superiority to one side of the grass or the other. Most parser generators/composable grammar tools, etc, have tools for dealing with context sensitive sections of a language, just as they use regular expressions for tokenizing and allow regular language sub-languages.

I can point to ambiguous context-free grammars and say whitespace sensitivity is ok, because the tools to fix the one are often the same to fix the other. (Ambiguous context-free grammars do turn out to make context sensitive languages, because it's a blurry line in the grass between them.)

Whitespace sensitivity is one of the easier context sensitivity challenges to embed as a sub-language in an otherwise (mostly) context-free language, because you can represent it entirely as pseudo tokens from a simply modified lexing phase in a traditional CFG parser. Python is an easy and clear proof, as that is exactly what it did (Python is also not purely a CFG after whitespace tokenization because of also how it handles the dangling else, but we've already mentioned those weeds).

Even if you don't find the boundaries between the classes of languages fuzzy (and Parser Combinators and PEG grammars have a lot to say here), context-sensitive languages are a tool in a growing toolbelt, not a "complex" evil to be demonized.

Re:Avoid Else, Return Early (2013) by sedachv on Tuesday March 27, 2018 @03:17PM (#16691845)
> it's weird to assign moral superiority to one side of the grass or the other

Algorithmic complexity is not "moral superiority." The whole point of formal language theory is to show how the different languages differ in terms of how difficult they are to work with.

> just as they use regular expressions for tokenizing and allow regular language sub-languages

That is because regular languages are a subset of context-free languages. There is nothing special to "allow" there.

> I can point to ambiguous context-free grammars and say whitespace sensitivity is ok, because the tools to fix the one are often the same to fix the other.

No they are not. Precedence rules and extra lookahead will help with ambiguous context-free grammars but not with context-sensitive ones. There is also a whole class of algorithms, such as GLR, specialized to handle ambiguous CFGs efficiently.

> Whitespace sensitivity is one of the easier context sensitivity challenges to embed as a sub-language in an otherwise (mostly) context-free language, because you can represent it entirely as pseudo tokens from a simply modified lexing phase in a traditional CFG parser.

That statement is nonsense. There is no way to "embed" a context-sensitive language in a CFG. What you are saying is that it is "easy" to make a whole context-sensitive parser just to produce a context-free language so you can pass that to another CFG parser. That is not "easy," that is bolting crap onto other crap.

Re:Avoid Else, Return Early (2013) by WorldMaker on Tuesday March 27, 2018 @03:54PM (#16692158)
> Algorithmic complexity is not "moral superiority." The whole point of formal language theory is to show how the different languages differ in terms of how difficult they are to work with.

Chomsky's hierarchy was designed for production from categories of languages. That it doubles as a useful rule of thumb for algorithmic complexity in parsing is a fascinating dualism in mathematics. That those same rules of thumb reflect basic automaton abstractions is even more fascinating. This is also where it seems the clearest analogy lies to why I find your arguments to "complexity" so useless. It sounds to me like a strange steampunk form of the "640k is all anyone will ever need" fallacy: "why use Turing machines when Pushdown automatons will do just fine?"

Yes, there are a lot of great tools for working with CFGs, just as we've pushed Regular Expressions far past the boundaries of formal Regular Languages, we push these same tools past the boundaries of formal CFGs. We aren't actually constrained by the limits of only using Deterministic Finite Automata or Pushdown Automata in our programming, we have vast Church-Turing–level state machines at our power completely and easily capable of tracking contexts/states.

The "context" in a whitespace-sensitive language is "how many spaces have you seen recently?". This is not a hard question, this is not some mystical "context sensitivity" that needs an entire "context-sensitive language parser". It's a handful of counters. That is the very definition of easy, that's the built-in basics of your average Church-Turing machine, keep a number on the tape and update it as necessary.

Python's specification for the language defines the language in a CFG BNF just as the majority of brackets languages. The one concession to its whitespace sensitivity is that it expects from its lexer INDENT and DEDENT tokens. Those tokens are added simply by counting whitespace between lines, seeing if there is a < or > relationship. After those tokens are in the stream (along with the rest of the tokens defined in their associated (Mostly) Regular Languages) it is parsed by whatever CFG automaton/algorithm the Python team wants (LR, GLR, LALR, PEG, etc). That's not "bolting crap onto other crap" by most stretches of the imagination, that's using a pretty simple stack of tools that any programmer should be able to (re-)build, and not one at all more complicated than (and in fact built entirely inside) the classic tokenizer/lexer feeding a parser stack.

Re:Avoid Else, Return Early (2013) by sedachv on Tuesday March 27, 2018 @05:51PM (#16693076)
> This is also where it seems the clearest analogy lies to why I find your arguments to "complexity" so useless.

Honestly, from everything you have posted so far, I really get the impression that you have never worked on programming language parsing, or with large code bases. The difference in algorithmic complexity is not "useless," it is the difference between being able to compile millions of lines of code in a few seconds on modest hardware, and needing a cluster just to get your builds done:

https://community.embarcadero.com/blogs/entry/compiling-a-mi...

Re:Avoid Else, Return Early (2013) by WorldMaker on Wednesday March 28, 2018 @10:47AM (#16698063)
Not saying algorithmic complexity is useless, only that it is a starting point in a discussion, not the end of the discussion. I've repeatedly used the analogy "tools in the toolbelt" as my return point in the discussion, so maybe you are missing plenty of my actual words in your "impression" that swings wildly toward an ad hominem attack.

If we want to fight anecdote for anecdote, I've certainly seen millions of lines of code of Python analyzed ("compiled") in seconds on modest hardware, rather than a cluster. Differences there are more in the static typing versus dynamic typing, and strongly compiled versus weakly compiled/primarily interpreted there. Maybe a better anecdote is millions of lines of Haskell? Seen that too. Whitespace-sensitive languages scale just fine.

I can understand it if you want to admit that you just don't like whitespace-sensitive languages for personal reasons, but there aren't very good technical reasons and you seem unhappy with my explanations of why that is the case. I'm not sure how much more I can try to explain it, and given you seem close to resorting to personal attacks I'm afraid this is likely where the conversation ends.

Re:Avoid Else, Return Early (2013) by princekolt on Monday March 26, 2018 @09:59AM (#16678906)
I agree. This is especially good when you have long method names and the method declaration might need more than one line. Having the opening brace on a new line clearly separates where the function/method declaration ends and the body begins.
Re:Avoid Else, Return Early (2013) by Traubenfuchs on Monday March 26, 2018 @10:25AM (#16679114)
As someone who started out with C# and learned Java later, let me tell you: This is nonsense. You can get used to both easily, but only one of them saves space.
Re:Avoid Else, Return Early (2013) by melling on Monday March 26, 2018 @10:30AM (#16679155)
The One True Brace Style:

https://softwareengineering.stackexchange.com/questions/9954...

The name tells you that it won.

Re:Avoid Else, Return Early (2013) by JoeAltmaier on Monday March 26, 2018 @10:33AM (#16679171)
That link's favorite response actually refutes that brace style matters at all. No correlation with bug frequency detectable. So its all religion.
Re:Avoid Else, Return Early (2013) by ergothus on Monday March 26, 2018 @11:47AM (#16679727)
I wish this sentiment were expressed more often but also expressed less judgmentally and given more depth than sometimes occurs..

To say it is religion isn't to say "your preference is dumb". In my own code preferences I try to maintain a split between "have a reason I have confidence in" and "personal preference". And "easier to read" is almost always in the latter. (If you can say WHY, that is the actual reason, but your actual reason still has to be provable.)

Given the difficulty of finding good research and the inherent difficulty of the research (if familiarity is a big component to preference, and a proven ability to mentally parse code required to judge, then good luck running a control group) I have a lot of items I have confidence in being provable without having actual proof, but the lengthy preference list also means I'm willing to accept that each of those can switch columns.

Way too many of our "easier to read" defenses are really "it is easier to read because I'm familiar with it. I personally think camelCase is terrible - language has spaces for reasons! - but there is no denying that it is very common and that the vast majority of coders learned it first, an unfortunate self perpetuating cycle.

Nonetheless, shown research that said I was wrong (assuming said research had taken familiarity into account) I would change my stance rather than dig in my heels...even if that change were to only add "but that's just me" to the end of it.

Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @11:00AM (#16679389)
It's not "religion" if there's a technical reason to choose one method over another.

A later comment on the SO post described how K&R can have maintenance costs because when moving things around it's more difficult to tell where a statement ends, and can accidentally cause side effects.

Re:Avoid Else, Return Early (2013) by JoeAltmaier on Monday March 26, 2018 @12:16PM (#16679971)
Not reflected in the bug statistics, according to the post. So if the effects exists, its negligible.
Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @01:16PM (#16680580)
>So if the effects exists, its negligible.

The only thing being considered was "bugs in the final code", not "time and effort in maintenance."

Re:Avoid Else, Return Early (2013) by youngtaff on Monday March 26, 2018 @09:57AM (#16678897)
I'm with you on Allman style braces, prefer it and find them much easier to read.

As everyone else seems to use K&R I've just had a adapt over time :-(

Re:Avoid Else, Return Early (2013) by bunderbunder on Monday March 26, 2018 @10:41AM (#16679217)
What's interesting to me is, anecdotally, the people I know who are the biggest K&R haters are people who started with K&R, then had to switch to Allman for a significant length of time, and then had to switch back to K&R.
Re:Avoid Else, Return Early (2013) by drewmate on Monday March 26, 2018 @11:57AM (#16679802)
That probably tells you more about the haters than the brace style. That was my career path, and while I've gotten used to reading Allman braces, I've heavily relied on tooling (especially Visual Studio) to auto-format my K&R (or 1TBS) braces for whatever project I'm in. Same thing with tabs/spaces.

There are lots of sane ways to read code, but consistency is key. It's hard to change muscle memory in how I type, though, so I think auto format is the way to go.

Re:Avoid Else, Return Early (2013) by drawkbox on Monday March 26, 2018 @10:41AM (#16679216)
A pro maintains consistency though so that is good.

I like One True Brace Style (1TBS) with an uncuddled 'else', which has else/else if on new-line with the break before it (similar to Stroustrup K&R without any same line properties, one thing per line, no bracket-less statements) and setup the standard that way if I am designing it, but do Allman or whatever variant the codebase uses if it already exists.

Re:Avoid Else, Return Early (2013) by CodeWriter23 on Monday March 26, 2018 @12:26PM (#16680054)
I’m with you on this, and all that other stuff you said. But really, on the topic of braces, brackets and parentheses, any decent editor will highlight the matching brace for you.
Re:Avoid Else, Return Early (2013) by scythe on Monday March 26, 2018 @11:38AM (#16679655)
I don't work on an 80/24 terminal either, but I find that if there's too much text on the screen at a time, my eyes glaze over and don't read it all. So I work with larger fonts and use a terser style with a 40-line rule for functions that aren't switch statements.
Re:Avoid Else, Return Early (2013) by noobermin on Monday March 26, 2018 @03:51PM (#16681907)
I'm often coding on a server that has 4-6 tmux windows along with another terminal window for my local machine. I rarely have only one wall of text open, so I effectively work on mini-terminals often.
Re:Avoid Else, Return Early (2013) by redleggedfrog on Monday March 26, 2018 @09:55AM (#16678867)
Yeah, I agree with the list above except for that one. It always bugged me that idiomatic Javascript was like this. I guess Javascript just bugs me in general, though, too.
Re:Avoid Else, Return Early (2013) by hailk on Monday March 26, 2018 @10:51AM (#16679301)
I see it the same way. It offers symmetry. The method name can be as long or short. At least the start of the code block and the end is easily visible since the indentation level remains same. Gives a sense of Python readability.
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @10:58AM (#16679361)
Huh? My emacs auto-indents both styles in exactly the same way:

    void foo() {
      code...;
    }
    
    void foo()
    {
      code...;
    }
Re:Avoid Else, Return Early (2013) by hailk on Monday March 26, 2018 @11:12AM (#16679483)
The start of the code block is signified by the opening brace, which starts at the end of the method name in the first case, hence breaking symmetry.
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @11:19AM (#16679532)
To my eye, the start of the code block is signified by the indentation, i.e.:

    stuffstuffstuff....
      stuffstuffstuff...
So I read C and Python (and Lisp) code the same way. A naked open brace looks jarring and ugly to me. It also increases the separation of other related parts of the code, i.e.

    if (...) {
      while(...) {
        do(...) {
vs

    if (...)
    {
      while(...) {
      {
        do(...) {
The latter seems unnecessarily wasteful to me.
Re:Avoid Else, Return Early (2013) by Merad on Monday March 26, 2018 @01:57PM (#16680960)
K&R isn't that terrible when code is neat and clean, but it starts to have trouble IMO particularly when declarations or conditions wrap to multiple lines. Compare the following examples... I personally have to stop and read the code to find the blocks with K&R braces, vs being able to see them at a glance with Allman.

    void MyLongMethodName(SomeLongParamType param1, SomeOtherLongParamType param2,
        YetAnotherLongParamType param3) {
        if (longContrivedVariableName1 == longContrivedVariableName2 && 
            longContrivedVariableName1 != longContrivedVariableName3) {
            // do stuff
        }
    }
    
    void MyLongMethodName(SomeLongParamType param1, SomeOtherLongParamType param2,
        YetAnotherLongParamType param3) 
    {
        if (longContrivedVariableName1 == longContrivedVariableName2 && 
            longContrivedVariableName1 != longContrivedVariableName3) 
        {
            // do stuff
        }
    }
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @02:14PM (#16681098)
Emacs autoindent to the rescue:

    void MyLongMethodName(SomeLongParamType param1, SomeOtherLongParamType param2,
                          YetAnotherLongParamType param3) {
      if (longContrivedVariableName1 == longContrivedVariableName2 &&
          longContrivedVariableName1 != longContrivedVariableName3) {
        // do stuff
      }
    }
Re:Avoid Else, Return Early (2013) by Merad on Monday March 26, 2018 @03:23PM (#16681656)
IME the more complex your code formatting, the less likely people are to do it, especially after your code has been touched by dozens of different people with a dozen different editors.
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @03:30PM (#16681708)
This is why emacs is the One True Editor ;-)
Re:Avoid Else, Return Early (2013) by Rusky on Monday March 26, 2018 @03:59PM (#16681984)
I treat the parentheses the same way I treat the braces:

    void MyLongMethodName(
        SomeLongParamType param1,
        SomeOtherLongParamType param2,
        YetAnotherLongParamType param3
    ) {
        if (
            longContrivedVariableName1 == longContrivedVariableName2 &&
            longContrivedVariableName1 != longContrivedVariableName3
        ) {
            // do stuff
        }
    }
I also make liberal use of temporary variables:

    void MyLongMethodName(
        SomeLongParamType param1,
        SomeOtherLongParamType param2,
        YetAnotherLongParamType param3
    ) {
        auto condition1 = longContrivedVariableName1 == longContrivedVariableName2;
        auto condition2 = longContrivedVariableName1 != longContrivedVariableName3;
        if (condition1 && condition2) {
            // do stuff
        }
    }
Vertical space is allocated to the actual parameters and conditions, and the extra syntax-only lines exist only to separate chunks. This works well with "paragraphs":

    void MyLongMethodName(
        SomeLongParamType param1,
        SomeOtherLongParamType param2,
        YetAnotherLongParamType param3
    ) {
        auto condition1 = longContrivedVariableName1 == longContrivedVariableName2;
        auto condition2 = longContrivedVariableName1 != longContrivedVariableName3;
        if (condition1 && condition2) {
            // do stuff
        }

        auto someData = buildSomeData();
        doSomethingWith(someData);
        while (someCondition) {
            // loop
        }

        finishUp();
        return someData;
    }
Re:Avoid Else, Return Early (2013) by reaperducer on Monday March 26, 2018 @11:18AM (#16679522)
Great for you. I guess you never have to share your code with other people; especially those who aren't using emacs.
Re:Avoid Else, Return Early (2013) by reaperducer on Monday March 26, 2018 @11:05AM (#16679422)
I agree with you 100%. It's all about being able to skim through code to find what you want and understand structure quickly.

Allman is great for this. Though I'm a Whitesmiths guy, myself. Still, same idea.

/36 years of commercial coding here.

Re:Avoid Else, Return Early (2013) by npsimons on Monday March 26, 2018 @05:28PM (#16682789)
> I'm not working on an 80x24 terminal anymore.

Even with 80x24 (don't ask), I fully agree with Allman style braces.

Re:Avoid Else, Return Early (2013) by icedchai on Monday March 26, 2018 @03:07PM (#16681553)
Ok. I've been writing code for 25+ years, and disagree.
Re:Avoid Else, Return Early (2013) by zerostar07 on Monday March 26, 2018 @10:44AM (#16679243)
for a vi user, visually selecting a block to its matching end for cutting/pasting is the equivalent of coding photoshop
Re:Avoid Else, Return Early (2013) by khedoros1 on Monday March 26, 2018 @03:44PM (#16681833)
v%y works well enough, seems like.
Re:Avoid Else, Return Early (2013) by pebcakID10T on Monday March 26, 2018 @05:27PM (#16682782)
NoooOoooooOOOooooOOOooo! I hate Allman braces! My hatred burns with the fire of a thousand suns!
Re:Avoid Else, Return Early (2013) by tytytytytytytyt on Monday March 26, 2018 @12:49PM (#16680292)
I agree! The secret to code readability isn't squeezing out all whitespace, vertical or horizontal.