3+ Ways to Write Clean Code in JavaScript

preview_player
Показать описание
View the Starting Code:

Coding is an art, so why not make it as clean as possible? In this video I go over 3 techniques I like to implement to help make my JavaScript code easier to read and extend upon.

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

#dcode #javascript
Рекомендации по теме
Комментарии
Автор

OK, I did mess up the "if statement" regarding the hours and light/dark mode. Sorry about that. It probably should be an OR instead.

dcode-software
Автор

I would have written something like:

function getThemeColors() {
const currentHour = new Date.getHours();
const darkTheme = { textColor: "#fff", backgroundColor:
const lightTheme = { textColor: backgroundColor: "#fff"};

return (currentHour > 20 && currentHour < 7) ? darkTheme : lightTheme;
}

saschamajewsky
Автор

Amazing stuff Dom! We need more of this sheesh 🔥 Thanks man for giving out valuable content as always🥂

eip
Автор

I try to avoid concatenate.
const capitalize = function(name, locale = 'en') {
return name.replace(/^./,
}

mofmse
Автор

Thought provoking as usual - thank you.
But I do disagree with one of your solutions. I was taught that functions should have just one entry point and one exit point. Why? Imagine say you have a function that looks in a file for something. On entry it opens the file, and on exit it closes it again. If you exit in the middle you could forget to close it and cause a resource leak.

In general a function on entry has some setup to do, and on exit knocks it down again. Returning form the middle is just asking for trouble. A maintainer may add something towards the end of the function, not realizing that it can exit earlier and not run the new code.

castletown
Автор

Sorry for the question, but why at 13:15, there is not need to write
names.map(name => capitalize(name));
is it a javascript shortcut?
thanks.

magicorpse
Автор

Dcode tenho uma dúvida. No appgyver Com a api da xano eu criei um aplicativo e adicionei dois métodos de login, um para cliente e outro para empresas, com isso eu criei duas tabelas de base de dados na database, uma para a conta pessoal e outra para conta da empresa. Até aí tudo bem. Tudo da parte de cadastro funcionou. Mas eu queria criar uma data base média para fazer upload de imagem do aplicativo, Da foto de perfil de usuário. MAS ISSO PARA CADA USUÁRIO. para as duas contas tanto de empresa quanto de usuário eu queria que eles fizessem upload de imagem na mesma database, de novo até aí tudo bem. O PROBLEMA É que quando eu quero que ele busque a media_id do id da imagem na database ele precisa do token de autenticação, o problema é que toda a vez quando tento, dá acesso não autorizado no aplicativo. Ele não pega o token de autorização da conta empresa, Isso quando eu faço pela conta da empresa! Só pega a do cliente que foi a que eu fiz primeiro. Queria criar um aplicativo duas databases diferentes uma para empresa e uma para o cliente. Mas quando eu quero que ele busque id pra colocar na tabela da média ele da erro de autenticação access unaltorizhed.

DroidNexus
Автор

Am I wrong or should be "7 < currentHour)" (eventually "currentHour > 7)") ?

maxdevjs
Автор

“currentHour > 20 && currentHour < 7” is how we say it.
In a program, however, this will always be false.

But I see the point anyway, thank you!

andrew_ortega
Автор

Thanks for the Clean-Code tips, Dom!

{2023-10-21}, {2024-03-25}

Pareshbpatel