DEV Community

Discussion on: Bad Habits of Mid-Level React Developers

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

God damn! using TS as "good practice"... The post was good since I read such a stupid thing.

I encourage you to learn JS properly instead.
We've tools in JS to use as we need them, it's not the most used programming language around the world just by chance.

facts

Do you want type checking? Simply add

// @ts-check

at the top of your file, VSCode will handle it for you in dev time[*1], all you need to do is to add JSDoc to your functions/methods and variables, which is one of the most important best practices: Document your code.

Do you want types and function/method inference? Again, JSDoc.

Better intellisense? Guess what... JSDoc.

Do you want to produce a maintainable code? Apply SOLID and KISS patterns.

Since we're talking about react, you should know about prop-types as well, more useful than type-checking if you take in mind the context of React[*2].

About data fetching libraries, there's fetch API by default in JS, that can handle ALL existent features that HTTP requests provide (moreover fetch API is coming to Node as well). Why adding an additional one?

One important (and real) good practice is to add the minimum amount of third party libraries into your project. This makes your project more robust, faster to build/deploy and usually more secure.[*3]

personal opinion

I find styled-components useful and probably the best option for several reasons (everything is a component, no unused CSS, much cleaner than tailwind, sass/scss capabilities by default, JSX inside styles definition, it can handle JS props (usefull for interactions that CSS alone can't handle and you can place it on a different file).

I like to have 3 files in each component folder, those are, as example:

myComponent.js
myComponent.styles.js
myComponent.utils.js

Which I guess are self-explanatory (ask me if you want detail).


[*1] Meaning that you don't need to add TS to your project and waiting the extra build time, it will work, let's say, in runtime, while you are developing instead. 0 time wasted and same type-checking than adding the huge TS lib to your project, isn't it amazing? 🤗

[*2]Most part of the logic should be in the backend. You should only handle interaction logic in the frontend, which makes easier to scope everything where it belongs so you can swap your backend or frontend without the hell of a code with heavy coupling.
It may seem that React will exist forever but people that is migrating from JQuery-UI maybe thank the same back those days 😂

[*3] By robustness meaning that you don't need to update your project each time a lib changes it's way-to-work, relying on language API (if it suits the project needs) is so, usually better.
By secure I mean that the probability that a third party deals to a security hole is higher than using the language API itself as a general rule of thumb.
I find faster to build/deploy as self-explanatory.

Collapse
 
haaxor1689 profile image
Maroš Beťko

Learning JS properly as you said and using Ts are by no means exclusive. You write to not use Ts but then immediately suggest to use ts-check pragma. While JSDoc is powerful tool, it shouldn't be used for typechecking your code, this again is thanks to TS extracting some info from your js documentation and by no means is a solid solution for larger projects.

Also about the data fetching libraries, you clearly didn't understood what they are for. React Query isn't there to replace fetch, it uses fetch to smartly and effectively work with async data.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited
Learning JS properly as you said and using Ts are by no means exclusive.

Sure it's not, I totally agree with this. I'm not editing the previous comment to keep things as they are, I should have been avoided the word "instead" at the end or communicate it on a different manner.

You write to not use Ts but then immediately suggest to use ts-check pragma.

I said that that if you want type-checking for any reason you can simply use this instead loading the entire TS as dependency in your project.

While JSDoc is powerful tool, it shouldn't be used for typechecking your code

¿Why not? JSDoc annotations live in comments, rather than directly in syntax, which I prefer but it's totally opinionated, you can choose one or another or none of both and it will be Ok if it suits your project needs.

this again is thanks to TS extracting some info from your js documentation and by no means is a solid solution for larger projects. Also about the data fetching libraries, you clearly didn't understood what they are for. React Query isn't there to replace fetch, it uses fetch to smartly and effectively work with async data.

This post is about good practices, so my comment is targeted to avoid opinionated stuff into good practices rather than discussing the usefulness of a given library.

We need to take apart what we "like" or what we found "useful" or what it worked "well for us in a specific use-case" so we can differentiate what are actual good practices than what are our preferences.

Do you prefer TS for some reasons? It's OK, go for it. Do you prefer to avoid using TS? It's equally good, go ahead. The same applies to fetching libraries.

Is strong typing things good? Sure it can avoid runtime errors, but it's not the only way to reach the same.

Saying that TS or fetching libraries are "good practices" is just so out of the reality that it simply can't fit. By all means, best practices are not language specific, just for that reason adding TS or a given lib is just an opinion (to which you can agree or not, but that's not the point of discussion).

Thread Thread
 
brense profile image
Rense Bakker

The reason we can tell what is a best practice is because we can see Typescript winning massively in popularity. We can see code written in Typescript failing much less frequently in production. We can see junior developers learning much faster how to write proper testable code.

Javascript spawned all these cowboy devs who started their own religion of writing proper code in Javascript (with 1 follower per religion), because the language itself has no opinion about what proper code is. There's a reason why all those "tools" you mentioned earlier exist now. Typescript is the natural evolution of some of those tools.

And yes, best practices can definitely be language specific. For example its best practice to use proper indentation in Typescript, in Python your code simply doesnt work and in other languages like cobal appearantly its best practice to not use indentation at all :P

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

That script is looking good but then you realize that JSDoc exists sice the 90s and that Doc capabilities are in almost every language...
I'm always at the JS side here in Dev community for a good reason. You defend TS with personal preferences rather than analysing properly the project needs.

I would prefer, to use TS in a Node backend which provides business logic but I find a non-sense adding it to the auth service (which will simply validate tokens from Google IDP for example).
I would probably avoid adding TS to a microservice that aggregates 2 third parties (you'll not get strong typing in runtime anyway so all you need to do is a prop map, and the result will be the same with vanilla JS than with TS, probably you'll add some validation library like Joi).

So no, we can't tell that using TS is a good practice, as every tech, it's good where it has sense. I'm tired to tell that there's no single tech to rule them all, and that we are working in a Science field. Adding personal preferences to your projects harm them on a way or another. I.e.
Not using TS when you should makes the product less robust.
Adding TS where you shouldn't expands your dev times for any reason.

C'mon, you can find proper non-opinionated definitions on best practices around the Internet.

Thread Thread
 
brense profile image
Rense Bakker • Edited

We were talking about React projects here. The Wikipedia page you link holds the definition of what best practices are. It does not keep an extensive list of all best practices used in the field, nor does it claim that anything not mentioned there is not a best practice.

"Coding best practices are a set of informal rules that the software development community employs to help improve software quality"

Improve software quality is what Typescript does, especially for React projects.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

This is an opinion, not a definition. Assuming that TS improve the software quality we can extrapolate that and say that Java (which includes the "strong sides" of TS by default) improve software quality and by extension all Java code has quality.

I worked 4 years using Java and I'm confident to say that this is not true.
Strong typing and so gives your code robustness which is absolutelly not the same than quality.

SOLID principles, design patterns (when they fit), documentation, testing, good naming conventions etc are things that improve software quality and some of them also add robustness.

Moreover TS shines when dealing with complex logic (as it adds robustness, either be with ts-check pragma or adding TS into your project) and you are not supposed to add complex logic in the frontent (it adds coupling and makes hard to migrate to a new frontend technology when react dies in a future) so I would rather say "specially in complex Node services" instead "specially in React", where you will get interaction logic only.

Thread Thread
 
brense profile image
Rense Bakker

I'm just going to ignore the absurd comparison with Java (The way in which Typescript deals with type safety is completely different from Java) and only reply to the more sensible part of your reply. SOLID is great, especially the parts that carry over to functional programming, yes design patterns, documentation, testing, good naming conventions, completely agree and there is zero reason why you cant do this in Typescript. The second part of your reply... Thats not the point of Typescript, the point of Typescript is that I can trust that when a team member writes some code or some intern writes some code and after a while I have to make changes to that code, I'm not confronted with a big mess of different styles of coding and variable declarations that are changed all over the place, holding values that I cannot predict. Typescript has made my life a lot easier and less filled with fixing nonsense bugs and typos. If you want to dismiss that as a personal opinion, go right ahead lol

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

I know that difference between Java and TS, but making the assumption that TS always deals to a quality code is absurd in the same way.

I agree with you in most parts to be honest, it's just that, to add objectivity into the equation, you can get the same with JSDoc definitions and using ts-check pragma instead loading the entire TS as project dependency.

Again, I don't mean to say that using TS is bad or any other thing, it's just that fanboying a tech makes you think that it's good for everyone in every situation and for each use-case that you have to face and that's simply not true.

To get the correct tech stack for a project you need to check different metrics; the specific use-case/s to cover, the future planning, the human resources available (quantity, seniority, previous experience), the overall experience of the team on each tech stack and so on.

Then, in a perfect world, all projects would have unit tests, e2e tests, good documentation, an up-to-date confluence with the acceptance criteria to review before refactoring something and a huge etc of things.

The real life for most projects is that business needs go first and it usually means "We need this done by [DEADLINE]" which leads inevitably to cut time in some of those things, so you need to choose.
Do you prefer extended dev time by using TS? Do you prefer to have unit tests? Do you enforce documentation?

There are different approaches that lead you to -almost- the same result, for example, if you take the approach of "everything is a component" in React, using propTypes is more flexible than strong typing your code, plus you add explicit definition of what is optional and what is required and so on, just to add an incomplete example.

It's not all black or white, it's not a "good practice", it's not a use TS or not use TS, it's a tech stack definition and it will be the correct one or not depending on those different metrics I add before and it may be a question on "how much TS do you want to imply in your project" and for which reason.

I'm adding 2 use cases from some months ago just to illustrate.

Real-time chat app with Node + React using Websockets

and persisting data in a MySQL database.

It was meant to be integrated inside a different webapp as stand-alone webapp.

React code is just an entry point to authenticate the user in context + the chat component (divided in two, the place where you type and the place where you read). Around 200 lines of code as much, you just deal with strings.

Node backend it's just the event emmiter, the persister (to store) and the reader (to get previous chat history), around 150 lines of code as much if you put the files together, again you just deal with strings.

There is no logical benefit of adding TS. You can fantasize with things like "if the project grows then...", "if bla bla... then" but the reallity is that this project will remain as is for it's entire life.


Service to handle bussiness logic

Between a React frontend and a headless CMS.

  • Forecast of project minimum support time? 5 Years.
  • Forecast for project growth? Big.
  • Does this use-case fit for TS in the Node service? Sure it does! I wouldn't recommend doing it without TS.
  • Does this use-case needs to have TS in the React part (y/n)? No, it does not.
  • Are we about to using TS here? Let's see the rest of the metrics, oh, the team got experience with JS but few in TS, we are about to add only interaction logic to the frontend so we can cover the robustness with JSDoc, TS-pragma and prop-types.

That's the objective truth about this regarding the metrics, but you seem like the one dev that cries at the back saying "but we HAVE TO use TS" (usually just because he's used to it and as a personal preference) and can't give any good reason for it to Architecture and Management people.

