

How to avoid the assignment statement - loup-vaillant
http://www.loup-vaillant.fr/tutorials/avoid-assignment

======
cperciva
A C programmer's reparsing:

"Initialize right away" --> "Make it hard to figure out what your variables
are by mixing up code with their declarations."

"Construct new values" --> "Manually do what the first pass of the compiler
already does. Except don't use the const keyword."

"Make functions, not procedures" --> "Don't check for errors. Don't even make
it _possible_ to check for errors."

"Make your objects immutable" --> "Let's see how much time we can spend in
malloc!"

"Use purely functional data structures" --> "Arrays and hash tables are often
the optimally performant data structures... but you shouldn't use them."

"When you can't help it" --> "If all your other attempts to sabotage your code
fail to provide the required performance degradation, you can always slow your
program down by copying data around unnecessarily."

In all seriousness, just like the famous "GOTO considered harmful", the
author's "assignment statement is harmful" is an assertion which is valid in
some situations -- quite possibly most or all of the situations he personally
has to deal with -- but most definitely not valid in all circumstances.

~~~
btmorex
I agree with most of what you said, but:

"Initialize right away" --> "Make it hard to figure out what your variables
are by mixing up code with their declarations."

No. Putting all declarations at the top of a function makes it hard to figure
out. If you can use C99, you should keep declarations and initializations
together and hopefully as close to where they are actually used as possible.

One thing I'd add about the article: naming a function "move" and then having
it return the sum of two vectors is bizarre and worse than the original
implementation. At least, move as part of the Point class _actually moved the
point_.

~~~
cperciva
In a 50 line function with 5 variables, I find it much easier to look at 5
lines of variable declarations to figure out how I declared a variable than to
scan through up to 50 lines of code.

~~~
Qz
Stop writing 50 line functions.

------
zweinz
Seems to me like you should just be coding in a functional language. Coding in
Scheme might be easier than turning C into Scheme.

~~~
loup-vaillant
I agree. By the few code that I write[1] outside of my day job is not in C.
Also, I didn't meant C, but any language with garbage collection. I used C
syntax mainly to reach mainstream imperative programers.

[1]: <http://www.loup-vaillant.fr/projects/ussm/>

------
10ren
A useful summary (eg. I found my experimental code from last year a lot easier
to understand when it used functions rather than procedures) - though I
couldn't help laughing when he started saying "mistakes", "correct" and
"incorrect".

~~~
loup-vaillant
Ah… Do you think I should change my wording? Because if it made you laugh, it
could also discredit my point in the eyes of those who don't agree with me in
the first place.

I'd like to correct that, if possible.

~~~
10ren
The wording comes across as dogmatic; yet the article _isn't_ actually
dogmatic - it sincerely acknowledges cases where the functional approach isn't
suitable (even including the really cool algorithm of quicksort - BTW you
might mention games as another one where mutable state is convenient). Showing
respect for alternatives (where due) makes an article seem objective,
impartial, intellectual and truth-seeking (rather than pushing an agenda; or
trying to sell something). It would probably be beneficial if the wording
reflected your intent.

I did find it distracting, because (to be frank) people don't like to be told
what they should be doing; laughter is a way to defuse the affront. Another
approach is to appeal to those (few) people who are keen to learn about
techniques that will help them. And I really think that that's what you were
going for. Changing the wording would help achieve that aim, in my opinion.

So... yes.

btw: I really liked your explanation of linked lists being cheap to modify;
and the article in general is very well done and communicates very effectively
- I'd say it's of textbook standard.

