How to follow good programming practice?

Dear Friends, Good programming practice are the informal rules which can improve quality and decrease development time of the software.

Some of the programming practices mentioned here are valid in all programming languages whereas some are valid only for C programming.

# Be consistent with the formatting

The number of spaces you use in the program doesn’t matter in C. However, that doesn’t mean you should use different number of spaces at different places. Also, use proper indentation so that the code is easier to understand.

# Use one statement per line

What’s wrong with the following code?

int count; float squareRoot = 10.0; printf(“Square root = %f”, squareRoot);

Actually, the code is perfectly valid. But, wouldn’t this be better:

int count;
float squareRoot = 10.0;
printf(“Square root = %f”, squareRoot);

The goal here is to write code that your fellow programmers can understand.

# Naming convention and Consistency!

Give a proper name to variables and functions and be consistent with it.

int a, b;

Here, a and b are two variables and I have no idea what they do. Instead you can choose name like:

int counter, power;

Also, follow a convention while naming. For example:

int base_number, powerNumber;

Both conventions: using _ to separate words and using capital letter after first word is popular. However, don’t use both in one program; choose one and be consistent with it.

# Start Habit of Using Comments

Comments are part of code that compiler ignores.

You can use comments in your program to explain what you are trying to achieve in your program. This helps your fellow programmer to understand the code.

You can use comments in C programming by using //. For example:

// My first C program

#include <stdio.h>
int main()
{
    printf("Hello, World!\n");   // displays Hello, World! on the screen
    return 0;
}

“Commenting your code is like cleaning your bathroom – you never want to do it, but it really does create a more pleasant experience for you and your guests.”

— Ryan Campbell

I think C is a terrific language to learn.

If you are a beginner, it’s a good language to start learning programming. And, if you are a professional programmer with a tight schedule, learn it at your leisure; it will certainly help your brush up on programming basics.

# Learn C programming, the right way

Fact: 

You cannot learn C programming in a day. You might have difficulty grasping the important concepts if you try to learn C programming faster.

If you just learn the syntax and dive in, your C code may work but you’ll not end up learning C programming the right way.,

Don’t just look at the example code, run it in your system

Physboy has dozens of examples that will help you understand C programming. However, if you are reading it like a novel without running it in your system, you are doing it wrong.

If you want to learn a new feature of C programming, try to write code related to that feature. If you can’t, copy code from Physboy and run it in your system. Then, try to understand it. Once you understand it, modify the code, make it different and run it again.

Learn to use a Debugging tool

A debugging tool or debugger is a software (that’s already available in IDE) which allows programmers to stop a program at any point and helps to detect and correct errors.

When you have bugs in your program, rather than scratching your head to find the bug, you can use debugger to stop program at any point and find the value of variables to detect the bug.

Knowing how to use a debugger is an important skill that every programmer should learn.

Learn the feature the way it is intended 

Imagine a situation: It’s possible to solve a problem with an array. Also, you can solve the same problem with a structure. Technically both are correct, which method will you use?

To answer this question, you should have a good knowledge of when to use an array and when to use a structure.

Suppose, you are switching to C from a different language, let’s say C#. You do not want to write C# style code with C syntax in your C program.

# Join C communities

Once you get the hang of writing simple C programs, join online communities and forums. You can help other programmers where you can and ask for help when you are stuck.

PhysBoy recommends you to join:

We hope you enjoyed to read this article. If you have any questions, please comment below!

Previous                                Main Menu                                  Next Page

Leave a Reply

Your email address will not be published.