Pro tips for writing better code
Beginning coders often learn quickly that the ability to simply code in a particular language is only half the battle. The ability to write clear and reliable code is a different story -- one that requires knowledge of best practices, attention to detail, and experience with reviewing and editing code to optimize it.
Typically, when code is completed for a certain portion of a project, it gets passed on to another person (or multiple people) for review, revision, and extension. With this being the case, keeping your code clear and logical is essential to allow other team members to work efficiently. So how do you write better code? While certain programming languages have their own unique best practices, there are some general guidelines that you can follow no matter if you write with Python, Ruby, JavaScript, or your own preferred programming language.
Plan Ahead and Outline Your Process Beforehand
Similar to tackling writing projects, research, or even home improvement, the more you plan out your process in advance, the more you can anticipate issues and areas of focus and use your time effectively.
Going through the planning process when coding also lets you see how processes you may want to use in early stages of development may become complicated in later stages. Here are a few tips to make your planning process more efficient:
- Consider using a tool like Agile Manager, which allows you to collect user stories and client needs and coordinate a plan of attack for future steps, thus making coding more efficient and avoiding miscommunications or conflicted coding.
- If you are working as part of a team, a project management tool like Trello can also help you create individual to-do lists, coordinate on assigning tasks and project components, and keep track of what has been completed.
- Project managers or senior developers will usually be in charge of organizing and managing the more junior developers on a major project, but if you are working individually, you may have to do this yourself. Fortunately, there are some great resources out there that can help you organize your own project for the first time.
While Coding, Use Comments The Right Way
Much like outlining your process before beginning to write code can help avoid missteps later on, using comments appropriately from the start of a project can also prevent issues down the road. Creating documentation of the process ahead of time also helps you think through what you’ll be creating even further. You might realize a flaw in your plan beforehand, instead of as you write and work, or afterwards.
Here are some other tips and best practices for using comments:
- Don’t comment on everything. Be specific and intentional in choosing where you add them, and keep them brief to avoid wearing out anyone reading them
- Too many comments can make code unreadable and messy. Try to condense where you can.
- Focus on potential misunderstandings others may have when choosing where to add comments.
- If you are looking for specific feedback, use comments to point your reviewer to those spots to make revision more useful.
Proper Use of Descriptive Names
As you name different classes, variables, methods, functions, and so on, pay careful attention to making the descriptive names informative and unique.
- For example, you should give related features similar names, but unrelated features should be clearly differentiated to avoid vagueness and potential confusion.
- Remember to keep all names current and logical as the build continues and functionality changes. Make sure to revisit your descriptive names later in your coding and revise as needed to ensure changes are reflected.
- Learn and utilize common naming conventions for a specific language you are working with (for example, camelCase when using Java). Since conventions are created for uniformity specifically to avoid confusion, not using them will frustrate and confuse others who rely on them to navigate through code files.
Take Simple Steps To Keep Code Clean and Concise
There are some simple best practices you can utilize to help "slim down" your code to make it easier to read and work with to maximize efficiency.
- Avoid long nesting: A common beginner/inexperienced coder mistake is excessive or overly complicated nesting. When you add too many levels of code, the code can become unnecessarily complex and make a fellow team member’s revisions both complicated and potentially damaging if the wrong changes are made. Try to find ways to limit the levels to avoid this easy trap.
- Delete unnecessary code: Think of this as "cleaning up your workspace". While you might know why unnecessary code has been left hanging around, someone else won’t. Removing it makes code more easy to read and less confusing.
- Create multiple files: While an entire application’s code could go in a single file, the hassles of trying to read and edit a giant file far outweigh any convenience. To avoid this common issue, create clearly delineated files for different aspects of the build, labeled with a system that is consistent for others to navigate
- Separate languages into files: Don’t mix different languages such as HTML, PHP, or CSS in a single file. Divide your code into separate files in such a way that you avoid excessive HTML in PHP files -- and vice versa.
- Prioritize readability: Remember, readability is always the preference over being clever in writing your code, especially if you will be collaborating as part of a team.
- Avoid repeating code: Repeating code will make your document very long and it will break the reading flow. If you have pieces of code that are used more than once, it is preferable to make a separate file and include the file path when needed. For example, most pages will have the same header and footer, but there is no need to copy-paste the same code onto every page since you can simply link to it.
Organized Processes for Reviewing Code Are Key
While many teams and project managers require peer code review, if you are working on your own or with a less experienced group, sometimes code review can fall by the wayside. Here are some tips and resources for implementing an optimal code review process:
- Collaborate with team members and project managers to come up with a routine schedule for reviewing code. By setting benchmarks for submitting code for review, you can increase productivity of individual team members with deadlines/goals and also ensure reviews happen regularly
- The more reviewers and feedback, the better. Beyond the fact that the more eyes on code the better the chances to catch bugs and optimize the code for clarity, different team members bring different perspectives and may each catch something that another programmer did not.
- If you are working alone on a project or need extra help with reviewing your code, there are a number of effective sites for checking code quality and evaluating code automatically. Make sure to use some of these tools to serve as a "virtual editor" for your project.
Practice With Coding Challenges To Build Experience
While all of the tips above are considered best practices in the coding community, the truth is that the ability to effectively review and edit code for clarity often comes with experience. The more projects and specific problems you work on, the easier it is to anticipate issues and catch bugs early on in future projects.
Fortunately, there is no shortage of websites with professionally created coding challenges and problems for you to work through and hone your skills before working on a big project. Develop a "practice routine" by completing a number of coding challenges every week or month to build your own skills and simulate the work pace of an actual coding project.
Learn From The Best By Reading Their Code
Much like practicing with writing code and solving coding problems can help you learn in advance, learning from experienced professionals by reading their code is equally important for beginning coders looking to level up:
- Learning from others’ mistakes and seeing the differences between clear and confusing or vague code and crisp, clean code can also help you learn the upsides and downsides of writing both.
- There are numerous resources on the Internet with best practices for reading code as well as plenty of useful sites for actually writing code yourself. Github and Bitbucket are often cited by many in the coding community for their variety of projects and open-source options for you to review.
- Consult with various coding communities and forums if you see something that confuses you and want an explanation. Most major languages have thriving developer communities that share tips, workarounds, and are available to answer questions from their own experience to help you grow.
Photo Credit: Oleg Ivanov IL/Shutterstock