News for hackers. Stuff that matters.
Re:How to avoid the assignment statement by btmorex on Monday July 12, 2010 @04:08AM (#1507169)
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.

Re:How to avoid the assignment statement by cperciva on Monday July 12, 2010 @04:45AM (#1507236)
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.
Re:How to avoid the assignment statement by Qz on Monday July 12, 2010 @05:01AM (#1507265)
Stop writing 50 line functions.
Re:How to avoid the assignment statement by loup-vaillant on Monday July 12, 2010 @04:56AM (#1507259)
So, declaring variables at the top of the function makes it easy to find them. Now how do you find the point of initialization?

    declare x    |
    ...          |  ...
    initialize x |  declare & initialize x
    ...          |  ...
    use x        |  use x
In both cases, you will have to scan for the whole function, or hit the "search" hotkey. Or maybe sometimes you just need to know when `x` were declared, but not what value it holds?
Re:How to avoid the assignment statement by mahmud on Monday July 12, 2010 @04:36AM (#1507214)
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.

This makes sense in MVC where the controller is responsible for updating point coordinates and the view is responsible the actual rendering of the points on the display.

Also, in most graphic systems, the screen refresh is done by a single call made from within the interaction loop or by a designated callback. That's because all display updates are made to a back buffer, which then gets written to the framebuffer at a regular clock-driven interval.

Re:How to avoid the assignment statement by loup-vaillant on Monday July 12, 2010 @04:15AM (#1507183)
> naming a function "move" and then having it return the sum of two vectors is bizarre and worse than the original implementation.

I kept the name for the sake of consistency. I agree that's a mistake. The problem is that a `Point` should be moved with a `Vector`, and not another point. But introducing a `Vector` class would have lengthen my tutorial. Maybe I should use two `floats` instead of a `Point`.

Re:How to avoid the assignment statement by th on Monday July 12, 2010 @11:12AM (#1508095)
That example bothered me as well mostly because when the move function is placed outside of the class it is now performing a different action. The new function no longer really moves a point so much as it adds two points (regardless of the point/vector issue).

I think many of the naming conventions commonly used in OOP for mutable objects are not as appropriate for their immutable counterparts.

Re:How to avoid the assignment statement by tkahn6 on Monday July 12, 2010 @04:09AM (#1507170)
Agree.

When I was going through K&R I was always thinking to myself, "did they just know all of the variables they would need before hand?".

Re:How to avoid the assignment statement by mfukar on Monday July 12, 2010 @04:41AM (#1507221)
Maybe not. Probably not, in many cases.

Hey, have you seen function prologues in assembly? How does it know all the amount of stack space it's going to need beforehand?