Learning programming is fun and easy, but writing a good program can prove to be a daunting task. In most cases, we find ourselves constantly scratching our heads trying to understand the spaghetti code we wrote, or making lots of changes just because of a newly added minor feature.
These outcomes can bring down our confidence but in fact, they can be solved with proper development practices. Here are 10 worthy habits to invest in to write cleaner and smarter code with less effort. Let’s start developing these habits now!
1. Write Human-Friendly Code
Always remember that you are not just writing code for machines, but for your future self too. So it is important to write readable code. In truth, programming is like writing a good poem. The tone should be consistent, the words descriptive and sentences well structured.
Here are some tips to write friendly code.
1. Follow Consistent Naming Conventions
For example, if you name private variables with underscore as the first letter, you should follow through in the rest of your code.
If you are working collaboratively, discuss naming conventions before anyone touches the code.
2. Descriptive Variable / Method Name
The variable and method name should describe what that code does perfectly. For example uncommon abbreviations like
Sort_PT() may be confusing: what does the PT mean? If you can’t answer it on a second check, optimize the name to
Sort_PostType()for better comprehension.
3. Indentation And Line Breaks
Indentation is magical! With some simple Tab keys, you can reveal the entire code structure and edit the code with a clear understanding of how it will function.
For line breaks, use it when 2 codes on the same line execute different things. For instance, it’s best to not chain CSS properties on a single line — it complicates things.
2. Think Organization
On top of cleaner code, code structure and organization also helps with readibility issues. It is important to group your code to enable easy modification (if you need to scroll up and down multiple times to find your affected section, you need to work on the structure)
Also, don’t throw all the code into a single script. While it may seem convenient to have 8000+ lines of code in a single file, debugging is a nightmare when trying to recall method names. Always think about the ease of change.
The best practice is to separate code into different files according to their primary functions, for example manager, interface, animation, extension, etc.
Whenever possible, make sure the language only fulfills a particular purpose. If you are writing animation effect with CSS3, avoid writing jQuery animation effect, unless you have a strong enough reason. And if you do, comment in the CSS file about the exception.
3. Planning Before Coding
It’s crucial to know exactly what to do before you hit the first key. In the programming world, it is important to plan ahead. Writing a fixed navigation menu is easy, but what if the menu needs to be responsive, while being able to minimize itself when visitors scroll down the page?
Begining to code without a clear procedure in mind will often lead to retries, burnouts and a depression loop.
Plan Your Code
So instead of problem solving and programming the parallel way, it’s far easier to figure out the procedure first, then write a solution. List out general steps first (here is an example):
- Use Bootstrap as responsive framework.
- Create navigation menu.
.navbar-fixed-topto the menu.
- Create jQuery script to resize menu when user scrolls down for over 200px.
- If the user scrolls to the top, resize the menu once again.
At this point, you should discover other underlying isses, like how we should resize the site logo and menu title according to menu size, or if we need to resize the dropdown menu as well, or if the detection needs to be dynamic instead of fixed. Once you figure these out, it’s a snap to get the menu done.
4. Write Manageable Code
Trust me, you don’t want to change hundreds of variables individually, line by line. I did, for 1 straight year. That was a humbling experience, and since then I have learned to write code that requires as few manual changes as possible.
How to achieve this? Use Array. It’s perfect for storing multiple variables, and you can access those variables with convenience
for loop or
for each method.
There are even more dynamic ways to add, arrange and retrieve data in certain languages, such as List and LINQ for C#, so be sure to periodically check out better features, libraries or plugins for smarter data management.
Lastly, to avoid constantly modifying stuff in code, write highly independent code that will not break the entire system when new features or changes are applied. It’s called Loose Coupling. After optimization, the developer only needs to tweak code in 1 class instead of 3 classes for new features.
5. Stop Overdoing Features
As our skills mature, we tend to develop more complex solutions that cater to a wider range of needs. It’s a good sign of growth but be wary as you might be stepping into another trap – overdoing a feature that’s entirely unnecessary to the project.
In development, it’s important to regularly remind yourself of the project’s main objective, and only add features that fulfill the purpose. If you know the exact collection size, use Array. If List’s function can retrieve the data the way you want, don’t use advanced LINQ.
Why develop a plugin when you hardly use that feature? Just because you can, doesn’t mean you should. Don’t waste your time. Rremember the most time consuming part aka finishing the project, is yet to come! Now, onto debugging!
6. Learn To Debug Smarter
Where there is code, there are bugs. It’s impossible to have a bug-free code solution, sodebugging skills are highly sought after. The ancient trial-and-error method may work, but it is slow. Too slow. Plus, why torture yourself when there are already debuggersdeveloped for you?
However, a debugger won’t know your code inside out. When in doubt, put the log function into the code, like
console.log for Firebug, and make sure it’s good with variable integration (instead of retyping variable names as string or simply
'It works!'). The web is filled with more advanced and specific debugging methods for every language.
7. Find A Stronger Editor
The right editors can help you grow in knowledge, and expedite project completion.Regardless of how experienced you are, it’s strongly recommended for you to go for editors with code completion, such as Sublime Text and Aptana Studio 3.
Not only is the feature very helpful for beginners to recognize and learn the syntax, but it can be utilized by professionals to check for possibly better codes or solutions. Do take note, most IDEs only support code completion for certain languages, so look for the right one.
For instance, one thing I love about MonoDevelop is its code template feature. With a custom shortcut key, you can output a self-defined code template from common
switchstatement to full-fledged manager script. This is great for large scale projects, so try to make this feature a priority during your editor hunt.
Other essential features to look out for:
- regex replace
- version control support
- database support
- split editing
- layout customizer
- WYSIWYG editor
8. Do Version Control
There will be times when you make a huge programming mistake and want to go back to an earlier version of a code. Now what if your mistake involves several files in the codebase, and some of them were modified days, or even months ago?
Revert to the original and update the code bit by bit while solving bugs? This is counterproductive and why a version control system is greatly essential.
Version Control Systems
Among several version control software, Git is the most popular with a large number of documentations available online. With Git, you can keep as many revisions as you want, branch out the file for some code experiment, track down the part of code you changed last time, and revert back to them whenever you want.
Feels like overkill? Maybe, since it’s originally developed for team programming, but it’s also perfect for long term personal projects. You may be hesitant because the Git is in itself so much to learn up on, even with the GUI version. But it’s more of a sooner or later thing (so better that you make it sooner), plus the web is loaded with plenty of tutorials and guides to help ease the learning process.
9. No Extra Prototypes, Finish Current Project
Tedious coding and debugging can drain someone physically and emotionally. And some of us are inclined to prototype our work even before it is finished. While prototyping is a beneficial behavior in the long run, it doesn’t help when you do it to escape from work.
A better way to lose some steam is to enjoy something totally unrelated to the work, exercising, gaming, reading (perhaps?) — You have to keep your work life in balance. Anything but prototypes.
10. Always Learn Something New
In this field, you can get phased out fairly quickly. Many developmental approaches and programming languages even are declared obsolete within the past 10 years alone. Even if you graduate from a top university with a degree in the subject does not ensure that you are still primed for employment.
The only way to survive and thrive is to keep learning.The best way to learn? Don’t just read, program. Challenge yourself with practical projects that require higher scalability. This forces you to think more efficiently. Explore, and enjoy the creation process. Practice is not something you can ignore in programming. The more you program, the better a programmer you become.Reference :
This post has been seen 533 times.