DEV Community

codewithbug
codewithbug

Posted on

Do you think writing clean code is a bit difficult in javascript compared to other languages ?

Is it just me or it is harder to write readable code in javascript (especially in oop paradigm)

Top comments (4)

Collapse
 
aminnairi profile image
Amin • Edited

I think the main reason why it is so difficult about writing clean code is us, developer.

I often heard people saying that being lazy is not good, unless you are a developer. And most of the people out there have learned to adopt this moto without good reasons. There is nothing good at being lazy in our field of work. But instead of being lazy we should be smart.

Because lazy would mean writing a library that takes the least amount of work to be done. I see many libraries brag about being the fastest, about being the easiest to use. That's great really I don't mind that. But if that comes at the cost of the maintenance and the bugs that are created, leaving us a night of debugging because there has been a lack of runtime type checking in a chain of library calls, I would rather prefer a code that is 10x slower and that is reliable to use than that.

And most of the time this is what we saw:

function add(first, second) {
    return first + second;
}

Instead of this:

/**
 * Add two numbers together.
 *
 * @param {number} first The first number to add.
 * @param {number} second The second number to add.
 *
 * @throws {Error} If the function gets called with not exactly two arguments.
 * @throws {TypeError} If the first parameters is not a number.
 * @throws {TypeError} If the second parameters is not a number.
 *
 * @return {number} The addition of the first and second number.
 *
 * @example
 * add(1, 2);   // 3
 * add(-1, 2);  // -1
 */
function add(first, second) {
    if (arguments.length !== 2) {
        throw new Error("Expected two arguments.");
    }

    if (typeof first !== "number") {
        throw new TypeError("Expected first argument to be a number.");
    }

    if (typeof second !== "number") {
        throw new TypeError("Expected second argument to be a number.");
    }

    return first + second;
}

And that saddens me a lot. Because JavaScript itself has nothing wrong. No languages are perfect. Unfortunately, the path that JavaScript has taken (being an overly accessible and easy language for beginners to pick) has probably made it one of the most misused languages of all time.

Plus the fact that it tries to be both an object-oriented (but it is not since it is prototype-based) and a functional (but it is not, or at least not entirely) makes it even more difficult for newcomers to chose between the two to write good APIs. Also, the language itself does merely nothing to help us make the smallest amount of mistakes since it does not have type system baked into the language to do type checking, compared to TypeScript or Elm for instance.

So yeah, maybe JavaScript has gained its popularity because it is very easy to learn the basics of the language compared to TypeScript or Elm again, and it has contributed to the popularity of the Web ecosystem lately, but I think that now that its popularity is no more to justify, it should thrive for robustness and reliability.

But this is only my opinion, and I didn't have this opinion when I was a complete beginner since it was an opportunity for me that the language was so easy to learn and be productive with. Anyway, there is no very good answer to that question, we can only see that through the prism of our own experience and tell our own vision of what a language should be.

Collapse
 
codewithbug profile image
codewithbug • Edited

thank you for your response. i often look at the opensource project at Github, for projects which are written with php or python it it easier to read and it takes less time to understand what it is for. but when it comes javascript , i think javascript's oop approach is not what it should be, i mean in my opinion it lacks of oop principles. when i wrote code in a procedural style , other people read and understand it a lot easier but when i do it with oop paradigm, it is harder for them to read it. i think that another reason is manupilating dom with js. generally when i write a script without any DOM interactions is it much more easier to read and extend it

Collapse
 
aminnairi profile image
Amin

I agree with all of your points.

JavaScript prototype-based was one of the hardest points and I spent a long time understanding it coming from C & C++.

Recently, with the ECMAScript standard, they tried to make it look like a real OO language by adding some keywords like class or static but it is just syntactic sugar over this prototype-based programming paradigm. And since they are two different concepts, this OO emulation in JavaScript is nowhere near complete compared to PHP or C++.

In my opinion, it was a huge mistake. They should have gone to a more procedural path, or even functional, it would have been far easier to take, again still in my opinion.

It's funny how I can relate to your comment. I feel less lonely now. Haha!

Collapse
 
coolscratcher profile image
CoolScratcher

No, I don't think...