Make Your Code Easy To Read

When writing code for a program, it's easy to fix all of the bugs and have it compile correctly, but making it easy for another human to read it is much harder. Here are some steps & tips that will help you to improve your code's readability.

Steps:

  1. Use white space. Skip lines between code that does different things, and use spaces after a semicolon and commas. This will make your code look cleaner and easier to read.
  2. Indent inside curly braces. This makes it so that it's obvious that a chunk of code is separate from another. This can help when writing a long method. Indenting inside if statements and loops will make it easy to read. This technique is called nesting that is it shows a chunk of code is "nested" within another chunk of code, and allows the reader to easily follow the nesting pattern.
  3. Write descriptive comments. Assume the person reading your code knows nothing about what it's supposed to do. Your comments should explain every step of the way. All of your advanced, confusing, or complicated algorithms should be well commented. Also, try to describe what purpose the code has.
    For example, instead of writing "add one to variable," say why this is important in the code, as anyone can see it is incrementing the variable, e.g. "increment the number of page hits by 1".
  4. Don't use long object names. Avoid names that contain unneeded words that can be omitted. Make your names short so that they don't get confusing. Also, make your code "self-documenting" by creating descriptive object name that describe what their purposes are.
  5. It may also be useful, particularly if you are writing longer pieces of code, to add a few lines of comments at the top of the software, giving information and a date for revised versions of the code (version history), along with the author of the code and date it was initially written.
  6. Comment often. Write comments for nearly everything you do and whenever you can. That way, you won't need a Vulcan interpreter to read your code if you decide to make a new version.

Tips:

CSS

h1 { }
h1#logo
{
font-size: 2em;
color: #000;
}
h2 { }
h2.title
{
font-size: 1.8em;
font-weight: normal;
}

On using this technique, you can easily find out different heading tags for CSS. This technique is also useful if you are sharing code or working on a large site where you are using the same heading tags such as (h2) in numerous places, since you can give style to each one separately and not to worry about child classes inheriting attributes from the parent class.

Similar techniques are used for paragraph tags, anchor tags, & any other tag that requires multiple classes to look correct in every occurrence.