Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Disagree with `Avoid Side Effects (part 2)`. Different from the actual clean-code book. #298
Comments
|
The code under Set default objects with Object.assign also break the rule that Avoid Side Effects (part 2) establish. |
|
@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. |
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:
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.