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!
Learning Programming: 10 Misconceptions That Are Not True
There are plenty of misconceptions and myths surrounding the art of programming. Many view it as a job… Read more
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.
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.
However most programming language may now already have their own standard naming conventions either it’s defined by the core language itself or the community (e.g. PHP-FIG). So, most of the time, you don’t have to create your own naming conventions.
You can instead use a so-called linter or code sniffer to help enforcing and fixing your code to conform with these standards. Here is a few of popular linters in some programming languages:
- PHP: PHPCS
- Ruby: RuboCop
- Shell or Bash: ShellCheck
- JavaScript: ESLint, and JSHint
- Swift: Tailor, and SwiftLint
Descriptive Variable / Method Name
The variable and method name should perfectly describe what that code does. 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.
There’s an exception though. Most of programmers would already be familiar with the term “ID” for identity so we can always define a variable with the short form, “ID”.
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.
- Add
.navbar-fixed-top
to 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.
Troubleshoot
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, so debugging 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 debuggers developed for you?
Debugging Tools
Browsers are now equipped with a very capable of Developer Tools or DevTools for short that comes with error detection, breakpoint setting, expression tracking, performance checking, all for your debugging convenience.
10 Websites to Test Your Codes Online
Modern trends and webapps have dramatically changed the way web developers can build. Obviously you need some type… Read more
IDEs like Aptana Studio 3 and Xamarin Studio are even released with their own powerful built-in debugger, so investing the time to learn them can make your life much easier.
However, a debugger won’t know your code inside out. When in doubt, put the log function into the code, like console.log
for , 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.
30 Cheatsheets & Infographics For Software Developers
If you are just embarking on a journey to learn a programming language or would just like to… Read more
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, Visual Studio Code, 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.
Feature Filtering
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 switch
statement 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:
- debugger
- regex replace
- macro
- 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 softwares, 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.
30 Common Reactions Programmers Have When Things Go Wrong
Developing applications can be a very stressful job. Nobody is perfect, and running into buggy code is fairly… Read more
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.
Getting A College Degree Or Self-Learning?
Steve Jobs, Mark Zuckerberg and Bill Gates are some of the biggest names in the tech industry and… Read more
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.
The post 10 Programming Habits Developers Should Adopt appeared first on Hongkiat.