Hacker News new | ask | show | jobs
by BiteCode_dev 2271 days ago
This is getting ridiculous.

We went from:

> many Python projects have way more lines of code than equivalent C++ projects

To:

> It's a bit longer in C++ but frankly not by that much

With the proof being literally __twice__ more characters, with one line being more than a 100 characters long.

I understand that C++ doesn't have a native JSON lib, and sorting is a bit out there, but giving 3rd party lib access to Python feels like cheating:

    import pandas as pd
    agenda = pd.read_json("agenda.json", convert_dates=["hired"])
    for _, (name, age, hired, emails) in agenda.sort_values("name").iterrows():
        print(f"{name} ({age}) - {hired:%d/%m/%y}:")
        for email in emails:
            print(f" - {email}")
I mean, I can also create a lib that does the entire script, install it, and just do 'import answernh; answerhn.print_json("agenda.json")'

Now again, this is not so say "Python is better than C++". That's not the point.

If you want to write a game engine, C++ makes sense, verbosity doesn't matter, and you don't want the Python GC to kick in. If you want to iterate on your Saas product API quickly, it's probably not the best choice.

Why pretend otherwise? What the point of stating the sky is red?

2 comments

To be fair to the C++ version, you need to give as many guarantees from your code as C++ does from compiling. To me, in this case this is mainly the output formatting. Python won't tell you until runtime that you have a malformatted string, so add in some unit tests to make sure your string formatting won't crash, and you'll be at a similar number of lines of code as the C++ or Rust version.
> Python won't tell you until runtime that you have a malformatted string

Neither will C++, as far as I know, or do you mean that the compiler will validate the formatting of the string literal (python has linters that will validate this too!).

Hell, even with statically verifiable types, the python is shorter.

> Neither will C++, as far as I know, or do you mean that the compiler will validate the formatting of the string literal.

With iostreams there are no format strings so there is nothing to validate. Libraries such as https://github.com/fmtlib/fmt are allow to do compile-time parsing of the string though.

I think it shows quite well that the expressiveness difference is not as a large as many would think.

Also, the difference in tone towards jcelerier compared to steveklabnik seems not warranted here. Both IMHO contributed an educational example. Your response here equally does (or does not) apply to the Rust version, doesn't it?

Rust is fantastically expressive for a low level language.

And modern C++ is not 15 years old C++. You have auto, lambda, etc. I just think it's silly to try to pretend to be more expressive than scripting languages.

It's like Schwarzenegger trying to pretend it's as flexible as gymnast. What for? The guy is amazing at what he does.

> I just think it's silly to try to pretend to be more expressive than scripting languages.

So far we have a Python, a Rust and a C++ program in this thread which output the same thing given the same input, which, by the definition of expressive power of programming languages means that they are exactly as expressive as each other (given this information only and notwithstanding small differences such as error handling in this case).

Also you mentioned in your original thread :

> Even if the syntaxes were exactly the same (which they are not, python syntax is on average shorter)

indeed

> the low level nature of C++ requires your code to do operations that Python does need to do, such as memory management.

Notice that there is nothing which looks like memory management in my code. There are entire classes of programs that can be written without thinking about when to allocate or free your memory at all - just declare your variables on the stack and with std types, and that is handled for you (and that's not a modern C++ thing ! it's been like that since the very beginning). Opting in to memory management is an active, conscious effort in these cases (or a residue of people who only were taught java / c# at school and sprinkle their code with `new`).