Showing posts with label best practices. Show all posts
Showing posts with label best practices. Show all posts

Friday, April 11, 2014

How to Write Better Code

Writing code is all about readability.  Even if it's just for you.  When writing code, using some best practices and standards will make your life a lot easier when trying to fix errors, stay organized, or when working with teammates.

1. Use Comments to Help Readability



Comments within code are useful for several reasons.  First, comments are a great way to type out in a conversational language what the code is doing.  Second, they are a great way to organize your code.  If you're scrolling through a long piece of you, it's a lot easier to look for comments than a particular line of code; they stand out more.  Third, it's a great way for team members to understand what you were doing or thinking as well.

2. Use a Standard When Indenting



When writing code, make sure to keep a standard throughout all of your code.  When you indent a certain way, do it for a similar piece of code later on.  It helps improve readability, audience understanding, and it might even help you spot errors.

3. Use a Standard When Naming Files, Classes, Etc.

Take a look at the code in green.  It's in camelCase.

There are several ways to name files and it really just boils down to what you're comfortable with.  A lot of people might be used to doing something like this: Desk_Talk_Blog or DESK_TALK_BLOG, or Desk-talk-blog.  But for me, I like to use a modified version of PascalCase called camelCase.  I will keep the first word lower-cased and for the second word I will capitalize the first letter and so on.  This is what it ends up looking like: deskTalkBlog.  So why do this?  Again, it helps with readability and it just gives you some organization.  I recommend doing it throughout your website down to the folder directories.

4.  Avoid Deep Nesting and Keep Line Length To A Minimum

Don't do this
It's easy to get carried away with nesting.  It is usually a great way to keep organized and matches a standard when indenting, but sometimes it is totallly unneccesary and just adds difficulty when trying to read it.


Furthermore, keep the length of your lines short.  It is so annoying when you're trying to read a line of code and you have to scroll to the right to continue reading it.  There's a reason why newspapers use multiple columns rather than one column; it's much easier to read.

That's it for now.  These tips have mostly been focused on HTML/CSS, but they can extend how to other languages as well.

Thursday, February 20, 2014

GitHub: the open source backbone for your projects


If you are unfamiliar with GitHub, it is a code sharing community where programmers can network to tackle projects together.  In the beginning, it was mostly intended for code use, but now people are going beyond that and hosting non-code documents and files so that multiple people can work on one project. 

Users can host their projects on the site by “pushing” their content through Git, which is the source code management (CSM) software behind GitHub.  Linus Torvalds, who was the lead developer behind the Linux operating system, created Git in 2005.

Git operates by hosting projects in directories, or in GitHub’s case, repositories.  These repositories are then hosted on GitHub where anybody with an account can “fork” the project.  Forking a project creates a copy of the existing project and places it underneath your profile.  From here, you can continue to view the project, but the most important feature is the ability to edit the code.

My GitHub profile.


For developers, this is big.  GitHub is excellent for new open source projects.  With several developers forking and editing a project, a project can become so much more than what one person could complete.

For new users, GitHub can be kind of overwhelming.  It involves using Terminal, or whichever command-line software your operating system uses.  Because of this, many people don’t use GitHub.  To help, here are two tutorials to get started:


My two cents:

As an aspiring front-end developer, I’ve questioned whether or not it is worth using GitHub.  I recently used it for a project in which I was in South America without internet access for most of the day. 

Using GitHub actually promoted good coding practices.  GitHub provided a backup of my site in case I happened to lose or damage my laptop, which was a very strong possibility in one of the biggest cities during the rain season.

It is also promoted working offline.  I could code during the day without Internet access and push what I had completed when I arrived at the hotel.

It allowed for easy collaboration.  I worked closely with a professor during this time.  When we ran into a problem, we would fork each other’s work and push our ideas back at each other.  It was a smooth and painless process.

Finally, I had a complete project to show off on GitHub.  To me, having the final product on GitHub was like a trophy.  It documented all of my hard work; it showed every update I completed.

In the end, if you learn how to use GitHub, you’ll be strongly rewarded.  On top of learning some great coding practices and being a part of a popular, active community, you’ll have the opportunity to be a part of the next big project.  You might just fork and edit the code for the next Facebook.