News for hackers. Stuff that matters.
Re:My takeaways from "Clean Code" by jcampbell1 on Tuesday May 28, 2013 @08:33PM (#5783833)
> Zero or one argument is easiest to understand and maintain.

> Have No Side Effects

Can someone explain how you use zero argument functions that doesn't have side effects? I am trying to wrap my head around these two statements.

Re:My takeaways from "Clean Code" by jasonlotito on Tuesday May 28, 2013 @08:51PM (#5783909)
Having no side effects doesn't mean it can't do something. Only that it shouldn't do anything unexpected.

    storeItem.makeAvailable();
That should only make the store item available. It shouldn't re-enable a sale that was on the storeItem when it was made unavailable.
Re:My takeaways from "Clean Code" by gruseom on Tuesday May 28, 2013 @10:16PM (#5784187)
That's a zero-argument method but not a zero-argument function. That is, if you view makeAvailable as a function, storeItem is an implicit argument to it. In fact, storeItem is probably an object with several fields, so there are arguably several implicit arguments. To my eye, the name "makeAvailable" suggests a side-effect too.

In any case, it's fine for two rules such as "avoid side effects" and "prefer zero- or single-argument functions" to be in tension with one another. I don't buy that zero-or-one idea, though. A function should have as many arguments as it makes sense for it to have.

Re:My takeaways from "Clean Code" by colomon on Tuesday May 28, 2013 @10:18PM (#5784192)
Isn't that a side effect?
Re:My takeaways from "Clean Code" by blazespin on Tuesday May 28, 2013 @10:44PM (#5784267)
Yeah, pretty much. I generally agree with this definition: http://en.wikipedia.org/wiki/Side_effect_(computer_science)

I think instead of "side effect" the author should have used the more traditional term of cohesion:

http://en.wikipedia.org/wiki/Cohesion_(computer_science)

Re:My takeaways from "Clean Code" by ruficornis on Tuesday May 28, 2013 @10:39PM (#5784260)
That is more of an effect, not a side effect, no? Something still happens, but nothing very unexcpected. If a doctor gives you medicine for an ailment, you don't want it to have many side effects, but it intended effect is still desired (theoretically). Prehaps it should effect only what it needs to in order to give you or the next step it's output, not changing some finite machine state (unless...)
Re:My takeaways from "Clean Code" by colomon on Tuesday May 28, 2013 @10:50PM (#5784286)
That's not how I've usually seen the term side effect used in CS: http://en.wikipedia.org/wiki/Side_effect_(computer_science)

In jasonlotito's example, he's definitely modifying state.

(Mind you, I don't think side effects should be banned outright or anything like that. But minimizing them is frequently a nice idea.)

Re:My takeaways from "Clean Code" by ruficornis on Tuesday May 28, 2013 @11:44PM (#5784468)
Sorry, I was wrong. I did not read the comment closely nor was I familiar with the CS definition. Thank you for the information.
Re:My takeaways from "Clean Code" by dack on Tuesday May 28, 2013 @08:46PM (#5783888)
You could have a method on a class that uses it's own fields to do some calculation. So, although it does have access to the fields on the class, it takes no arguments and has no side effects.

You could certainly argue that a class's fields are a form of input arguments to a method, and I would agree - which is why another good rule of thumb is to not have too many of those either!

Obviously, it's a balance, so none of the rules can be taken without context to the situation - they are all just smells that you should "back away from" if you're finding yourself breaking any given rule too much.

Re:My takeaways from "Clean Code" by raylu on Tuesday May 28, 2013 @11:06PM (#5784321)
And what is it doing with that calculation?
Re:My takeaways from "Clean Code" by gfunk911 on Tuesday May 28, 2013 @11:13PM (#5784351)
Returning it.
Re:My takeaways from "Clean Code" by gizmo686 on Tuesday May 28, 2013 @08:47PM (#5783893)
I think it is all suggestions, not hard rules. Also, given that the quote allows you to use up to 1 argument, it is actually trivial to do (assuming you are fine with the no side effects part). The trick is that instead of taking 2 parameters, you take 1 parameter, than generate and return a function that takes 1 parameter. Probably not what the author has in mind.

Anyway, in the context of the rest of the post, it looks like object oriented programming is being discussed, in which case I think the implicit 'self' does not count as a parameter.

Re:My takeaways from "Clean Code" by retrogradeorbit on Wednesday May 29, 2013 @02:36AM (#5784964)
Or just make the function take two arguments. And then use currying (if your language supports it). A lot of this depends on language.

I definitely agree with you saying its suggestions, not hard rules. Unfortunately the author of the page keeps saying 'always' as if it's hard and fast rules.

Understand why and when first, then use where appropriate.

Re:My takeaways from "Clean Code" by gizmo686 on Wednesday May 29, 2013 @05:37PM (#5789302)
>Just make the function take two arguments. And then use currying.

Isn't that what I said?

Re:My takeaways from "Clean Code" by jacques_chester on Tuesday May 28, 2013 @08:41PM (#5783865)
1. They're not orthogonal rules.

2. Some functions are queries that don't require an arguments. Eg. getVariable().

Re:My takeaways from "Clean Code" by jcampbell1 on Tuesday May 28, 2013 @09:06PM (#5783973)
I just find that minimizing parameters makes a mess with tons of side effects. When I see nothing but 0 and 1 parameter functions, the code is typically doing the logical equivalent of refactoring `add(a,b);` into `push(a);push(b);add();` which makes a mess when there is more than the trivial case.

jQuery.ajax is a 33 parameter function. No one complains, and it is nice that it doesn't have side effects.

Re:My takeaways from "Clean Code" by jacques_chester on Tuesday May 28, 2013 @09:14PM (#5783999)
Sure. Usually when you find a strict rule, it's a rule to be considered ceteris paribus -- when all other things are fixed in place. So while thinking about parameters, I think about minimising them and ignore other considerations. When thinking about side-effects, I think about minimising them and ignore other considerations.

It's when the rules conflict that mere rule-following ends and professional judgement begins.

Basically, as noted legal scholar Captain Barbossa observed, they're less like laws and more like guidelines.

Re:My takeaways from "Clean Code" by galaktor on Wednesday May 29, 2013 @03:45AM (#5785149)
A method that acts as a "getter" by querying some sort of state wouldn't take arguments and shouldn't have side effects.