7 ms·
because readability is one of the most, if not the most, important quality for mainstream programming language adoption.
by alienreborn 9y ago
because readability is one of the most, if not the most, important quality for mainstream programming language adoption.
- cremp 9y agoGolang forces readability; might want to give it a shot.
- lilbobbytables 9y agoJust the other day I started looking back into Golang. Syntactically, and with Gofmt, it is quite readable. But then they have to go and ruin it by making single letter variables a common thing.
- pyre 9y agoBut significant whitespace means that I can't write code like this: sub (@) { return map { "@{[ $_[0] . $_ ]}" } @$_ }; Disclaimer: The majority of the programmers that I have known in real life that have a significant (and very vocal) dislike of Python's whitespace requirements have all written code like that above (this is my from-memory reproduction of actual code that one of them wrote). Python sucks because it's "restricting" them from being "expressive" with their code.
- minimaxir 9y agoFor statistical programming in particular, readability of your code by others is the most important part of the coding style.
- BurningFrog 9y agoThat's absolutely true of any programming.
- neuronexmachina 9y agoYep. An order of magnitude more time is spent reading and maintaining a line of code than is spent writing it. Readability adds a tiny amount of time to the writing, but makes reading and maintainability so much better.
- lilbobbytables 9y agoGlad I read your disclaimer before responding. :P Was just reviewing colleague's code with them the other day. Hit some blocks like this (not quite as offensive, but tackling a bit in one line), and hell if they knew what was going on in their own code.
- AlexCoventry 9y agoWhat does that do?
- deleted 9y ago[deleted]
- jwilk 9y agoIt's syntactically correct, but pretty much nonsensical. (I guess the original code did something useful and looked very similar to this.)
- falsedan 9y agoSo let's break it down: sub It's an anonymous sub (@) It has a prototype that says it accepts a list of arguments (this is the default in perl though). Prototypes are mostly for giving hints to the compiler on how to unambiguously parse calls to your function later. For an anonymous sub, it's pointless because you assign its value to a variable, and then brackets are required when calling it. return It returns the result of the map (this default in perl is to return the result of the last expression & everything is an expression though) map same as python map(code, list) { "@{[ <some stuff> ]}" } This is a particular perl goof for interpolating array lookups into a string. Usually, perl's sigils let variables be interpolated without any issue, but since we're using $_[0] to use the first element of the @_ array, it's ambiguous with $_ . '[0]'. The @{} dereferences the contents of the braces as an array, and [] creates a new arrayref. The arrayref has one element, and perl's default stringification of an array (join them all with no separator) means that it… iterpolates the thing inside the @{[]} into the surrounding string. This is pointless though, since it's the only thing in the string. The author might have been trying to give a string context to force stringification of the thing, but the dot operator already provides that context. $_[0] . $_ This is a string of the first element of … hmm. Not of the currently-mapped element (that's $_). $_[0] refers to the first element of @_. This makes a new value which is the concatenation of the first element with the current element. @$_ This is a short way of writing @{ $_ }: dereference the arrayref that's stored in $_. But $_ is not set anywhere, so I assume this is a typo of @_ I would have written this as: my $prefixer = sub { my($prefix , @rest) = @_; return map { $prefix.$_ } @_ }; And it would work like this: say $prefixer->('Hi_', 1, 'str', 7); > 'Hi_Hi_Hi_1Hi_strHi_7' The 'best' way to write it would have been: sub { $_[0].join($_[0], @_) }
- seanmcdirmid 9y agoAlternative syntax for short one liner functions (e.g. As recently added to C#, always in scala) can reduce a lot of this friction without sacrificing formatting guidelines. However, I'm not sure if it's in python's philosophy to explore such things.
- someone1012 9y agoHave you ever looked e.g. at the distutils code or tried to figure out how to use it without using Stackoverflow? That function is easy to parse if you take 20 min learning the syntax. Object oriented monstrosities aren't.
- wyldfire 9y agoPhew -- it's an automatic filter. Sadly, you can write code just like that in Python. I don't know what that perl snippet does, my caches have long since flushed ;) -- but here's a Python snippet that's relatively dense: def foo(y): return lambda x: {str(i) : '_{}'.format(i**y) for i in range(x)}
- reificator 9y agoI've never posted about whitespace sensitivity before today, so maybe I don't fit in the group you're pointing at, but I don't like Python's whitespace sensitivity either. Not because I want to write everything on one line, but because it's an extra step when trying to understand what code does. For instance, a piece of code is misbehaving, not being called as part of the block it visually appears part of. In a bracket/paren language I can highlight one bracket and my editor shows which one matches. In Python I have to figure out what the indentation is built from, spaces or tabs, and make sure there's no ambiguity there. We have developers here that are claiming they wrote things in Python for their PHD, and yet when I look at their code in any language there are tabs and spaces mixed in the same line, with inconsistent indentation. Multiple people have tried to express the issue or teach ways to deal with it, to no avail. The fact of the matter is that whitespace is by definition invisible, and that makes it hard for people to grasp its importance. Basing control flow on invisible characters is asking for trouble. To quote PEP 20: > Explicit is better than implicit. Brackets are explicit, whitespace is implicit. You only can see whitespace by the gaps between visible characters, and then you have to guess. Is it an actual U+0020 like you see 99% of the time? Is it an nbsp? It it a tab that happens to stop after the width of a space? You don't know until you encounter an error or turn on whitespace rendering. Was it copied and pasted from www.python-help-7575.com, which uses a platform that favors punctuation spaces? Was it copied from a site that didn't wrap their code in <code> or <pre>? (I see this all the time in comment systems on programming blogs) Should we claim that copying and pasting code in a beginner targeted language is something you do at your own risk?
- jplatte 9y ago> In Python I have to figure out what the indentation is built from, spaces or tabs, and make sure there's no ambiguity there. The following program throws a TabError in Python 3: def demo(): print("1 tab") print("8 spaces") It does work in Python 2, but I'm pretty sure the majority of developers have 4 space wide tabs by now, meaning they won't be able to mix tabs and spaces in Python 2 code either. EDIT: So Hacker News has code blocks, but doesn't strip the indentation that is required to introduce them. So you'll have to lead the 2 leading spaces in each line if you want to try this out for yourself.
- mark-r 9y agoAs much grief as Python gets for significant whitespace, I think it really helps the readability. Not only does it enforce what should already be good indenting habits, but it eliminates the noise of braces.
- wickawic 9y agoI always figured the people complaining about the whitespace thing are people whose code I really don't want to be reading anyway
- runevault 9y agoThe problem isn't whitespace period, its that if you aren't careful the significant whitespace can bite you, for example people sometimes accidentally mix tabs and spaces, which can get... ugly. Personally I prefer {} blocks so I don't have to know what sort of white space is creating the indentation, even though I do miss using python sometimes and may come back to it for the right project.
- redblacktree 9y ago> accidentally mix tabs and spaces, which can get... ugly. This is trivially solved. You can find and replace with sed[0] if you want to be fancy, or just use your favorite text editor/IDE. The teams I've worked on have never had a problem with this. There is always an established convention in a project, and if you're breaking the convention -- e.g. because you prefer tabs -- you're doing it wrong, just by virtue of breaking the convention. If your project is new and you need to set a convention, do that. (also, PEP8 recommends spaces, so there is an authority to reference as a tie-breaker) [0]: something like: find ./ -type f -exec sed -i -e 's/ / . /g' {} \;
- devrandomguy 9y agoFurthermore, there is no need for anyone to care what spacing scheme their team uses, after configuring their editor on day 1 with that team. I have Vim convert Python code to tab indentation when opening files, and have it convert to 4-spaces indentation when saving a Python code file. No one should be wasting even a flicker of brain activity over indentation in the 21st century, regardless of how backwards everyone else's preferences are. While you're at it, please also s/\s+$//g on save. Those unintentional trailing spaces really clutter up Git history, and that comprehensive, clean history is what enables the team leads to do their job without bugging you on Slack. It also signals carelessness, or at least a lack of care for personal tools, and has your name attached to it.
- tw1010 9y agoWhy do us engineers have a tendency to oversimplify everything to one big major cause like this? I see it all the time. Obviously in this case this is not the only reason. The real reason is a mix of many influences.
- acomjean 9y agoWhile better than perl, as someone who has to maintain others python code the tendency to want to write stuff compactly makes it difficult to maintain than other languages in my opinion (the 5 others I work with). I've found other languages easier to read. It might just be the code base I'm working with..
- kodablah 9y agoI think there are two forms of readability: localized readability and whole-program readability. Python only satisfies the former. Your statement about readability being one of the most important qualities I believe is true, but more for whole-program readability in my experience.
- JoeMalt 9y agoAgreed. Working on a large Python codebase, I find myself greatly missing Java's improved static analysis, even for simple things like "find the definition of this function".
- andybak 9y agoIf you aren't ideologically opposed to the use of an IDE then PyCharm solves this for the vast majority of cases. I get ~90% of the editor functionality I'd get in a statically typed language in PyCharm. Automated refactors and all that jazz...
- Alex3917 9y agoWith Python even the code I wrote as a junior developer basically stands up five years later, looking back as a senior developer. There are definitely things I would have done differently, but there isn't any code where it would take several weeks just to figure out what the hell was going on and then another several weeks to make some modest improvements.