Talk:Sokoban: Difference between revisions

Content added Content deleted
m (→‎The Long C Version: strike wrong statement)
Line 33: Line 33:


:2) For a library routine, putting <code>const</code> in front of a var gives some hint about the interface; init code may put <code>const</code> in read-only memory. Outside those two, <code>const</code> does absolutely nothing for the code. For routines used only locally, I don't really want to bother. <s>Plus, <code>unqueue_move(state_t *)</code> can decl a const pointer, but not a pointer to const: it modifies data inside the struct.</s>(nvm: I was think of <code>queue_move()</code> --[[User:Ledrug|Ledrug]] 20:29, 14 May 2012 (UTC)
:2) For a library routine, putting <code>const</code> in front of a var gives some hint about the interface; init code may put <code>const</code> in read-only memory. Outside those two, <code>const</code> does absolutely nothing for the code. For routines used only locally, I don't really want to bother. <s>Plus, <code>unqueue_move(state_t *)</code> can decl a const pointer, but not a pointer to const: it modifies data inside the struct.</s>(nvm: I was think of <code>queue_move()</code> --[[User:Ledrug|Ledrug]] 20:29, 14 May 2012 (UTC)

:: Right, using assert() as allocation guards is a bad idea, maybe it's better to remove those asserts. You can't assume no one will compile your code with NDEBUG defined. Putting non-pure code inside assert() is often a bug waiting to happen.
:: Regarding const, it's also a contract between the programmer and the compiler, it says that something hopefully will not change (in D language this is enforced much better than C), this makes the code simpler to understand too. Every variable that doesn't need to change must be const/immutable, if practically possible: http://blog.knatten.org/2011/11/11/disempower-every-variable/ --[[User:Bearophile|Bearophile]] 00:06, 15 May 2012 (UTC)