Six Tips for Writing Clean Code

Tips for Writing Clean Code Boot Camp Blog

 

Despite what you may think, not all code is created equal. There’s good code, bad code, and sometimes what’s referred to as “spaghetti code” (it’s pretty much exactly what it sounds like). We want to make sure you’re writing the good stuff, so here are a few pointers on how to clean up your code and write with quality in mind.

 

  • Label variables and functions with meaning.

Be descriptive when you’re labeling variables and functions to the point where you may be “overly descriptive.” You want to make sure that if you or a co-worker were to go back through your code at a later date, you’re actually able to decipher the original application.  

 

  • Leave some whitespace.

Not only does leaving a little white space look cleaner, but it also allows you to easily see and read the code you’ve written. Some professionals might argue that leaving too much whitespace will increase the file size or make it seem like you’re embellishing the number of lines of code. But if you’re able to find a good balance between keeping it neat and readable yet also manageable, there’s nothing wrong with leaving some space between commands.

 

  • Don’t be afraid to leave comments.

This one might be a no-brainer, but we just want to emphasize how important commenting really is. You don’t need to go overboard on the comments. Just keep in mind that leaving a note to yourself (or anyone who reads your code) explaining why a certain line of code exists can be helpful in the future.

 

  • Automate duplicate code.

Programmers are always trying to figure out ways to work smarter and not harder, and automating duplicate code is certainly one way to do it. If you’ve written a command that is repetitive, you can use a single variable that contains a list value. This allows your commands to be more flexible and structured.

 

  • Group similar variables together.

Keeping your variables in groups allows for easier comprehension down the road. By grouping them in categories based on type or grouping miscellaneous variables with a list at the top to help you decipher what these items are, you’ll be better equipped to keep  your code clean and easy to read in the future.

 

  • Keep functions short and simple.  

By using smaller, simpler functions, your code becomes easier to read and absorb. For many beginners, this is helpful and can make them into better programmers in the long run. Also, by separating these functions, other parts of your code are able to use that same function without having to create duplicate code.

 

Visit our experience page to learn more about how you can jumpstart your career in web development. And for more tips on how to write better code, follow The Coding Boot Camp at UCLA Extension on Facebook.

Get Program Info

The following requires your attention:
Back
Back
Back
Back
Back
Back
Back
Back
Back
0%