Thread Thread
 
longebane profile image
Long Dao • Edited

I don't know man, jsdocs, proptypes.. I see that too often as wishful thinking, because only one dogmatic guy of the entire team is keeping those up to date and consistent. Strict ts is more friendly for a mixed-level team because it puts less burden on Sr engineers during code review to always double check if "best practices" are adhered to (and reduces friction due to being code-blocked during the code feedback stage)

A codebase with outdated jsdoc/proptypes/etc brings a tear to my eye.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

You can use a linter to ensure those are present and validate ts-pragma as well, so you can't have an invalid definition

Thread Thread
 
brense profile image
Rense Bakker

Or you can not use all those things and just use one thing: Typescript :P

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

Sure it's an option that I follow depending on the use-case :)

Collapse
 
nicoladj77 profile image
Nicola Peluchetti

I really don't agree with you on this one, writing jsDocs or PHPDocs is one of the biggest waste of time ever, in my opinion: when was the last time you read a useful comment in a jsDoc?
I think that all jsDocs do is either:

  • make your files bigger for no parctical reason
  • make you think twice before you write a function, because you are forced to put in the boilerplate comment To give some context, I am talking about agency level codebases, so for important clients but that will be used by not more than 40 developers in 5 years: I understand that WordPress needs proper documentation, it's a library, but code you write for clients needs to be clear, not to have boilerplate comments, so basically it just needs commenting when you are doing something bad. For me the golden standard should be to type hint everything, in PHP you get it for free, in JS you need typescript.
Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

