If you are beginning to code or you have been already coding, you might have come across the terms compiler and transpilers. Before we move to the compiler or the transpiler, we need to know that our machines do not understand the programming languages, so to run a code it needs to be converted into binary code aka machine language by some tools.
And these tools are compilers and transpilers. Let's dig deep and understand what are these tools?
What is a Compiler?
A compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). Compiler basically converts the high-level programming languages like C++, Java, Python, etc. code into the lower-level language (e.g. machine code, binary code, or assembly language ) that are executable by our machines.
Now comes transpilers. So,
What is Transpiler?
Transpiler aka source-to-source compiler is a computer program similar to the compiler or we can say the special case of compilers wherein it transforms the code written in one language to another with the same level of abstraction. It means one high-level language to another high-level language. For e.g. C++ code to C code or TypeScript code to JavaScript code.
If a compiler can directly convert the code into executable code then what's the use of a transpiler.
Transpiler is used for various reasons:
Compatibility: Generate code conforming to an older version while developers benefit from features of a newer version, such as with browsers not updated to the latest JavaScript standards.
Performance: Initial code was written for quick prototyping or the project is moving to a different platform where another language is more suitable. Perhaps the target language has a better compiler that can generate more optimized code. For example, critical parts of a Python codebase can be transpiled to Fortran and then called from Python.
Tooling: Source language has better IDEs, testing and debugging tools, such as with .NET and Visual Studio.
Migration: Migrate legacy code to a modern language.
Coding Skills: Codebase can be transpiled to a language in which the skills are readily available. Or it might be a matter of preference. For example, those who are from OOP backgrounds prefer TypeScript. Python coders prefer CoffeeScript instead. In both cases, code is transpiled to JavaScript.
I hope, you now understand what are compilers and transpilers and what they are used for. Thank you so much for reading till the end.
Keep Learning. Keep Growing.