r/javascript • • Apr 07 '20

AskJS [AskJS] Tried writing code in vanilla js but can someone suggest a correct file system, what I did is not satisfying to me

Color Tile Game

The above link takes you to a color game which I had created ila few years ago, the idea is to select the color that will be formed when three colors RED, GREEN and BLUE are mixed in certain percentage. It was made with basic html, css and js website hosted in GitHub itself.

Recently, I was super bored doing react and angular and due to lockdown I had plenty of time. So I decided to rewrite everything from scratch using ES6 and adding a few features like login for my friends so that we can store our past gameplays, also a leaderboard to keep track of who is doing better.

To my surprise, a small project like this is super easy to make without any frameworks and I enjoyed Coding something where I understood everything and there wasn't any magic happening 🤩

It took me almost 4 days(3hr/day) to complete the project. The current implementation is much more better than previous one, the codebase is readable, modular and neat. I have also worked on the UI to make is more user friendly by using flat colors.

If anyone is into front-end development I love to hear your feedback on this app.

Repo link: Github

If you like the repo, do let me know by 🌟 ing it 🤓

Note: you don't have to login, that's for my friends only ✨

0 Upvotes

16 comments sorted by

3

u/SaaSWriters Apr 08 '20

Interesting game, it would be nice if the color remained after you get it. That way you can look at it again and analyze it.

Why are you using arrow functions? You're not using this anywhere in your code - arrow functions don't help here.. If you named your functions, your code would be easier to read. Plus, it would be easier to debug as well.

Overall, it would be better if your code was easier to read. You can still organize it so that it's more explicit what each part is doing. Right now, it seems like you're more focused on making your code concise.

I will admit that concise code impresses many developers. But it's excruciating to maintain and extend. In the long term, it adds to development time and it more bugs get introduced.

Since this is a small project, this may not be obvious. For bigger projects, this is one of the things that make JavaScript hard to work with.

Let it be clear what your code is doing.

1

u/raj_chandra3 Apr 08 '20

Interesting game, it would be nice if the color remained after you get it. That way you can look at it again and analyze it.

Opened an issue, I will work on this, you can edit the issue here - #21

Why are you using arrow functions? You're not using this anywhere in your code - arrow functions don't help here.. If you named your functions, your code would be easier to read. Plus, it would be easier to debug as well.

I am not sure if I understand this that well but I will look into that.

Overall, it would be better if your code was easier to read. You can still organize it so that it's more explicit what each part is doing. Right now, it seems like you're more focused on making your code concise.

can u share some resources that can help me understand this?

1

u/SaaSWriters Apr 08 '20

Break down your question a bit so I can see which parts exactly you don’t understand.

1

u/nxsynonym Apr 09 '20

You're not using this anywhere in your code - arrow functions don't help here.

Arrown functions are fine stylistically. Theres no inherent downside to using them over "normal" functions.

0

u/SaaSWriters Apr 09 '20

What do you mean stylistically?

Imagine looking at your stack trace. Arrow functions and anonymous functions make your code harder to debug. When you name them, it’s easier to figure out what’s wrong quickly.

Plus, other people reading your code will find it easier to understand what you’re trying to do. This includes you, down the line.

I know most people don’t do it this way. That’s why people think JavaScript is hard. Too many bad practices have become common approaches.

2

u/nxsynonym Apr 09 '20

Arrow functions and anonymous functions make your code harder to debug. When you name them, it’s easier to figure out what’s wrong quickly.

Fair.

Plus, other people reading your code will find it easier to understand what you’re trying to do. This includes you, down the line.

Dont know if I agree with this. How do you mean? Easier to because of debugging?

why people think JavaScript is hard. Too many bad practices have become common approaches.

This is a little exaggerated imo.

I wouldn't call using arrow functions without "a good reason" a bad practice. It's something that I would leave up to the team or style guide or maintainer to decide, hence a stylistic choice.

Personally I find arrow functions help readability and consistency. Which imo outweighs the benefits of easy debugging.

My point is, I dont thinks it's an absolute.