It's the first time in my life that I read something that "documenting your code is bad". You guys surprise me more every day 😆

Of course you need to think before writing a function, and as best practice you'll probably re-write this function splitting it in two or three because you forgot to apply the S on SOLID (single responsibility).

Remember that the first half of a developer job is to make the code work, the other half is to make it maintainable, readable, documented and tested.

Thread Thread
 
brense profile image
Rense Bakker

Yes, good code doesnt need documentation. Read Clean Code by Martin Robert. If your code needs documentation, its not easy to read/understand and is therefor bad code.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

That describes a concept not a reality. The maximum perfection in a team would be to understand each other's code to the perfection, all with the same level/amount of knowledge and working together for years.

In a real life project working in a team, usually you'll get different expertise levels, there's people rotation and you'll have to maintain the code for long time.

I can code a function composition inside my code and the junior will come and say "bruh, WTF". If we can take the time and resources to raise the seniority to a given "minimum" level for the project it would be fine (I guess, never saw it IRL)..

But even that, there's no single pattern or best practice other than documenting your code that can release you from the burden of remembering everything in a future stage.

Let's say you work 5 years in a project and someone new comes to it. Understanding what it does in the big picture is a matter of reading function names (if the codebase is good).

i.e. getUserId(user) will obvious get the user ID, probably from a user object. If you use TS you can see that user ID is a number and that user param is a user object. That's all the knowledge you can take from the declarative part of the code.
Usually you'll see something like:

