Best Practices when Programming for Reliability

Instructor: Lyna Griffin

Lyna has tutored undergraduate Information Management Systems and Database Development. She has a Bachelor's degree in Electrical Engineering and a Masters degree in Information Technology.

Programmers have developed a series of best practices when programming to ensure reliability, longevity and maintainability of developed code. In this lesson, we will look at a number of best practices used in the world of programming that can be applied irrespective of the programming language used.

The Need For Best Practices in Programming

The success of any project or implementation rests on its longevity and maintainability. The longevity depends on its adaptability to change. In simple terms, if the project or application is not sustainable in the absence of the creator then that project soon dies because nobody has the ability to take up the reigns of continuity and maintenance. The founders of the most prestigious cars today, lived and died long before our time, yet the legacy of their inventions is kept alive till this day.

Creating software cannot be limited to developing applications that meet their basic requirements but creating maintainable, robust and readable applications is the need of the hour.The following best practices are a general guide to the way code is to be written.

Have a Plan

No planning no coding. Coding without a plan is like building a house without a blue print. Things are added and changed in an adhoc manner which could lead to high costs and unreliability. A plan ensures that the programmer is well versed with the requirements stipulated in the problem definition. A plan should include:

  • An organized file and folder structure for images, CSS files, Js files etc.
  • A system that will allow future adaptability to cross-platforms.
  • An organized system for reusable code such as menus, headers, functions and classes for example.

The plan also ensures that the solution is implemented efficiently. Written code is far more expensive than the overall plan. It costs less to trash a plan than to trash written code.

Make the Code Understandable

For the applications to be sustainable the written code should be easily understandable by any other adequately experienced programmer, presently or in the future. Many applications are used and re-adapted long after the coder created them. This involves the way a programmer comments, indents and writes the code.

Readability

Readability is the ease with which computer interprets the code to execute it and the programmer can return to the code when necessary and be able to understand it. In a professional environment involving a team of programmers, this characteristic is very crucial for the smooth flow of work. Readability makes deciphering the code very easy.

Indentation

Indentation is the placement of text further to the left or to the right in comparison to the rest of the text surrounding it. Indentation helps readability. For example, if a complex loop with multiple decision conditions like if-else are properly indented, it makes it easier for someone to figure out where each program block begins and ends.

Comments

Commenting of code helps the reader to better read and work through the code and figure out exactly what is happening at every point in time. Comments are little explanations placed at strategic points in the code to make things clearer. They usually carry as much information as possible in a short length of text. Comments should be added even when code seems self explanatory. Figure 1 below shows a piece of code with comments using //.


Figure 1: Comments in Code
best pract1


Naming Conventions

Proper naming conventions work hand in hand with readability as a best practice. Adopt an easily understandable naming format for functions, variables and classes names. For e.g. a variable for storing student's age can be called studentAge, a function to calculate salary can be called computeSalary(), etc.

Validations Checks

Validation Checks refer to mechanisms which are incorporated into code to ensure that all input data (values) conform to that input field's requirements. In other words, the user of an application, for example, can only enter integers (whole numbers) into a 'credit card number field'. If the user attempts to use letters or strings (letters mixed with numbers) an error will occur, generating an error message and the entry will not be accepted. There are many different types of validation checks used to authenticate all types of input data within the application.

Optimize Code Efficiency

It is one thing to write a working code but writing code that is efficient and executes quickly takes additional skills. Efficiency can be achieved by the use of loops, arrays, proper use of boolean functions, for example. In the following example we will see how a loop is used to improve code efficiency. A loop is a sequence of instructions that repeatedly executes itself until a particular condition is met. This helps code execute faster and there are as fewer lines of code as necessary. Let us examine the code in Figure 2 below. The condition in the following loop is to display 6 years of expiration dates:


Figure 2: Sample Code
best pract1


A loop reduces the lines of code to this:


Figure 3: Sample code with a loop
sample code loop


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support