4 ms·
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 declarati
by btmorex 16y ago
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.
- tkahn6 16y agoAgree. 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?".
- mfukar 16y agoMaybe 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?
- loup-vaillant 16y ago> 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`.
- th 16y agoThat 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.
- mahmud 16y agoOne 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.
- cperciva 16y agoIn 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.
- loup-vaillant 16y agoSo, 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?
- Qz 16y agoStop writing 50 line functions.