Five Ways To Write Better Code

« »

When I was first starting out in development, I thought that writing code was pretty easy. It took me a while (and a long learning process) before I realized that writing code is harder than it looks. Looking back on some of that first code, I wonder how it ever worked, how I avoided a serious security problem, and what I was thinking about when I wrote some of that code.

Experience is the way that most of us learn how to write code, but experience is a lousy teacher: it gives the lesson after the test. Here are some ways you can improve your code writing right now.

1. Read a lot of code.

Developers spend a good amount of their time looking at code written by other developers. We can gain valuable insights into coding practices by reading a lot of code. The code doesn’t necessarily need to be pristine and perfect in order to be useful; it only needs to be code that you’re unfamiliar with. Also, it need not be code that is necessarily in a language you know or know well. Reading code in other languages can often be a great way to learn something about the nature of languages or the way that code works.

2. Fix bugs in code you’ve never seen before.

If you think that fixing bugs entails writing code, step back for just a minute. Fixing bugs is not about writing code; it’s about far more! Fixing bugs requires several skills: reading the code, understanding how it works, creating and testing a hypothesis, and finally applying a fix. The smallest part of the fix is the actual application of the new code.

By utilizing code you’ve never seen before, you are automatically forced to go through the entire process: research, understand, experiment, debug, hypothesize, test, and fix. This will make you better at all these steps, which are all critical to writing better code.

3. Fix bugs in the open source projects you use.

I’ve never taken a hard look at the Apache source code before, but I have found a few bugs in Apache that I’d love to have fixed. If you commit to fixing bugs in open source projects that you use, there’s a good chance that several things will happen: first, you’ll have bugs in multiple languages to identify and fix. Second, you’ll have to work with others to get your fixes integrated into the project. And third, you’ll be able to learn several new systems.

Working with others to get patches accepted and bugs closed is a crucial skill in writing good code, because the best programmer in the world who can’t work with others leads a lonely and inconsequential life.

4. Learn another language.

By learning another language inside and out, you will boost your skills with all the other languages you know. Seems strange, but it’s true: different languages are designed for different classes of problems; being able to solve those problems will help you have a greater conceptual understanding of software development overall, which will improve your skills.

When I learned Python, it taught me a lot about the good (and bad) elements of PHP. It improved my object oriented development skills, as well as boosted my understanding of data structures. These are all good things.

5. Learn all you can about software development best practices.

Writing code is one small part of software development. Learning the best practices of the entire software development process is a crucial component to writing better code. Understanding how your bugs affect other aspects of the process, for example, will encourage you to write better code; understanding how users perceive your work will help you design better user interfaces.

Write better object oriented PHP today!

Object oriented programming always leaves you with a headache. What if you could master it instead? Stop struggling with object oriented PHP. Mastering Object Oriented PHP is the answer! Get your copy today and start writing better object oriented PHP.

Brandon Savage is the author of Mastering Object Oriented PHP and Practical Design Patterns in PHP

Posted on 1/15/2013 at 7:00 am
Categories: Learning, Best Practices, Software Development

Manmohan Bishnoi wrote at 1/17/2013 1:53 am:

Nice article.

I myself have learned a lot about write better code by using ways you have mentioned. Reading code of various open-source projects has taught me better indentation practices, how to organize large code.

schlingel wrote at 1/17/2013 3:40 am:

That’s nothing new. At least you’re not wrong and you got featured on the Code Project newsletter.

Alexei Rayu wrote at 1/17/2013 4:40 am:

Thanks, I enjoyed it. One part made me smile. The code does not have to be pristine or perfect. In fact, it does not necessarily be in a language you understand. In fact, you don’t even need to see it. Close your eyes, relax, let it flow over you, FEEL IT! :)

joseph wrote at 1/30/2013 4:23 am:

nice article, thanks alot

« »

Copyright © 2023 by Brandon Savage. All rights reserved.