This post is all from my experience and what I have learnt during my short professional career. Of course this is a very big topic and unending topic to write on but as a coder/developer/programmer it is very important to write the code in an effective way such that it is easy to understand and make subsequent changes easily.I will give you few tips which will certainly help you in writing code in a better way.
So here it goes……..
Write a very basic code
This is the very first step considering the problem you are solving.Let’s consider I am checking if the number is prime or not. For me the very basic thing will be to make the code run.This is the very first step that we should do.
All running codes are not performant.
When we talk of a performant code we mean that code should doing our work faster in terms of all means i.e memory,time etc….
Here comes the next step.Then we try to find out and analyze how in less steps we can do the work.
This can be done by reviewing your own code. Reviewing the code on the same day/time on which it is written by yourself only might not give desired results. You can review your code might be the next day or any day after or can ask your peers to do the review.
Never be afraid of a code review as it makes you only better.
Reading a code basically gives you more idea of how things are implemented. Each and every programmer has his own style of coding. Reading ensures that you write code in a more cleaner way.
Try reading code as much as you can.
This code reading might be done by doing a code review of peers or of colleagues/seniors who have written the code.
I basically work in .NET domain. Once I had a small Console application about 20 lines long.My senior/mentor/guru took a glance for a few seconds and said you are writing few lines extra and you can reduce 4 5 lines from the code. Such is the power of reading code.
Know What You Are Writing
As a programmer our whole life depends on learning. Each and every day we try to learn new things. We become happy when our code works somehow. Stuck in something trying trying trying and foo!!! Then Hurrah!!!It works!! And then we never look at it again.Thus we miss out on the most important thing i.e learning!!!
The main challenge here is to know why the previous code was not working. If approach and understanding is wrong then it’s okay!! But in a real scenario if something was working and suddenly it stops then it’s important to know why it stopped and what might have caused the issue.
Then when we solve the things we tend to put things from internet. Copying things is great but it is really necessary to know how things are working now and what the copied code is doing.
Know what your code is doing.
Code vs Architecture go hand in hand
Becoming a better programmer will also depend on how your project architecture is. Poorly constructed project architecture can cause havoc . You never know a change in one place can break what!! I have seen big project running on a simple 3 tier architecture with a lot of code repetition.
In most of the cases we should try to make the code easy to manage. Easy to manage means when an update occurs to our project then that change should be at the minimal places possible. Duplicate codes will make your life hell if you have huge changes to make.
Avoid duplicate code.
Well these were a few tips that I wanted to share for writing a good code.This is a two part post. In next part I will give you examples in the form of actual code that will make you better.