Using Lookup Table Makes Your Code More Readable, Clean and Elegant.

Mohasin Hossain
2 min readNov 10, 2023

--

Let’s talk about lookup table. Lookup table definition

Basically, a lookup table (LUT) is an array that replaces runtime computation with a simpler array indexing operation.

If you have a big switch statement, or collection of if’s, check if you can refactor is to a more readable variant: the lookup table.

Let me give you some example so that you can really understand how lookup table works! Have a look in the image below. Lots of if else, right?

Now look the image below. More readable, clean and elegant, right? This is how look up table works and makes your code readable, clean and elegant.

Let’s see one more example with switch case statement.

Once again More readable, clean and elegant.

Last but not the least example for you.

Using lookup table is simple but the benefit of using it a lot.

Hope you enjoyed. Happy coding!

Bonus tip: Reading complex code takes extra brain power, so try to write clean code that save you and your team’s brain power.

Bonus: You must love my most read article : How To Use Single Responsibility Principle in PHP/Laravel
And most GitHub star repo:
https://github.com/mohasinhossain/SOLID-Principles-in-PHP

--

--

Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux