You are on page 1of 8

Fundamentals of Computer

Programming

Coding Standards

Lecture 19
Being a good developer
 Why would you want to be a good
developer? … well it is going to be the
bread and butter in your careers.
 If you make some good coding habits
now, they will pay dividends in the long
run
 The road to being a good developer
starts with following good coding
standards!
Coding Standards?

 These are general rules of writing


code … kindof similar to what we have
in English, like “thou shalt always start
a new paragraph with an
indentation/tab”

 The rules of coding a much cooler


than the English language … behold …
1. Variable Naming

int nCount; // and not ‘int a;’


char szFilename[MAX_PATH];
// and not ‘char arr[256];’
bool fValid; // and not ‘bool c;’

 ‘n’ in nCount means the variable is an int


 ‘sz’ in szFilename means the array will contain a string
terminated by 0
 ‘f’ in fValid means it’s a flag … i.e. true/false
2. Indentation in code
Bad Code Good Code
void main() void main()
{ {
int a=1; int a=1, b=2;
int b=2;
if(a!=b) if( a != b )
printf("\nHello"); {
printf(" World"); printf("\nHello");
for( int i=0;i<4;i++) printf(" World");
for( int j=0;j<4;j++) }
{
if(i==j) for( int i=0; i<4; i++)
printf("\n%d. Hey There", i ); for( int j=0; j<4; j++)
else {
printf("\n%d. Ho Ho Ho", i );} if( i == j )
printf("\nBye Bye" ); printf("\n%d. Hey There", i );
} else
printf( "\n%d. Ho Ho Ho", i );
}

printf("\nBye Bye" );
}
3. Liberal use of Preprocessors

Bad Code Good Code


#include <stdio.h> #include <stdio.h>

void main() #define MAX_NAME_LENGTH 50


{ #define MAX_NUM_OF_STUDENTS 100
char szNames[100][50];
void main()
{
for( int i=0; i<100; i++ )
char szName[MAX_NUM_OF_STUDENTS][MAX_NAME_LENGTH];
scanf("%s", &szNames[i] );
for( int i=0; i< MAX_NUM_OF_STUDENTS; i++ )
for( int j=0; j<100; j++ ) scanf("%c", &szNames[i] );
printf(“%s", szNames[j] );
} for( int j=0; j< MAX_NUM_OF_STUDENTS; j++ )
printf("%c", szNames[j] );
}
4. Always Debug your code
 Use breakpoints (F-9) and always run in
Debug mode (F-5)

 Step inside functions with F-11 key when


debugging to checkout if everything is good

 Always test your code with different input


data before signing off on code
That’s it …

Best of luck to you all …


and wish you a good
career ahead!

You might also like