Good Coding Habits

Qasim Ali
Pens
Published in
5 min readSep 20, 2020

Sadaf — this article is dedicated to you. Sadaf is my only student I am teaching to code these days. I am learning a lot, while teaching her. How do we keep it good and manageable even when it has now turned into a thousands of lines of code? Let’s discover today!

Planning by Kelly Sikkema on Unsplash

I have been writing nodejs and front end for over 3 years. At some point — it all stops working. Code gets stupidly lengthy and multi branched. You want to fix a simple route to filter the data. You try to find the appropriate route where you can plug the new feature inside — and you fail miserably. Because you did not give due thinking into writing it the first time, now it is not modular and new feature is only going to make it difficult to manage.

All my projects have expired due to my bad coding habits. Only because I complicated real simple things.

For example

  • Forms never followed their natural submit. I always interrupted them using prevent defaults () in javascript.
  • Excessive ajax to make meaningless interactions.
  • Doing multiple things on a single html page.
  • Building front end before the backend.
  • Coding preceeded with nearly no thinking.
  • Mongoose using multiple functions under 1 statement to make myself look cool [ to no one ] : — e.g findOneAndUpdate with { upsert: true } option embedded.

If you are failing in building a proper functioning product — read this article carefully.

1 — Draw your plan

If you have an idea, or a multi billion dollar proposal, or your life depends on it. I would suggest WAIT. Absorb this idea. Let it settle in your head 2 to 3 days.

All websites are complicated and thorough planning makes these complicated things into simple things. For example, every time I built a product it needed a Sign Up functionality.

Sign Up page is the entry point where you get your earnings. It is meant to work properly for your product to churn properly.

Sign Up, is not alone. It needs Sign in Form beside it. And Sign in is not alone. It needs a forgot Password Form sitting there in the corner of your project. And forgot Password, needs a method to send you 4 digits verification code to your email inbox. This is not it. You got to make a separate Premium portion — where people signing up should be able to pay you.

See — it is not simple, if you get to hitting keyboards out of the box.

I strongly suggest, if you want to deliver a quality product, make a sober sign up system. That gets money into your account in the fewest possible clicks.

Draw your plan on a white blank paper. Write down the exact hierarchy of actions needed to build the system. Use good hand writing that is referable a month later.

Very few people have made a good sign up / in system — in the history of web development. facebook did it right and it blew up. Apple did it right, on there Macs. Google did it right on OAUTH [ Sign in with Google ] and finally manage to earn. Good platforms make you sign up right up, as you reach the landing page, in the fewest clicks and reward you with a system built on scalable architecture.

If you get sign in / up / payment methods right, you are far ahead of every other clever programmer out there.

2 — Simple is the best approach

I have seen many people, almost all the developers, using the shorter route to building an MVP.

Common mistakes they commit as they get excited about a new idea: —

  • They will skip proven CRUD methods to fetch data from database.
  • They will first get into writing the front end.
  • Losing the momentum to keep the excitement alive. This causes death of all good ideas.

In the web development hype on twitter, almost everyone is skipping the basic guidelines today. Repeatedly compromising the quality and later developing a habit of writing bad code.

PEOPLE — coding is not it. It is not a skill, if it is not written thoughtfully. It turns into a crap, which you do not want to see happening with your product in the future.

3 — Do not jumble up the File System

You must have seen a pile of files belonging to developers’ project directory. Each method falls into a separate file. Model Schemas go else where. Configurations are pulled out in a separate file. And so much of base line code — that could have fallen into 1 x file, shifts to a number of files.

It is a wrong approach.

Multiple files cause excessive confusion. Specially, when you are an amateur or a novice developer.

Write everything in 1 file.

For example I am writing nodejs: —

  • My html files are “.hbs” files and they go at one place in “views” directory.
  • My backend file is one “index.js” file and it resides at only one place.

There is nothing that is out of my reach when I am handling backend. The images need a delete route? I search “image” and all of it is here right in front of me. Layered, beautifully placed with its neighbouring routes.

Repeatedly looking at same lines of codes, keeps things familiar. And slowly, the entire layout, wires itself to our natural way of thinking.

If a code breaks or a route malfunctions, I have only 1 file to fix. There are no back and forth jumps to find the bug. No getting familiar with the code first. And it is all now easy. Next cool addition, next middleware authentication or building a nice Cart, that works without bugs, becomes as clean as the rest of your back end.

Conclusion

Writing clean code, giving simple routes, doing one-thing-at-one-time approach and understanding before getting into building — are all things that is keeping writing code fun, for me.

If you do not love what you are building, you might find some customers but you won’t be able to maintain its quality.

Slowly, things will start breaking apart, and you will be wanting to give up. At that point — you will need to re-write all the bad lines of code. You might dump it and restart the entire project from zero.

I seriously wish, in my early days some one would have told me, it has to be simple and linear to Work. I would not have wasted past 3 years of building crazy stupid projects.

Be very conscious of your decisions while you are building it. Slowly you will build trust over your project and it will get easier to improve or refine. Embedding new features will become easier. And you will at once know; if your customers are asking too much or they indeed need it.

I would conclude today’s article with a base line advise, every developer needs to remember : —

Keep it Simple and Linear.

If you liked this post, you will like our meetup coming October 4th. It is “Making a Useful Website” and 3 amazing developers are going to teach us that. RSVP here.

--

--