I made a statement sometime ago, "Segmentation faults are better than infinite loops", which makes me think, how in the first place, the code went into an infinite loop?
Possible reasons :
- messed the loop variant
- missed an assignment
- don't know where the loop should end in the code!
- based on the looping type, increment or decrement the variant appropriately,
- if its pointers that one is messing around, the most typical assignment node = node->next; is messed, un-mess it.
That's the motivation for this rant (a continuation of my previous rant). Write readable code!!
Its not a small task to try and understand code written "once upon a time, long time ago". We need comments accompanying the code to understand what the code does and why? I have written about commenting code in my previous post.
Matters are worsened when the code itself is not readable.
Try to figure out what the code below does?
void maxh (char uidArray[][17],long int n) {
int i,j,k;
char item[17];
for(k=1;k<n;k++) {
strcpy(item,uidArray[k]);
i=k; j=(i-1)/2;
while(i>0 && strcmp(item,uidArray[j])>0) {
strcpy(uidArray[i],uidArray[j]);
i=j; j=(i-1)/2; }
strcpy(uidArray[i],item); } }
For now, lets not bother what the code does ( that's a part of my Algos assignment code btw :D )
Any normal human would find the code above un-readable.
Reasons are simple, I have not explained what the code does, neither have i written the code in some readable manner.
If the same code above was written little better**, at least we can try to figure out what the code does.
Indentation and spacing play an important role while writing good code. Coding conventions are something that every one of us programmers have to follow. Primary intent of having coding conventions is to ensure uniformity in the code.void maxHeapify(char uidArray[][17],long int n) { int i,j,k; char item[17]; for(k=1;k0 && strcmp(item,uidArray[j])>0) { strcpy(uidArray[i],uidArray[j]); i=j; j=(i-1)/2; } strcpy(uidArray[i],item); } }
Consider a project on which five programmers are working. If each of them follow their own ( mostly no-standard ) convention to write code, the day when bugs become hard to detect and fix is not very far! This is one of the prime reason why a standard is enforced while coding.
In fact many of the programming languages have their own coding conventions.
As always, I am not enlisting any of those here. I am just ranting about non indented code :-D
However, if I may suggest, I am listing here some pointers to the coding conventions and rules of various languages.
C is a common language most of "programmers" get introduced to initially, it pays to know how code is written in C. There is a three part literature on the same: Naming, Style and formatting and some best practices in www.cprogramming.com
We can find conventions for Java here.
Perl is my favorite language. Here is how we write Perl code.
** I have removed the comments here to keep the post length small :)
PS:
Links provided are copyrights of the respective authors.
1 comment:
UUUUUUUUUUUU NERRRRRRRRRRRDDDDDDDDDDDD
Post a Comment