What Are the Advantages and Disadvantages of a Compiler?
Tags: programming, computerscience, codequality
Published: 2022-01-24 13:13:25

If you're a software engineer, I can imagine that you've heard the term "compiler" a lot whenever you're in the process of coding something. Especially when you're getting an error. 😁

The compiler can be a friend or an enemy, depending on what you're doing.

This article will show you the advantages and disadvantages of a compiler. 

The book "[Five Lines of Code](https://www.manning.com/books/five-lines-of-code?utm_source=maddy&utm_medium=affiliate&utm_campaign=book_clausen_five_5_8_20&a_aid=maddy&a_bid=4b64bea7)" explains it brilliantly, so I'm going to refer to that in this article. The code snippets are in Typescript.

> 
DISCLOSURE: This article contains affiliate links to external products. This means that I will earn a commission at no additional cost to you if you decide to make a purchase. Thank you! 

Now let's dive in.

#### What is a compiler? ####

A compiler is a program that turns a high-level language into low-level code that the processor can digest.

This is the primary goal of a compiler.

But nowadays, compilers do more than that. 

They check if some errors can happen during runtime, and in this article, we're going to concentrate on this aspect.

#### What are the advantages of a compiler? ####


*1. Advantage: Reachability ensures that methods return*

The compiler ensures that a method has a return statement. The compiler will let us know when a method should return something, and if we miss the return statement.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642683107299/jYMp0YZC5.png)

*2. Advantage: Definite assignment prevents accessing uninitialized variables*

Compilers are helpful with checking whether a variable has something assigned to it before we use them.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642683139942/ZBCh0JhX1.png)

The compiler won't let the programmer get away with not declaring a variable.

*3. Advantage: Access control helps encapsulate data*

The compiler does an excellent job of managing access control. 

If we make a member private, we know that it will not slip away easily.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642685112779/V7is2vTux.png)

*4. Advantage: Type checking proves properties*

The compiler helps us check whether members and variables exist and are associated with compatible types.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642863548812/SA6eyXCwg.png)

We've seen the advantages of the compiler.

Now let's examine the disadvantages of a compiler.

#### What are the disadvantages of a compiler? ####

*1. Disadvantage: The halting problem limits compile-time knowledge*

What is the "halting problem"? 

[Wikipedia](https://en.wikipedia.org/wiki/Halting_problem) defines the halting problem like this:

> "In computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program and an input, whether the program will finish running, or continue to run forever."

In simpler words, the definition says that we don't know what's going to happen until we run a program.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642938438030/8W6AWCkQ2.png)

In the screenshot above, what's inside the IF statement won't run, and regardless the program will fail because there is no definition for the method foo().

This is a weakness because the compiler will allow a program to behave unexpectedly in some cases. In other cases, the compiler will perform *conservative analyses*, which means it will refuse to run the program if it is unsafe. 

*6. Disadvantage: Dereferencing null crashes our application*

Have you ever got a `NullPointerException` unexpectedly?

Unfortunately, the compiler doesn't help with this. 

The compiler will allow a program to run but then throw a NullPointerException at runtime.

In some cases, the compiler will tell you something such as "XYZ may not have been initialized". But not all the time the compiler will help you prevent a NullPointerException.

An excellent way to prevent a NullPointerException is to ensure that all objects have been initialized before we use them.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642938495529/ZIPOV31Fz.png)

*7. Disadvantage: Arithmetic errors cause overflows or crashes* 

Remember the `StackOverflowException`? 

The compiler doesn't help with this as well. It doesn't even tell us whether a number is divisible by zero (in that case, we'll have to trust our maths skills). 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642938343442/LFVx7O8Jd.png)

*8. Disadvantage: Out-of-bounds errors crash our application*

Have you ever seen an `ArrayOutOfBoundsException`? I certainly have.

It happens when you try to access an element outside the data structure.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642938571222/xuRKe5P_M.png)

In this case, the compiler will not help us.

A great way to overcome this exception is to go through the data structure using a loop. Alternatively, we can check that an element exists before we access it.

 *9. Disadvantage: Infinite loops stall our application*

The compiler doesn't help us when we stare at the screen while there is a never-ending loop running in the background.

I encountered this problem in the past when using a while loop.

Indeed, it's better to use a for loop instead of a while loop to prevent this problem.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642938615735/aG4XWF-L3.png)


*10. Disadvantage: Deadlocks and race conditions cause
unintended behavior* 

The compiler doesn't help us with multithreading. And multithreading is an advanced topic.

Many problems (such as race conditions, deadlocks, and starvation) can occur with multithreading.

When does a race condition occur?

"A race condition occurs when a software program depends on the timing of one or more processes to function correctly."[[TechTerms.com](https://techterms.com/definition/race_condition)]


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642867225164/T6bH9csuI.png)

When does a deadlock occur?

"Both threads are locked, waiting for each other to unlock before continuing."


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642867364335/hPaJS8FUD.png)

A deadlock can be solved using a *lock*, which checks whether a thread is free before moving on.

When does starvation occur?

The author of the book uses an excellent metaphor:

"The metaphor for it is a one-lane bridge where one side has to wait, but the stream of cars from the other side never stops".

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642867470220/pNphyYlp0.png)

**A bit of curiosity: what's the difference between a compile-time and a runtime error?**

Compile-time errors happen because of mistakes in the syntax or semantics. The programmer can quickly fix the error during compile-time.

Runtime errors happen during program execution. Fixing runtime errors is challenging because the compiler doesn't highlight runtime errors.

**CONCLUSION**

In this article, we went through the advantages and disadvantages of the compiler, and the difference between compile-time error and runtime error. 

If you enjoy my content, consider subscribing to my [newsletter](https://techwithmaddy.com/).

Thank you for reading this article.

Until next time! 👋🏾

***ADDITIONAL RESOURCES:***

- [Christian Clausen: Five Lines of Code - "How and When to Refactor"](https://www.manning.com/books/five-lines-of-code?utm_source=maddy&utm_medium=affiliate&utm_campaign=book_clausen_five_5_8_20&a_aid=maddy&a_bid=4b64bea7)

- [Baeldung: Runtime VS Compile-time](https://www.baeldung.com/cs/runtime-vs-compile-time)