r/cpp • u/pfultz2 • Jan 26 '15
Why a C++ programmer might say "I could never write Python"
http://jvns.ca/blog/2015/01/24/why-a-c-plus-plus-programmer-might-say-i-could-never-write-python/17
u/Raptor007 C++03 Jan 26 '15 edited Jan 26 '15
It's a good point about compiled vs interpreted languages in general. But...
So maybe the C++ programmer who says she can't write Python is really saying "Writing safer code in a dynamic language is a skill that takes time to learn! I have not yet learned it! I would be too scared to commit to writing a reliable Python program right now!"
I'm not fond of the tone of this; the author implies it's somehow a fault of the programmer to want an additional source of error checking, particularly one as powerful and well-developed as a mature C++ compiler. Compiler tests are like having another set of eyes skim your code for common mistakes before you run it.
So you need to spend all your time learning how to write correct code without a static type checker, partly by writing a much better test suite, by using a linter, etc.
The author even implies that if we want something like the -Wall test suite, we should just write it ourselves in our Python projects; for a language that tries to "simplify" programming, reinventing the wheel in this way would waste a lot of time and effort, and likely yield inferior results.
All that said, my main beef against Python is the meaningful whitespace. If they decide to fragment again (Python 4) I hope they replace it with curly-brace scoping.
9
u/donvito Jan 26 '15
Writing safer code in a dynamic language is a skill that takes time to learn!
More like a waste of precious life time. Why should I spend thousands of man hours to learn something I get for free from a compiler? (Btw. I don't believe you can really learn to actively avoid typos and brainfarts).
5
5
u/Wriiight Jan 26 '15 edited Jan 27 '15
I'd be OK with meaningful whitespace if it weren't for the tab-vs-spaces problem. I'm sure it is easy to deal with if you write python all day, but if you haven't taken the time to customize your tools towards python development, it just gets you over and over again.
Edit: Read all the comments below and try to convince yourself it is not a problem. You have an invisible character that can ruin everything in an interpreted language. It used to be a known annoyance in formatting consistency, but Python decided to turn it into a source of syntax errors. For an example of just using editors as they are set up by default, notepad++ will auto-indent using tabs, even if the prior indent is spaces. I wonder why python doesn't decide to standardize on a tab being syntactically equal to 4 spaces. I suppose then the tab-terrorists win.
2
u/o11c int main = 12828721; Jan 26 '15
It's not a problem with python, they have succesfully reached consensus on that "tabs are evil, use 4-space indents".
Since indenting with 4 spaces is the most sane policy for any language, there's no customization to do ...
2
Jan 27 '15
they have succesfully reached consensus on that "tabs are evil, use 4-space indents
Real pythonistas - yes, but I constantly see python code written by part time python programmers who mix tabs and spaces in python code and they have hard time understanding why that is bad.
1
u/o11c int main = 12828721; Jan 27 '15
Okay, so make tabs a hard error (python has been considering this for a while). Problem solved.
3
u/ukalnins Jan 27 '15
Yeah, because breaking backwards compatibility has worked so well for Python in the past.
1
u/lacosaes1 Jan 27 '15
The main frameworks/libraries are supporting Python 3.x now. Am I missing something here?
5
u/ukalnins Jan 27 '15
Python 3.0 was released more than 6 years ago. Yet large portion of new code is still written in python 2.7.
1
u/lacosaes1 Jan 28 '15
There's a price you pay for breaking backwards compatibility. But I would say that Python 3.0 was a successful case even though it wasn't an ideal one. BTW:
Yet large portion of new code is still written in python 2.7.
How big is the percentage?
1
1
u/Wriiight Jan 27 '15
We force you to indent, but be damn sure not to hit tab..... Yeah, there is nothing wrong with that logic.
4
u/o11c int main = 12828721; Jan 27 '15
What obscure editor are you using that doesn't have an "expand all tabs to N spaces" option?
Literally the only editor I know of that doesn't have that is
notepad.exe. I know, because it's the first thing I look for whenever I use an editor for the first time.2
u/Raptor007 C++03 Jan 27 '15
Personally, I don't get why anyone would prefer spaces over tabs. It's easy to be off by one space, but tabs are very clearly lined up or not. Also, it's just one character to move your cursor rather than 4.
5
u/gfixler Jan 27 '15
Personally, I don't get why anyone would prefer spaces over tabs.
Because of the literally hundreds, if not thousands of files I've gotten from archives, the internet, online acquaintances, and coworkers over the last 24 years from people who cannot if their life depended on it not mix tabs and spaces. Also, it really upsets me on a fundamental level that one character in my file has a variable with, and every other one sits nicely in the grid.
Not long ago I went to rosettacode.org to grab some example code for a demo I was going to do - believe it or not, on tabs vs. spaces. I chose the n-queens problem, and scrolled down and copied the Lua version. I was doing a little demo of converting tabs to spaces and back with regex, and it wasn't working as expected, and then it hit me, and I said out loud "Oh, you've got to be kidding me." In one shot, I'd picked this mess - that's me searching the code in Vim for
\t. I had a coworker who had code way worse than this all the time. It happened because he used the mouse to drag code around all over the place all day long. He had tons of trailing tabs and spaces, too, because his editor didn't show him trailing spaces (my Vim does), and that is not at all uncommon. This is why I don't like tabs. I want WYSIWYG, everywhere.It's easy to be off by one space
It's almost impossible for me to be off by one space. When I type
def function ():in a Python file in Vim, and hit enter, it auto-indents me 4 spaces on the next line. This happens for anything requiring indenting. I rarely even use the tab key, because all day long Vim just automatically tabs itself in where appropriate. This works even better in Clojure, but it also works well in Haskell, javascript, Common Lisp, C, C++, Assembly, and hundreds of other languages. I was surprised to find out not that long ago that emacs users hit tab on every new line to get proper indentation. It chooses the right indent level at that point, but hitting tab on every line is crazy to me.Also, it's just one character to move your cursor rather than 4.
When I'm done typing lines at the current indent level, I hit backspace (obviously on the next, blank line), and it jumps back 4 spaces, or 1 indent level. I can keep doing that - jumping back 1 level - until I hit the left edge, or just
ctrl uto undo all the indentation on the line. If I want to indent a block of code, I do>ipto indent (>) in-paragraph (ip), and unindent works the same way. If I want to indent the line I'm on and the next 3, I do>3j. If I want to indent from this line to the end of this block, I hit>}. If a line was off by a character during any of these operations (which quite literally never happens - I'd have to selectively delete or add spaces to achieve this), it syncs back up with the rest, because indents - with my settings - always snap to multiples of the current shift width.Obviously those of us who like spaces aren't suffering constantly. We have this stuff worked out quite well.
2
u/Raptor007 C++03 Jan 27 '15 edited Jan 27 '15
Because of the literally hundreds, if not thousands of files I've gotten from archives, the internet, online acquaintances, and coworkers over the last 24 years from people who cannot if their life depended on it not mix tabs and spaces.
I can totally see why that would have you jaded; conversely, I keep coming across files where the other coder inconsistently used 2, 3, or 4 spaces to indent.
It happened because he used the mouse to drag code around all over the place all day long.
Oh god, really? I remember when this "feature" first started hitting operating systems; I had no idea anyone ever used it intentionally. Cut-paste is so much more precise.
It's almost impossible for me to be off by one space.
That's because it's your code and you're doing it consistently.
When I'm done typing lines at the current indent level, I hit backspace (obviously on the next, blank line), and it jumps back 4 spaces, or 1 indent level.
And this is a real pain in the ass when it's somebody else's code and they were using 3 spaces to indent.
I think we can both agree, consistency is key. Like you, my frustration comes from other people's inconsistent spacing. And both of our experiences point out the glaring flaw in Python's use of whitespace for scoping.
2
u/gfixler Jan 27 '15
That's because it's your code and you're doing it consistently.
I am by nature a very consistent person. I even once accidentally rewrote a function I'd written months before - about 20 lines - and when I finally found the other one and compared, they were exactly the same, to the letter, whitespace and all. That's par for the course. I follow every guideline rigidly, and can count on things like searches for words in my code finding every occurrence, because they're all exactly the same, down the capitalization. I find maybe 2 typos per year.
A lot of that is me using tools and processes that keep me from hurting myself, though. Vim is a good example - it shows me trailing whitespace and misspellings, autoindents, let's me complete words (so I never mistype them), etc. I find tabs to be an example of something that constantly confuses, and makes it hard to reason about what's actually going on in a file. I didn't know there were tabs in that n-queens file until my regex attempts in Vim were failing. I don't like hidden things. At least the spaces don't lie about where things actually are. You can screw them up, but if you are screwing them up, it's 100% on you.
1
u/Raptor007 C++03 Jan 27 '15
I am by nature a very consistent person.
I am too; I just chose a different method to use consistently. I find inconsistent spacing atrocious, whether tabs are a part of it or not.
I find tabs to be an example of something that constantly confuses, and makes it hard to reason about what's actually going on in a file.
I didn't know there were tabs in that n-queens file until my regex attempts in Vim were failing.
You can screw them up, but if you are screwing them up, it's 100% on you.
But all of these points apply to my perspective as well. Inconsistent indentation hampers readability and is 100% on the writer of the code. I would have written that regex to look for tabs, and if it failed it would be because someone else's code was using spaces where I expected tabs.
I don't like hidden things. At least the spaces don't lie about where things actually are.
This seems to be the only difference that isn't just about familiarity with the method. For me, I don't really care where the letters line up relative to a different level of scope; one tab means one level deeper. This fact remains true regardless of what width the editor displays tabs.
2
u/gfixler Jan 28 '15
I guess what I'm confused by here is your argument about not liking inconsistent spaces, when I've seen inconsistency so constantly with tabs. I've known a number of developers who switch back and forth between tabs and spaces for indentation, until it looks right (like that n-queens file - I've seen a few hundred like that over the years). Then when I've opened their files in my editor, which treats tabs differently, all has been scattered to the winds, and I have to fiddle with the tab settings until it realigns, then
:retabin Vim to change the tabs to spaces without moving anything. Then I consider it right.Python requires indentation consistency, and I actually knew a guy who would add tabs and spaces based on error messages until the file ran, so visually the file was all over the place, the count of whitespace characters worked for the interpreter. I never figured out how he was keeping track of things, when Python doesn't have other ways of denoting which things are part of which blocks. I also had a guy who used a 2-space wide tab stop, and mine was still at the default 8, so he was adding 4 tabs to simulate 8 spaces, and when he would send me a file, it would open with what appeared to be 32-space wide indent levels.
I don't want to deal with all of this. I just want things to be what they are. What I'm looking at should be what's actually there. I cannot stand that "Oh, wait, is this...? Yeah, this is a tab, that's why" feeling. I haven't seen anywhere near the craziness with spaces as I have with tabs over the last 2 decades. I almost want to speak in absolutes - i.e. it's impossible for developers to deal with tabs, or no one ever gets tabs right. It's been bad enough that that doesn't feel too far off.
Maybe it's just my particular field.
→ More replies (0)
8
u/ukalnins Jan 26 '15
That is one of the reasons why avoid python / lua for anything complex, I just quite quickly get sick with debugging spelling and type errors.
1
u/gfixler Jan 27 '15
Type errors, yes, but spelling errors never happen, because of how lazy I am. Once I've typed any longish word in a Python file in Vim, I use ctrl+n (or ctrl+p) to complete every other instance from then on. So if I need to type
ImportError, I typeIm<c-n>, and it fills it in. I've gotten super fast with this. I use the outsides of my hands for the control keys, so I barely move to hit ctrl-modified characters.If I need the same line again, I type the first few characters, then ctrl+x ctrl+l (one of the most emacs-like combos in all of Vim), and it fills in the rest of the line for me, or gives me a popup of matches, and I ctrl+n/ctrl+p through those. I've probably only type about 1/2 to 2/3rds of the characters in any given file.
6
u/ZMeson Embedded Developer Jan 27 '15
So maybe the C++ programmer who says she can’t write Python is really saying “Writing safer code in a dynamic language is a skill that takes time to learn! I have not yet learned it! I would be too scared to commit to writing a reliable Python program right now!”
Or maybe what the C++ programmer means is "I don't like wasting time writing extra tests and using extra tooling to make sure my code is safe when compilers can alleiviate much of the work."
Of course anyone who says "I could never write <language>" is really saying they are inflexible, aren't willing to expand their mind, and are blind to the fact that some tools are better suited for some jobs. (I write short Python scripts all the time. But there are situations where a compiled language is better too. And I must admitt that C++ is da bomb for certain situations.)
8
u/pashazz Jan 26 '15
Because it's error prone since it doesn't have compiler.
Though, python is the only language I could use among scripted ones. Very easy and clean syntax.
"I could never write PHP/Perl" is better :p
5
u/zvrba Jan 26 '15
Because it's error prone since it doesn't have compiler.
Just today during refactoring I changed a class member from public to private and used the compiler to find all occurrences of method use for me.
3
u/vlovich Jan 26 '15
Python 3 is getting optional type-checking: http://mypy-lang.org/
Still only a draft PEP, but Guido is in-favour of this approach.
1
u/kkrev Jan 26 '15
Well from that list perl doesn't have this particular undeclared variable problem and will give you an error. "use strict".
0
Jan 26 '15
[deleted]
4
u/kkrev Jan 26 '15
Not the same. The perl will give an actual compiler error. As far as I understand it Python only errors when the routine is actually run.
3
5
u/Bolitho Jan 26 '15
Almost all comments here are so... childish!
Every language uses white space - in python it is just checked by the compiler and therefore a built-in convention. So please stop complaining or joking about whitespace!
The reasoning about writing more tests that cover missing type checking is just nonsense! (As much as the claim by the article, that python programmers write better unit tests!) In a unit test you always test the behavior of a unit of work - this is true for a statically typed language and also for dynamically typed languages! So the question is, whether one knows what a good unit test is and how to write code that is testable or not. Once you got the concept, you can easily adopt that to a new language!
But according to the article and the comments here it seems to me, that lots of people here are just not polyglot... that is so sad!
0
Jan 27 '15
[deleted]
5
u/Bolitho Jan 27 '15
In other languages you also use whitespace to make blocks visible and therefore readable! The braces are only used by the compiler - not by the human brain!
The only difference is, that python enforces a specific formatting by the programmer, where other languages do not directly. But for every language there are style guides that demand a specific formatting. The only difference is therefore the layer in the tool chain, that checks for the correctness.
6
Jan 28 '15
The problem with whitespace formating is that it produces incorrect code whenever you copy&paste or move code between block levels until you fix it. And when you accidentally screw up the indention, Python won't even give you an error as it's all dynamically typed and automatically creates variables for you. So you end up with code that just silently does the wrong thing. That's a type of problem that you simply don't have in C++.
1
u/Bolitho Jan 28 '15
That's a type of problem that you simply don't have in C++
I agree.
But I can comfort you: Those errors hardly occur in real life! On the one hand you should not have too deeply nested blocks, so pasting won't confuse you. On the other hand you should have tests, that protect your refactoring.
0
Jan 27 '15
[deleted]
2
u/Bolitho Jan 27 '15
Do you really see the block here?
if (...) {do_something(); do_anotherthing(); do_something_again();}I doubt that - and I believe there is no style guide around that would strive for such a formatting...
1
u/Lucretiel RAII Junkie Jan 30 '15
What? Why? It's the same philosophy as static typing- make the correct way to to do something the only way to do something. Why would you ever not want to indent properly?
1
u/smspillaz Jan 28 '15 edited Jan 28 '15
I recently started using python for some projects where I needed to get something out the door quickly and performance didn't really matter much to me.
There's a lot of things I like about the language, and the ''with'' statement with context managers gives me sweet RAII-like semantics.
Not having a static type checker does suck, especially for all of the NameErrors that might come as a result of typoes. That said, I've found that you can mostly get around that with the various static analysis tools set to their most paranoid level. Prospector is a really nice tool run them all at once. Other than that, a more paranoid level of unit testing helps a lot too.
0
Jan 26 '15
Any coder who says he or she would never use a particular language is just someone who hasn't been unemployed lately.
-8
u/snarkhunter Jan 26 '15
Lol. Yes, because STL type errors are really helpful to the debugging process. Don't know what I'd do without them.
11
u/o11c int main = 12828721; Jan 26 '15
In all seriousness, STL type errors are really helpful to the debugging process. Don't know what I'd do without them.
Could they be better? Sure. I'm in the process of filing a bug with a list of error cases that should give hints.
0
Jan 26 '15
[deleted]
8
u/gfixler Jan 27 '15
This might make you feel good: I used to disagree with you, and hated types, and declaring anything. Now I've finally had some big projects under my belt, and felt the sting of Python's loosey-goosey type system, and I've started banging around in Haskell, and I've fallen completely in love with types. After a year of playing, Python has become a chore at work, because I can't really trust anything. I realized one day that a large portion of my very carefully built tests were just making sure things handled types well, and even chunks of code asked things like if something was a string, do this, and if a list was passed in, handle that, because that's bad. It's just all over the place. Now that I have experience with types, that all seems like complete craziness. Of course, C/C++ types don't really excite me still - the Haskell stuff is where it's at for me, but at least - "yay, types!" - right?
I've noticed in myself (waning, thankfully), and in many programmers I've known a wish to get things done really fast, and either feel good, or impress, or get to market first (which is a thing, I get it), so the speed of dynamic languages for prototyping and banging things out super fast is nice. The tradeoff I feel now is one of immediate gratification vs. long-term investment. Investment pays off - we know this in so many other areas of life. You hold back on the rewards now, and you get much more substantial rewards later. I've seen this in many programming and computing things that took me a while to grok - Linux, git, Vim, Haskell/FP, etc. They took me awhile to push through (and Haskell in particular is ongoing, and will be for a long time), and things were a bit boring and without payoff up front, but now things are so much better, and they're better from now on. Now it's constant payoff.
Now that I'm in a strongly typed language a lot - Haskell - I'm constantly hitting compile errors, but they're up-front errors. Once I make it through that barrage, once I've dealt with that early fight, I'm way safer for the rest of time. Vim was a pain in the beginning, but I'm ridiculously powerful now. Same for Linux. Git has changed how I code - not just how I version, but how I code - and for the better. Not every up-front investment is a winner, but there's something suspect to me now when things are really easy up front. I wonder if I'm trading anything away. Haskell's compile-time fight, and TDD - being about up-front testing, pre code-writing - have also shown me that seeing errors immediately when you make them is in and of itself a huge benefit. It's good training. I don't make a lot of errors now, because I can feel the compile error hovering nearby, or the failing test about to rear its ugly head. The up-front battles are like electroshock conditioning.
This is what a good type system seems like to me - it feels like a lot more work to think about types up front than to just say x = 3, okay, now x = "foo", but the rewards later seem pretty great, and have turned me around on the subject. This is why prototypes - IMO - work great in dynamic languages. Prototypes tend not to stick around long enough for the long-term investment to pay off, or for the short-term efforts to cause issues.
Anyway, my two cents... at least for now.
21
u/donvito Jan 26 '15
I like Python for when shell scripts would get too complex. But my tolerance ends when the project size gets over one file.