Skip to content
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

Disagree with `Avoid Side Effects (part 2)`. Different from the actual clean-code book. #298

Open
noelzubin opened this issue Feb 10, 2020 · 2 comments

Comments

@noelzubin
Copy link

@noelzubin noelzubin commented Feb 10, 2020

Its mentioned that modifying a variable means it has a side effect; which is not true. A side effect is when a function does something that it doesn't say it does. Even in the original clean code book the author prefers modifying the owning object instead of returning it.
Modifying variables is a very common use case in all programming languages. There is no need to shallow copy an object where it is not needed.
The confusion that this might lead to bugs is covered by properly naming your variables and SRP. In fact this way of programming does to guarantee any extra correctness. You could still be passing a variable with unchanged values where it was expecting a changed one.
The issue with having multiple mutable reference cannot be solved by this point.

eg:

const posts = getPostsOfUser(user); // return posts; user untouched.
populatePostsOfUser(user) // modifies user. does not return anything.
// both of them perfectly correct.

Also in the point previous to this it is specifically mentioned that modifying variables is a side effect. which clearly is not. Modifying variables when a function is not supposed to do it is a side-effect.

@noelzubin noelzubin changed the title Disagree with `Avoid Side Effects (part 2)` Disagree with `Avoid Side Effects (part 2)`. Different from the actual clean-code book. Feb 10, 2020
@johachi
Copy link

@johachi johachi commented Feb 19, 2020

The code under Set default objects with Object.assign also break the rule that Avoid Side Effects (part 2) establish.

@jdsandifer
Copy link
Contributor

@jdsandifer jdsandifer commented Sep 22, 2020

@johachi You're correct that the code under the Good section of Set default objects with Object.assign does mutate an object, but because it mutates an object created within the function, there's no possibility that any other part of the program could be holding a reference to it. Thus, it doesn't break the rule that Avoid Side Effects (part 2) establishes.

@noelzubin It sounds like your are using "side effect" to mean the opposite of something like "main effect" - i.e. the stated purpose of a function. A function that doesn't do what it says it does has a Bad Name and one that does more than it says it does doesn't Do Only One Thing. Both are bad, but neither is what I understand Avoid Side Effects (part 2) to be about.

Here Ryan is using the concept of side effects as used in functional programming to mean something like affecting anything outside the scope of a function or taking in data from a source other than the function parameters. Common examples are I/O operations like displaying things to a screen, sending data over a network, accessing a database, reading keyboard or mouse input, etc. Modifying the properties of an object that was passed as a parameter to a function would be another example. (That change would be visible outside the scope of the function.)

In general, modern software wisdom seems to be that side effects should be avoided - as stated in Avoid Side Effects (part 2) - but you're also right that sometimes they're necessary or expedient. What program doesn't accept input from the user and display it in some way?

I don't have Clean Code nor do I have a perfect memory (I read it a while ago) so I can't comment on how close to it this idea is. However, I'm guessing Bob would approve of the concept even if some of the examples in his book don't follow it themselves.

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.