Interview Basics| Clean coding
This blog talks about a “practical” way to keep your code clean during interviews. Most people make the common mistake of not taking it into consideration during practice.
The day you admire a piece of code just for the sheer beauty of it, irrespective of it’s functionality, you shall start understanding what clean code is.
Things to keep in mind
- In most cases, interviewers are experienced enough to understand that writing a production-level clean code requires one to put more thought than just 5–6 minutes.
- The interviewer is only human, don’t expect them to understand your approach in a second.
- During an interview, the aspects that are mainly in focus are naming and readability. Testing and scaling are secondary aspects here.
- Talk - The biggest mistake possible during an interview is writing 50 lines of code while being quiet.
Common Mistakes
- Single-letter variables are bad in most cases. (x and y as coordinates are just fine). Ex: Using i and j as the iterator variables.
- Long ass names Just because you want to keep things clear doesn’t mean your variables end up being 1 sentence long because that makes your code illegible.
- Using too many expressions in an if-else statement. There is nothing wrong with the statement below, but moving it to a function as shown in the next image offers more clarity about what the code is doing.
- Some companies choose not to allow IDEs during interviews (sed 😓) . This leads to random {} syntax and also typos while reusing variable. (These are usually overlooked during interviews, but should be taken care of when you recheck your code.)
- Not using comments when you write code.
Tricks
- Keeping talking. Explaining to the interviewer while you code helps you get away with a lot of shitty names ;).
- If you don’t have a decent variable name, just call it out right then and there. Inform the interviewer that you will come up with it later. This will let you keep up your speed.
- Adding comments to offer more clarity. It can also be used to move ahead on complicated pieces of code that the interviewer would be struggling to understand.
- Use the variables that the interviewer provides. Ex: If he says that the number of students in a bus is represented in a list buses. Use the variable name as buses. Don’t go around with studentsInBus.
- Lastly practice. It is the ultimate trick.
Example Code snippet
Just providing the code here. Complete questions and discussions are available in the comments of the link.
You can go through this list of Google interview questions. Their discussions contain pretty great examples of clean codes.
Please let me know in the comments if you don’t understand anything. Also, let me know if you guys face any other issues that I should be writing about.