[LINUX] Bad code

Coding sense

Do you have a sense of coding? Will you choose the best architecture? Is it writing code that is highly testable and easy to change? Is it just to write code fast?

I think it varies from person to person, Who has a sense for Linus Torvalds? It seems to be a person who can understand the correct code intuitively by looking big. image.png

For example, someone who can see that a special case of an if statement can be generalized. The following example is a code fragment introduced in the above video converted into a working code.

 #include "common.h"

 typedef struct linked_list
 {
     int value;
     struct linked_list* nextp;
 } linkedlist;

 linkedlist* firstlistp = NULL;

 void initList(int value);
 void addList(int value);
 linkedlist* findList(int value);
 void deleteList(linkedlist* entry);
 void printList();

 int main()
 {
     initList(11);
     addList(12);
     addList(13);
     addList(14);
     addList(15);
     addList(16);
     addList(17);
     printList();

     puts("TEST A");

     linkedlist* lp = findList(14);
     deleteList(lp);
     printList();

     puts("TEST B");

     lp = findList(12);
     deleteList(lp);
     printList();

     puts("TEST C");

     lp = findList(16);
     deleteList(lp);
     printList();

     puts("TEST D");

     return 0;
 }

 void initList(int value)
 {
     linkedlist* newp;
     newp = malloc(sizeof(linkedlist));
     newp->value = value;
     newp->nextp = NULL;
     firstlistp = newp;
 }

 void addList(int value)
 {
     linkedlist* newp;
     newp = malloc(sizeof(linkedlist));
     newp->value = value;
     newp->nextp = firstlistp;

     firstlistp = newp;
 }

 linkedlist* findList(int value)
 {
     linkedlist* currentp;
     currentp = firstlistp;

     while((currentp != NULL) && (currentp->value != value))
     {
         currentp = currentp->nextp;
     }

     return currentp;
 }

 #define BAD

 void deleteList(linkedlist* entryp)
 {
 #ifdef BAD
     linkedlist* prevp = NULL;
     linkedlist* walkp = firstlistp;

     while(walkp != entryp)
     {
         prevp = walkp;
         walkp = walkp->nextp;
     }

     if(!prevp)
     {
         firstlistp = entryp->nextp;
     }
     else
     {
         prevp->nextp = entryp->nextp;
     }
 #else
     linkedlist** indirectpp = &firstlistp;

     while((*indirectpp) != entryp)
         indirectpp = &(*indirectpp)->nextp;
     *indirectpp = entryp->nextp;
 #endif

     free(entryp);
 }

 void printList()
 {
     linkedlist* currentp = firstlistp;
     while(currentp != NULL)
     {
         printf("%d\n", currentp->value);
         currentp = currentp->nextp;
     }
 }

The deleteList function was introduced. The difference between the BAD route and the else route is that there is no if statement. The special route disappeared and was generalized.

readability

When talking about code quality, the topic of readability comes up. Is the BAD code easy to read? If you see this code in your review, you might choose the BAD code. I think so at least in my workplace.

Bad code

I think there are many ways to achieve a function. The issues that come to the fore are speed, readability, and scalability. Since the execution speed can be measured, it can be objectively judged that it is faster or slower, Readability varies from person to person, and expandability is limited as long as we don't know where it will change. It also depends on how much it depends on the configuration file. Good or bad taste may depend on the situation. However, there is a good code for each situation. What Linus says is that there are good and bad codes, and there are people who can intuitively understand them. difficult.

Digression

When I started writing without thinking about what I wanted to say, I didn't understand until the end, and it ended up being an inconclusive article. It seems that I have neither a sense of coding nor a sense of writing. I have written a linked list since the introduction to C language. Until now, I never wrote it myself in my work. So, I wrote it after a long time, but I think the link list is quite difficult. There are some things that are more difficult in terms of processing, but I thought it was a high hurdle for program beginners to write. The concept of adding to the front when adding, processing of the delete function These two hurdles are high. You can do it, and if you make a mistake, it will SEGV immediately, so it's easy to debug, and it may be functioning as a good behavior.

When I went back to the beginning and coded while drawing a picture, it became a picture that no one could understand. Do I have no sense of painting? .. ..

image.png

Recommended Posts

Bad code
Character code
Write python-like code
FFT pseudo code
VS Code settings
python character code
[Python] Algorithm-aware code
QR code display
Zip code geocoding
PyCharm code interpolation
Good and bad code to compare on a minimap