const getNewNickname = (user) => generateNickname(randomize(capitalize(concatNameAndSurname(user))));
Enter fullscreen mode Exit fullscreen mode

Adding doc would be something like:

/**
   * Generates a new random nickname based on the current user name and surname
   * @param {Object<User>} user 
   * @returns {string} Nickname
   */
  const getNewNickname = (user) => capitalize(generateNickname(randomize(concatNameAndSurname(user))));
Enter fullscreen mode Exit fullscreen mode

Isn't that beautiful?
Let's get a step further adding function composition:

const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);
const getNewNickname = compose(capitalize, generateNickname, randomize, concatNameAndSurname, user);
Enter fullscreen mode Exit fullscreen mode

Can everyone predict what will that do exactly without having to think for some seconds? I can honestly say that a quarter of my current team wouldn't.

Let's add Doc to it:

 /**
 * Executes a sort of functions from right to left, passing the output to the next function
 * @param  {...any} fns functions
 * @returns {any} execution result
 */
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);

/**
 * Generates a new random nickname based on the current user name and surname
 * @type {string} nickname
 */
const getNewNickname = compose(capitalize, generateNickname, randomize, concatNameAndSurname, user);
Enter fullscreen mode Exit fullscreen mode

Isn't it better, faster and quickier?

So i've lost less than a minute to avoid others in the team not loosing a couple of them in a future.

And we just saw the declarative part, on the imperative part scoped in helpers and functions could be harder to catch up things quickly as they'll have procedimental code with conditions, loops and so on, and here is where doc shines most.

Thread Thread
 
brense profile image
Rense Bakker

Clean code is a concept that was born from a shit ton of practical experience... Uncle Bob has what? 40 years experience in the field? And it's not like he's alone, or that the concept of clean code wasnt adopted and put in to practice for the past 14 years...

You can claim all you want... No junior developer is going to understand what your JSDoc comments really mean, or know how to use it properly themselves. With typescript they have no choice... But more importantly, they don't have to care at all because Typescript does type inference automatically and it will just tell them what they're doing wrong if they try to assign the wrong type to a variable or function :B no understanding JSDoc required and a bunch of other tools and VS code plugins that you need... I have the complete opposite experience than what you claim... The junior devs I have encountered found Typescript very easy to learn and use.

JSDoc, like PHPDoc is archaic, from a century before we had modern type systems and stems from Javadoc which is even more archaic...

I agree, the example you give is a horrible obfuscation of logic that you should not do at all. If someone changes what is returned by one of the functions in your composition and doesnt update the JSDoc for it, Javascript is going to crash in runtime and I would love for you to let a junior dev try to find and fix that runtime error with your function composition. If you work with 10 devs, I guarantee atleast 5 of them are not going to care much about updating code comments when they're writing code. I much prefer devs who try to write clean code, instead of trying to explain their mess with JSDoc.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

