DEV Community

Brunner
Brunner

Posted on

why are there so many programming languages when they all do the same thing?

You want to code for android? You need to know java or kotlin
you want to code for ios? you need to know swift or objective c
you want to code for the web? you need to know php or js or ...

In order to code for multiple platforms, you need to learn so many languages when they all do similar things.

For example: 'print()' prints text to the console in java, swift and php. It does the same thing everywhere. But its interpreted by different compilers.

Don't you think it would be great if we could cross compile our code to different platforms? Thanks

Top comments (10)

Collapse
 
pentacular profile image
pentacular

While most languages can do most things, they do them differently, resulting in different costs -- it can be cheaper to do X in Y.

We can cross-compile our code to different platforms -- this is what cross-compilers do.

Collapse
 
brunner1000 profile image
Brunner

Thanks for you feedback @pentacular

Collapse
 
pentacular profile image
pentacular

You're welcome. :)

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha

Just because it looks like its doing the same thing, doesn't mean it is. Answer to this will have different perspectives. 'Printf()' does not print on console for these languages. printf() prints on a default standard output consumer. If you run your program from console, your standard output device is console. If you run it on network, that will be standard output consumer. If you run it from another program in another language, its output will be a pipe(or something similar) that will ingest the output egest it as input for that program.

Mostly it boils down to their specific approach to specific problems.

PHP began as template system for CGI. So it process a request and forward them and then kill itself. Nodejs approach this with Event-Loop enabling them to maintain a connection even after serving the request. ASP / JSP primarily targets run time security for business logic, that's why there is so much setup and configuration needed before creating any website using them, compared to PHP or Node.

Another example is the final executing code. Swift and Obj-C compiles down to Apples Hardware and OS and its own instruction set ( Even Intel Mac has modified instruction set specific to them. Intel for windows will not have those) Kotlin and Java compiles down to JVM bytecode. And for android, there is a different compiler and linker which will compile it to Dalvik/ART. Which will then execute them to different chipsets.

But even Kotlin and Java are very different programming language (even though they both target JVM ) Kotlin architecture for lambdas and coroutin is different from Java threaded model. Lambdas in Java work differently than lambdas kotlin.

You can cross compile provided you can take the extra work. Dropbox earlier used C++ for both ios and android, but cost of developing for multiple platform was much higher than cost of maintaing two codebase. So they gave up. Flutter uses C++ api for targeting both platforms reducing your work, but you need to learn Dart for it. Kotlin Native has different approach, they emulate generation of Swift bytecode when building for ios.

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha • Edited

There is a compiler explorer godbolt.org/ , Write Hello world or something similarly small on multiple language and see what kind of bytecode is generated. If you know little bit of assembly, you can go through it and see how different language and compiler works. C program on GCC and on LLVM has different output for same program, there you can see how each compiler has different approach to the problem of compiling to machine code.

There is a youtube channel ContextFree there he talks about how different programming language works, why they were created, what problem area they focus on, etc.

Collapse
 
brunner1000 profile image
Brunner

Thanks for your feedback. Your comment is very informative

Collapse
 
33nano profile image
Manyong'oments

Because humans are despicable creatures.

Collapse
 
brunner1000 profile image
Brunner

haha. Thanks for your feedback Kaizen

Collapse
 
skryking profile image
Jason Ormes

Different hammers for different tasks mostly.

Collapse
 
brunner1000 profile image
Brunner

thanks for your feedback. Your analogy is spot on