Jump to content

Coding Procedures...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Beginners learns programming and do a large number of programs, but sometimes fail to use coding procedures...Coding procedures makes the program simpler, easy to understand, easy to decode and soon...

This tutorial is about some of the coding procedures that I learnt...

1. COMMENTS

Comments are the blocks that explains about what something does...
For example,
/* file.c

a one line descriptor of the contents of the file

copyrights, authors... 

*/


/* function_name()

one-liner for the function

about assumptions and limitations, etc

*/
Comments helps in understanding of what the code does... Comments should not explain about how does the code work... It is only needed when the code is complex... Simpler code needs just a one liner statement

2. INDENTATION

It is second most thing that is very important... If the indentations are poor, It becomes very difficult to decode where the braces starts and braces end, and lot more...

Indentation can be a char space or a Tab... Tabs are 8 characters... If the code has too much nesting of loops, dont use Tab Indentation because it will makes difficult to read the code .

3. BRACE STYLE

The placement of braces is not so important but still it makes the code look pretty. The opening braces should be on last of the line and put the closing braces at first. For example,

if ( x == y) {

	.... 

} else if (x  > y) {

	.....

} else {

	.....

}

If condition is shown in the example,. Likewise, it goes for while, for and switch as well...

However, for functions it can be at the beginning of the next line. For example,

int function ( ....)

{

	.........

}

NOTE: Do not use open version of statements. For example,

for ( x = 0 ; x < 100; x++)

	loop ( );

next( );

While using this you can feel bit easy but when somebody tries to modify your code, he might get tripped.
So always use
for ( x = 0 ; x < 100; x++) {

	loop ( );

}

next( );

4. C TYPES

Always use the standard C types... For example, If you have a struct pointer, just name it as such.

Use:

struct fun {

	int i;

	....

};

and not:

typedef struct {

	int i;

	....

} fun;

It does means that typedef shouldn't be used. It can be used for industry-recognized types like ushort (unsigned short) and so forth..

5.NAMING

use short local variable names within function. keep variables in the same class of length.
Globals ( both function names and variables)used across source files need to be have longer, more descriptive names.

6. ISO void *

When dealing with a generic pointer value, use ISO void pointers. Do not do maths with such a pointer. gcc might treat it as a "char *" , but don't assume that all the world is gcc. If you need, typecast them to (char *) and then do maths.

---------------------------------------

These are some of the coding procedures that I learned.. They are still lot more. If anyone knows some other procedures, Do share with us..

Hope you all find these very useful.. Any suggestions are welcomed...

#2
nikhilkhullar

nikhilkhullar

    Newbie

  • Members
  • PipPip
  • 20 posts
Nice Tips...

I would like to add a few that I've learnt from my experience:

Try using camel-case or interCaps for variable naming. Like:
monthlySalary, annualIncome etc.

Another common approach is: monthly_salary...

For pointer variables, it is sometimes convenient to use prefixes like:
ptrArrayNum...


Always Remember: Code is Poetry !


Regards,
Nikhil Khullar