So, to recap, you'll use TS always instead JS, isn't it?

Thread Thread
 
brense profile image
Rense Bakker

I am using JS since TS is just superset of JS, but yes, for typesafety and to avoid a million runtime errors everytime you forget something, I will use TS until something better comes along.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

So you took a decision based on other decisions you made before (learning TS and ignoring vanilla JS as much as possible).

“If the only tool you have is a hammer, you tend to see everything as a nail.”

Taking in mind that TS does not check types at runtime and also knowing the context where you are working on (in this case React), where runtime errors are mostly due to some API error (the backend guy forgot to add some property, the swagger says that in this contract you should get a string but instead you got an integer and so on) you'll face exactly the same amount of runtime errors either using one thing or another but hey, those are your projects, not mine.

Thread Thread
 
brense profile image
Rense Bakker

The amount of assumptions you make about my work experience with other languages shows how narrow minded you really are.

There are ways to avoid runtime errors from API changes, but i'm sure you have a very strong opinion about those tools as well and frankly I have come to the conclusion that its utterly pointless to discuss these things with you.

Collapse
 
brense profile image
Rense Bakker

Or instead of learning all your "tools in js", you could just learn Typescript properly because it has all those things built in :)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Following the same logic you could just learn Kotlin and then transpile it into JS, as it has even more capabilities than TypeScript and you can also code native Android Applications, web services, gateways and so on with it running over the JVM which is better for complex computational tasks than JS is over Node.

Maybe you could just code in Rust and compile it to WebAssembly .

There's plenty of options if you really want to avoid digging deep in a programming language!
If you prefer to just scratch the surface, then you get tired for any reason and need a different challenge, go for it.

I'm pretty sure that more than half the people reading this didn't even know about JSDoc, I even got comments about how bad documenting your code is 😆

Thread Thread
 
brense profile image
Rense Bakker

If your code needs documentation to be understandable, its bad. Highly recommend reading Clean Code by Robert Martin. Sure you could invent your own language and be a solo dev cowboy and nobody will understand what the hell you're doing, or you could just use whats already there and appearantly works for a large chunck of the dev population. JSDoc doesnt work, thats why nobody uses it except a very small group of preachers. And yes, I highly recommend that you dig a little deeper into Typescript until you understand that its not comparable to Java.

Collapse
 
spock123 profile image
Lars Rye Jeppesen

Wow I cannot believe you wrote this.

Collapse
 
mehyam profile image
MehYam

Major error in your post: you centered your "personal opinion" header instead of aligning it to the top.

Collapse
 
srmagura profile image
Sam Magura

@mehyam 😂😂😂

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

It's an introduction 😆

Collapse
 
codewander profile image
codewander

In terms of compile time, ReScript is lightning fast, if you ever want to dabble in it. I am not suggesting switching to it, it's just an interesting thing to try if compile speed is something that bothers you about typescript.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Just pointing out some things there 😆 Thanks for your comment, I'll check it out :)

Collapse
 
ohscee profile image
Vincent S.C.

I’m definitely no js purest (in fact I simply adore types and interfaces and abstract classes) but I think you’re absolutely right with your statements, especially concerning React.

The framework itself isn’t really ideal for especially complicated projects to begin with, and TS is just another build layer on top that isn’t really necessary if you’re comfortable with JSDoc.

I’m ALWAYS astonished that React documents don’t seem to bring up styled-components at all. I loathe how inflexible css-in-js is. It’s absolutely nonsensical if you’re building anything larger than a personal website or a series of detached components. Having a SASS or LESS is such a time saver, and really optimizes how you can use css

Collapse
 
pelv profile image
Alex Benfaremo • Edited

Talking about "// @ts-check" i don't know if you use the comment for some means but actually there is an options in the settings that enabled it by default for all javascript file.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Adding the TS pragma makes it work on any team member VSCode without having to check that they have it enabled in their IDE, so it's a convenience while working in a team.

Thread Thread
 
pelv profile image
Alex Benfaremo

make sense :)