The Most Beautiful Code I Never Wrote
I got my first job as a software programmer in 2005. Two days after I started, one of my system engineer assigned a small task, to calculate the number of incoming MSU and outgoing MSU in a signaling gateway. I started writing the code without thinking the nature of input and format of output . There were many programming languages I could have select, but don’t know why I selected ” PERL SCRIPT” .I started with file operations , open the file , read the content and count operation of incoming MSU and outgoing MSU . Total number of code was around 100-150 lines. I was very happy and I appreciated myself .Somewhat code worked fine. Later My system engineer did the same task in single line by using “SED” operations with some regular expressions.
I have understood one of the basic and important rules of software development. After understanding the requirement, it’s very important to select the right tool to develop and implement the same. We, many of the software developers not concerned about this. A beautiful code piece is not measured based on the KLOC .,but from its simplicity and performance. It’s always important to follow some coding standards
int i , j ,k .. ( always use meaningful variable Name – that need to tell a lot of stories )
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
(GOOGLE Coding Style)
Test your beautiful Code
Most of the programmers have had the experience of looking at a piece of code and thinking it was not only functional but also beautiful. Code is typically considered beautiful if it does what it’s supposed to do with unique elegance and economy.
A combination of things makes tests beautiful and elegant. For example every if statement in the code requires at least two tests (one test for when the condition evaluates to true and one when it evaluates to false). An if statement with multiple conditions, such as:
An if statement with multiple conditions, such as:
if ( a || b || c )
Tests those are beautiful for their breadth and depth
Common Software Coding Errors.
1. Confusing character and string constants
C considers character and string constants as very different things. Character constants are enclosed in single quotes and string constants are enclosed in double quotes. String constants act as a pointer to the actually string. Consider the following code:
char ch = ‘A’; /* correct */
char ch = “A”; /* error */
The second line assigns the character variable ch to the address of a string constant. This should generate a compiler error. The same should happen if a string pointer is assigned to a character constant:
const char * st = “A”; /* correct */
const char * st = ‘A’; /* error
2. Not null terminating strings
C assumes that a string is a character array with a terminating null character. This null character has ASCII value 0 and can be represented as just 0 or ‘\0?. This value is used to mark the end of meaningful data in the string. If this value is missing, many C string functions will keep processing data past the end of the meaningful data and often past the end of the character array itself until it happens to find a zero byte in memory!
Most C library string functions that create strings will always properly null terminate them. Some do not (e.g., strncpy() ). Be sure to read their descriptions carefully
3. Size of arrays
Arrays in C always start at index 0. This means that an array of 10 integers defined as:
int a[10];
has valid indices from 0 to 9 not 10! It is very common for students go one too far in an array. This can lead to unpredictable behavior of the program.
4. Not initializing pointers
Anytime you use a pointer, you should be able to answer the question: What variable does this point to? If you can not answer this question, it is likely it doesn’t point to any variable. This type of error will often result in a Segmentation fault/core dump error on UNIX/Linux or a general protection fault under Windows. (Under good old DOS (ugh!), anything could happen!)
Here’s an example of this type of error.
#include
int main()
{
char * st; /* defines a pointer to a char or char array */
strcpy(st, “abc”); /* what char array does st point to?? */
return 0;
}
How to do this correctly? Either use an array or dynamically allocate an array.
#include
int main()
{
char st[20]; /* defines an char array */
strcpy(st, “abc”); /* st points to char array */
return 0;
}
or
#include
#include
int main()
{
char *st = malloc(20); /* st points to allocated array*/
strcpy(st, “abc”); /* st points to char array */
free(st); /* don’t forget to deallocate when done! */
return 0;
}
Actually, the first solution is much preferred for what this code does. Why? Dynamic allocation should only be used when it is required. It is slower and more error prone than just defining a normal array
To be continued – Beautiful Code “Complexity”