-1

u/SaaSWriters Apr 09 '20 edited Apr 09 '20

a stylistic choice.

Nope, it's not. Again, that's how arrow functions are looked at by a lot of devs. But, that's not what they're for.

The most important thing to remember is that they don't have this. But with an arrow function, you can use this from up the scope chain. So, you don't have to do things like var self = this.
Depending on what you're doing it could be a downside or an upside. Better still, when you know how it works, you use for the right purpose. Functions don't all behave the same in JavaScript, a very important point to grasp.

When it comes to readability, think of having to refactor some code you wrote five months ago. You're looking for a specific block of code so you can add a feature.

Here are two simple examples. Tell me which one is easier to deal with - especially if there were many more lines of code involved.

var users = [{name: 'maciek'}, {name: 'asia'}];

var _users = users.map((user) => {

	user.url = 'http://site.dev/' + user.name;

	return user;

});

With the above function, you have to read the whole code to figure out what the arrow function does. It will also be hard to find. But, see how much easier your life becomes when you do it like below:

var users = [{name: 'maciek'}, {name: 'asia'}];

var better_users = users.map(function addURLToUser(user){

	user.url = 'http://site.dev/' + user.name;

	return user;

});

Not so stylish, doesn't impress cool devs but it can save you hours, if not days in the future.

By the way, the same thing applies to let and const. I don't why but it's become a fashion fad to use them all the time rather than var. But, again, they have their purposes and implications.

1

u/Wilesch Apr 10 '20

0

u/SaaSWriters Apr 10 '20

No they’re not. Arrow functions are anonymous. Read your own link. It also reiterates some of the points I’ve mentioned.

Assigning a function expression to a variable is not the same as naming a function.

1

u/Wilesch Apr 10 '20

https://stackoverflow.com/questions/27977525/how-do-i-write-a-named-arrow-function-in-es2015

Did you read the link?

try this for yourself

let foo = () => null  

console.log(foo.name) // "foo"  

// As for anonymous functions, they are the same in this case

[].map(function(x) { return null });
[].map(x => null);

// I do agree that naming them in a callback is useful

[].map(function getNull(x) { return null });

1

u/SaaSWriters Apr 10 '20

Here is an example that should explain what I mean a little bit better. Try this:

(function(){
"use strict"


	console.log(iHaveARealName.name); //iHaveARealName
	console.log(foo.name); //Oops..
	var foo = () => null  

	function iHaveARealName(){
		return null;
	}
})();

1

u/Wilesch Apr 10 '20

That is called hosting. It has nothing to do with arrow functions and has been around forever.

We know that if you declare your function like that then it does not get hoisted up.

That is the point.

1

u/SaaSWriters Apr 10 '20 edited Apr 10 '20

Nope, you're approaching it all wrong.

You have to look at what happens at compile time and what happens at run time. When you're coding, you have to choose your tools according to their behaviour. In my example, there is only one function declaration. Then, there is a variable assignment (var foo).

Keep in mind that functions don't get hoisted up. Yes, I know that's how things are explained to people learning JavaScript. I'm happy to spend time explaining this but it would take a while.

What matters is, some things happen at compile time - like function declarations. Actual variable assignments happen at run time (during execution). So, it looks like hoisting but it's actually something else.

0

u/SaaSWriters Apr 10 '20

if you declare your function like that

That's not a function declaration. That's a function expression being assigned to a variable.

0

u/SaaSWriters Apr 10 '20

First of all, StackOverflow.com is not a valid source.
Second, I checked the spec and yes, when you assign the function expression, the function gets a name. http://www.ecma-international.org/ecma-262/6.0/index.html#sec-assignment-operators-runtime-semantics-evaluation

They can be named as stated above. That doesn't change what they do or their purpose. Or the fact that in of and of themselves they are anonymous.

Nevertheless, they're not a stylistic choice.

0

u/[deleted] Apr 08 '20

[deleted]

1

u/raj_chandra3 Apr 08 '20

I just noticed this. I have opened an issue, you can edit it here - #20