Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Side Effects pt.2 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 17, 2017
Merged

Conversation

kbariotis
Copy link
Contributor

Regarding #108. Is the example clear?

Copy link
Owner

@ryanmcdermott ryanmcdermott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Kostas! Overall this is a really great start! I left some comments, I think we will have a really great part 2 that will help a lot of people.

@@ -550,6 +550,32 @@ console.log(newName); // ['Ryan', 'McDermott'];
```
**[⬆ back to top](#table-of-contents)**

### Avoid Side Effects pt.2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should read (part 2). The example before should say (part 1)

@@ -550,6 +550,32 @@ console.log(newName); // ['Ryan', 'McDermott'];
```
**[⬆ back to top](#table-of-contents)**

### Avoid Side Effects pt.2
Side effects could also occur from inside a function. In Javascript, function arguments
are always passed by value except when they(functions) are passed reference values such as
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reword this sentence. We are saying here "JavaScript always does this, except when it doesn't." Many guides word it this way, but it's kind of a pedantic way of saying it. Yes technically that's true, you are passing a value in every case, but if your value is a pointer then it's a reference. Anyway, we can just say "In JavaScript, primitives are passed by value and objects are passed by reference."

### Avoid Side Effects pt.2
Side effects could also occur from inside a function. In Javascript, function arguments
are always passed by value except when they(functions) are passed reference values such as
objects and arrays. In that case, we should be carefull not to change any of these
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

carefull -> careful

are always passed by value except when they(functions) are passed reference values such as
objects and arrays. In that case, we should be carefull not to change any of these
argument's properties. A possible solution would be to always clone the variable,
edit it and return the clone.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to mention, as a caveat, that there are some times where people want to modify the passed in object. Also, we might want to mention the performance implications of cloning large objects. All in all, I agree with the sentiment though that intentional side effects can be good, but oftentimes it's better to avoid them altogether if you can.

```

**Good:**
```javascript
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start. I think we can do something that's not Flux/Redux specific, as these examples can go over the heads of a lot of beginners. Simple things like Planes, Trains, and Automobiles are understood by everyone! Let's think of an example along those lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could only think of an e-commerce kind of example. I believe it's very common among beginners. :)

cart.push({ item: item, date: Date.now() });

return cart;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semicolon)

c.push({ item: item, date: Date.now() });

return c;
}
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Jan 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here semicolon.

**Bad:**
```javascript
const addItemToCart = function (cart, item) {
cart.push({ item: item, date: Date.now() });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just { item, date:... ?

const addItemToCart = function (cart, item) {
const c = Object.assign({}, cart);

c.push({ item: item, date: Date.now() });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just { item, date:... ?

@ryanmcdermott ryanmcdermott merged commit 5e581f9 into ryanmcdermott:master Jan 17, 2017
@ryanmcdermott
Copy link
Owner

Thanks for all your help, this is great!

@kbariotis
Copy link
Contributor Author

@ryanmcdermott you are obviously a much better writer than me! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants