5 Easy Tips for Writing Cleaner Code [Technique Tuesdays]
Some of the highest ROI techniques that
If you like my writing, I would really appreciate an anonymous testimonial. You can drop it here.
To learn more about the newsletter, check our detailed About Page + FAQs
To help me understand you better, please fill out this anonymous, 2-min survey. If you liked this post, make sure you hit the heart icon in this email.
Recommend this publication to Substack over here
Take the next step by subscribing here
Clean Code,
It’s one of those terms that is thrown around a lot. Obviously, you want to write code that is pristine, but what does that actually mean? This can be a hard question to answer because the people interacting with your code are often not the end users. This means that code quality is not always correlated with the performance of the code, which makes the concept of code cleanness very abstract. You can’t really write tests to check for code cleanness the same way you can for functionality.
With that being said, writing clean code is exceptionally important. In Microsoft’s research into what makes great software engineers, the ability to write high-quality code ranked very highly. If you’re angling for promotions (especially in big tech companies), the feedback your supervisors give in their code reviews will be key. Thus writing amazing clean code is a must.
In this article/post, I will be giving you 5 easy tricks to immediately start writing clean code. You can implement these techniques right away to start writing code that is both functional and cleaner than Luka Modric’s dribbling.
Important Highlights
What is Clean Code- The way that I see it, clean code is code written for other developers. Clean code is code written in a way that other developers (even those with minimal context) have an easy time reading through the code base and extracting the key ideas from it.
Technique 1, 𝗣𝘂𝗿𝗲 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀- A pure function is a function that always returns the same output, given the same input(s). It doesn't depend on any external variable apart from the inputs provided, nor it affects/changes any outside variable. Having pure functions makes it a lot easier to test it.
Technique 𝟮. 𝗡𝗮𝗺𝗲𝗱 𝗣𝗮𝗿𝗮𝗺𝗲𝘁𝗲𝗿𝘀 - When using functions, we often use positional parameters which have to be provided as they're declared with the function declaration. It is hard to see from the function-calling statement which parameter corresponds to which argument of the function.
window.addNewControl("Title", 20, 50, 100, 50, true) #instead of this window.addNewControl(title="Title", xPosition=20, yPosition=50, width=100, height=50, drawingNow=True) #do this
This can save your fellow developers a lot of mental energy. Not every language has this feature. One way to get around it is to use Objects for params, where the objects contain the params. Another easy way to get around this is the next technique.
Technique 3. Meaningful 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲 𝗻𝗮𝗺𝗲𝘀- If we create variables like "p", "t", and "c" without having a context, it is really hard to read, trace and maintain such code. I talked about this in the post on writing better comments for your code. You can replace a lot of unneeded comments just by using meaningful variable names.
Technique 4. 𝗔𝘃𝗼𝗶𝗱 𝗛𝗮𝗿𝗱-𝗰𝗼𝗱𝗲𝗱 𝘃𝗮𝗹𝘂𝗲𝘀- It is really hard for someone reading the code to understand what the hard-coded value means. Especially when it's used at different places in the code. Large codebases have a lot of moving parts and keeping track of hard-coded values can get taxing. Use properly named Enums, const, or object mappings to represent constant values.
Technique 5. Use Default Values- A seriously overlooked technique is the utilization of default values in your code. You will see many lines of code dedicated to validating inputs. This increases code complexity, drops readability, and leads to more dev time spent in understanding the code as opposed to problem-solving. Enforcing default values is a great way to get around it.
function(val1, val2 ..){ #notice all the lines spent in validation check for val 1 check for val 2... } function(val1=default1,val2= default2 ..){ get directly into using the values }
Using these techniques will allow you to easily make your code much cleaner in an effortless manner. Do you have any techniques that you really like to use? Let me know in the comments/by reaching out to me.
I created Technology Made Simple using new techniques discovered through tutoring multiple people in top tech firms. The newsletter is designed to help you succeed, saving you from hours wasted on mediocre resources or on the Leetcode grind. Easily find your needs met in one place. I have a 100% satisfaction policy, so you can try it out at no risk to you. Use the button below to get 20% off for up to a whole year. Using this discount will drop the prices-
800 INR (10 USD) → 533 INR (8 USD) per Month
8000 INR (100 USD) → 6400INR (80 USD) per year
In the comments below, share what topic you want to focus on. I’d be interested in learning and will cover them. To learn more about the newsletter, check our detailed About Page + FAQs
If you liked this post, make sure you fill out this survey. It’s anonymous and will take 2 minutes of your time. It will help me understand you better, allowing for better content.
https://forms.gle/XfTXSjnC8W2wR9qT9
If you like my writing, I would really appreciate an anonymous testimonial. You can drop it here.
Stay Woke,
Go kill all,
Devansh <3
Reach out to me on:
Instagram: https://www.instagram.com/iseethings404/
Message me on Twitter: https://twitter.com/Machine01776819
My LinkedIn: https://www.linkedin.com/in/devansh-devansh-516004168/
My content:
Read my articles: https://rb.gy/zn1aiu
My YouTube: https://rb.gy/88iwdd