DEV Community

Cover image for C the right way to program
Anshuman Khanna
Anshuman Khanna

Posted on

C the right way to program

Hey, so I have been going deeper into the rabbit hole of knowing what's underneath my program. I used to main JavaScript and went to TypeScript because I hate programming without types. But I soon got to know TypeScript is just an illusion of types.

This led me to finding the next perfect language that can be used in the backend and I started digging...

My first thought was going to Rust. It was famous, it was fast and best of all it was the first language that I was using that was truly type-safe. I had worked in C++ but after getting some thrashing in the reddit server of Rust I got to know that C/C++ aren't type-safe.

And I did go to Rust. I also went to Golang. I tried both languages just for the heck of it. Trying them both made me realize they are useless for me because these languages are birthed by professionals who transcended from C/C++. I would be repeating my mistake of instantly switching to TypeScript without completing JavaScript and later realizing my faults if I switched to Rust/Golang without knowing C/C++.

Now it's not like I didn't know C/C++. I had done both languages fairly...lazily...

I used C++ for DSA which is what most people use it for anyways because I guess that's what Bjarne Stroustrup imagined his hardwork would be boiled down to. But I had never used C seriously for anything.

Now I wanted to.

So I did that.

I started learning how to create a HTTP server in C. This has changed my perspective on how coding should be.

C is a procedural programming language. I tried to make it OOP, but soon realized that it didn't made sense. C is often criticized for it's lack of generics, memory safety issues and as I said, problems with type-safety. But actually, none of those issues are really issues. Those are as is commonly known skill issues.

C has simple syntax. Almost everything is explicit. You allocate memory, you have to check if there are any errors, you have to type cast things to make them understandable for compiler.

Yes it is a lot of stuff to do when you just want to write code but that's not my point. The point is you must know what is happening when you do something. In C almost nothing happens automatically and you are doing it. The same way in your language full of syntactic sugar, are you sure you know what all is happening?

For example, creating an enumerate function in C.

typedef struct {
    size_t index;
    void* val;
} EnumeratedArray;

EnumeratedArray* enumerate(void* arr, const size_t size) {
    if (size == 0) {
        return NULL;
    }

    const size_t elem_size = sizeof(arr[0]);
    EnumeratedArray* result = malloc(size * sizeof(EnumeratedArray));

    for (size_t index = 0; index < size; ++index) {
        result[index] = (EnumeratedArray) { index, (char *) arr + index * elem_size };
    }

    return result;
}
Enter fullscreen mode Exit fullscreen mode

I know exactly what's happening in this function, I know how everything works on the machine.

Do you need to know all this? No. Does it help to know this when you have to improve the performance of the application? Yes.

Summary: Don't code blindly, know your code, know your machine because that's where the code runs.

Note: This is a raw article, I am not going to reread or edit it, so if you find any faults in it, just create a PR.

Top comments (2)

Collapse
 
kushal_vadher_f5bfa88636d profile image
kushal vadher

you wrote impressive insights that makes me think of my skills thanks for valuable info i tried the HTTP server into c++ and still trying to understand how it works

Collapse
 
anshuman_khanna profile image
Anshuman Khanna

That's great, thanks