ããã§ã¯ãC言語でプログラミングする際の覚書ã®èª¤è¨³ãåæãã¾ãã
åèã¨ãã¦ãç§ã®ç¿»è¨³ã¯C言語プログラミングの覚え書き(改訳)ã«ããã¾ãã
What follows is ...
Ãå¾ãã¹ãã¯
âããããè¿°ã¹ãã®ã¯
"What follows" ã§ãç¶ããã®ãã¨ããæå³ã§ããããã§ã®ãç¶ããã¨ããã®ã¯ãç¾å¨ã®æç« ã«ç¶ããã¤ã¾ãã以ä¸ã«è¿°ã¹ããã¨ãã§ãã
But they've been accumulating in my head, if not on paper until now, for a long time, ...
Ãããããç§ã®æè¦ã¯é ã®ãªãã«ãã°ãããã£ããã®ãã¾ã¨ãããã®ã§ãããé·ããæç« ã¨ãã¦å ¬éãã¦ãã¾ããã§ããã
âãããããããã®ãã¨ã¯ãææ¸ã¨ãã¦æ¸ãããã¨ã¯ããã¾ããã§ããããç§ã®é ã®ä¸ã«é·ãæéãããã¦èç©ãã¦ãããã®ã§ãâ¦
"if not on paper until now" ããã¯ã"in my head" ã¨ã®å¯¾æ¯ã§ãããé ã®ä¸ã«ã¯ãã£ãããææ¸ã«ãããã¨ã¯ãªããã¨ããæå³ã§ããã¾ãã"for a long time" 㯠"they've been accumulating in my head" ã«ãããã¾ãã
I've yet to see a good essay on how to plan the whole thing,
Ãããã¾ã§ããã°ã©ã å ¨ä½ã®è¨ç»ã«é¢ãã¦ã®è¯ãæç« ã¯èªãã ãã¨ãããã¾ããã
âå ¨ä½ãè¨ç»ããæ¹æ³ã«ã¤ãã¦ã®ããã¨ãã»ã¤ãèªãã ãã¨ã¯ããã¾ãããã
"yet to" ã¯ãã¾ã â¦ãã¦ããªããã¨ããæå³ã§ããææãå®å
¨ã«éã«ãªã£ã¦ãã¾ãã
a clear program is not made any clearer by such presentation
Ãæçãªããã°ã©ã ã¯ãã®ãããªè¦ãç®ã§æããããã®ã§ã¯ãªã
âæçãªããã°ã©ã ã¯ãããªè¡¨ç¤ºããã¦ããã以ä¸æçã«ã¯ãªãã¾ããã
"not made any clearer" ã¯ãããã«æçã«ã¯ãªããªããã¨ãããã¨ã§ãã
... is more to type (or calls upon your text editor)
Ãã¿ã¤ãéï¼ãããã¯ã¨ãã£ã¿å ã§å¼ã°ããåæ°ï¼ãå¢ã
âã¿ã¤ãéãå¢ããï¼ã¾ãã¯ãã¨ãã£ã¿ã®å©ããæ±ããï¼ãã¨ã«ãªãã¾ãã
"call (up)on" ã¯ãé ¼ããã¨ããæå³ã§ãã
if you consistently use a naming convention from which np means ``node pointer'' is easily derived
Ãã©ã® np ã "node pointer" ãæå³ãã¦ããããããã«åããå½åè¦åãä¸è²«ãã¦ä½¿ã£ã¦ããã°
ânp ã "node pointer" ãæãã¦ããã¨ãããã¨ãç°¡åã«å°ãããããªå½åè¦ç´ãä¸è²«ãã¦ä½¿ã£ã¦ããã°
"from which" ã® "which" 㯠"naming convention" ãæãã¦ãã¾ãã
As in all other aspects of readable programming, consistency is important in naming.
Ãããã°ã©ã ã®å¯èªæ§ã«é¢ãã¦ããã°ãå½åã«ããã¦ä¸è²«æ§ã¯éè¦ã§ãã
âããã°ã©ã ã®å¯èªæ§ã«é¢ããã»ãã®å´é¢ã§ã®å ´åã¨åãããã«ãå½åã«ããã¦ãä¸è²«æ§ã¯éè¦ã§ãã
å
ã®ç¿»è¨³ã§ã¯ "As in all other aspects" ã訳ããã¦ãã¾ããã
I prefer minimum-length but maximum-information names
Ãç§ã¯æçã®ååã§ã¯ãªãæãæ å ±éãããååã好ã¿
âç§ã¯æçã®é·ãã§æ大ã®æ å ±éã®ããååã好ã¿
ãæçã§ãããããããæ大ã®æ
å ±éãæã¤ãã¨ããæå³ã§ãã
They jangle like bad typography.
Ãæªãå°å·ã®ããã«ç®éããªã®ã§ãã
âã²ã©ã表示ã®ä»æ¹ã¨åãããã«ç®éããªã®ã§ãã
æåã¯ãå
¨ç¯ãéã㦠"typography" ããå°å·ãã¨è¨³ããã¦ããã¨æãã®ã§ããããã®åæ®ã§ããããã
Pointers are sharp tools
Ããã¤ã³ã¿ã¯è³¢ãéå ·ã§
âãã¤ã³ã¿ã¯åãå³ã®éãéå ·ã§
æåéãã®æå³ã§ãããè³¢ããã§ã¯ããã¨ã®ã使ãæ¹ãééããã¨â¦ãã¨ã¤ãªããã¾ããã
If we want the next element's type
Ããã次ã®è¦ç´ ã®åãå¿ è¦ãªå ´åã¯
âãã次ã®è¦ç´ ã® type ãå¿ è¦ã§ããã°
åæ㧠type ãã³ã¼ãç¨ãã©ã³ãã«ãªã£ã¦ãã¾ããããæèããæ§é ä½ã®è¦ç´ åã§ãããã¨ãæããã§ãã
less effort is expended by the compiler and computer
Ãã³ã³ãã¤ã©ãã³ã³ãã¥ã¼ã¿ãå±éããå´åãæ¸ãã¾ã
âã³ã³ãã¤ã©ãã³ã³ãã¥ã¼ã¿ã®è²»ããå´åãæ¸ãã¾ã
"expend"ï¼è²»ããï¼ã "expand"ï¼å±éããï¼ã¨ééããã®ã§ãããã
which allows some helpful compile-time error checking that array indices cannot share
Ãããã§ã³ã³ãã¤ã«æã«é åã®ã¤ã³ããã¯ã¹ãé©åã§ç¡ãæ¨ã®ã¨ã©ã¼æ¤åºãå¯è½ã«ãªãã¾ãã
âé åã®æ·»åã¨éã£ã¦ã³ã³ãã¤ã«æã®ä¾¿å©ãªã¨ã©ã¼æ¤åºãå©ç¨ã§ãã¾ãã
"array indices cannot share" ã® "share" ã¯ããåããæã¤ãã¨ããæå³ã§ããé
åã®æ·»åã§ã¯ã¨ã©ã¼æ¤åºãã§ããªãã¨ãããã¨ã§ããæå³ãåãã¦ããããã¤ãã¤ã¾åããã«ãªã£ã¦ãã¾ãã
As a rule
Ãã«ã¼ã«ã¨ãã¦
âã ãããã®å ´å
çèªã§ãã
expressions that evaluate to elements of a data structure
Ããã¼ã¿æ§é ã®è¦ç´ ãè©ä¾¡ãããããªâ¦ãã¼ã¿æ§é
âãã¼ã¿æ§é ã®è¦ç´ ã¨ãã¦è©ä¾¡ãããå¼
ããã§ã® "evaluate to" ã¯ããè©ä¾¡ããããã¨ã«ãã£ã¦â¦ã«ãªããã¨ããæå³ã§ãã
Consider what ... would look like using a compound expression for p
Ãp ã®è¤åçãªä½¿ãæ¹ããã¦ãããã®ã³ã¼ããä½ããã¦ããããèãã¦ã¿ã¾ããã
âãã p ã®ä»£ããã«è¤åå¼ã使ã£ã¦ãããã©ããªè¦ãç®ã«ãªããèãã¦ã¿ã¦ãã ãã
ãp ã®ä»£ããã«è¤åå¼ï¼ä¾ãã°ãnode[i] ãªã©ï¼ã使ã£ããã´ãã£ã´ãã£ããè¦ãç®ã«ãªããã¨ãããã¨ã§ããå
ã®è¨³ã§ã¯æå³ä¸æã§ãã
Sometimes it's worth a temporary variable (here p) or a macro to distill the calculation.
Ãæã«ã¯ä¸æå¤æ°ï¼ãã®å ´å㯠p ï¼ã使ç¨ããããè¨ç®ã®æ¬è³ªãæãåºããã¯ãã使ç¨ãã価å¤ãããã¾ãã
âè¨ç®ã®æ¬è³ªãæãåºãã«ã¯ãæã«ã¯ä¸æå¤æ°ï¼ããã§ã¯ pï¼ããã¯ãã使ããã¨ãå½¹ã«ç«ã¡ã¾ãã
ãä¸æå¤æ°ãã¨ããã¯ããã¯ä¸¦åã§ãã
Procedure names should reflect what they do; function names should reflect what they return.
Ãããã·ã¼ã¸ã£åã¯ãããä½ãããããåæ ãã¹ãã§ããã¤ã¾ãé¢æ°åã¯ãããä½ãè¿ãããåæ ãã¹ãã§ãã
âããã·ã¼ã¸ã£åã¯ããããä½ããããã表ãã¦ããã¹ãã§ããé¢æ°åã¯ããããä½ãè¿ããã表ãã¦ããã¹ãã§ãã
åæã«ãªããã¤ã¾ãããããããã«ãå®éã«ã¯ãªãè«ççé¢ä¿ããããã®ããã«è¦ãã¦ãã¾ãã¾ãã
A delicate matter, requiring taste and judgement.
Ãæ éã«ãçµé¨ã¨å¤æããã£ã¦æ¸ãå¿ è¦ãããã¾ãã
âããã¯ã»ã³ã¹ã¨å¤æåãå¿ è¦ã¨ãªãé£ããåé¡ã§ãã
ã³ã¡ã³ãã®ãæ¸ãæ¹ãã§ã¯ãªãããã³ã¡ã³ããæ¸ããã¨ï¼æ¸ããã©ãããå«ãï¼ãå
¨ä½ã«å¯¾ãããã®ã§ãã
a symbol table might be implemented ...
Ãã·ã³ãã«ãã¼ãã«ã¯â¦ã¨ãã¦å®è£ ããã¦ããã§ããã
âã·ã³ãã«ãã¼ãã«ã¯â¦ã¨ãã¦å®è£ ãããããããã¾ãã
"might" ã¯ããªãå¼±ãæå³ã§ãã
Algorithms, or details of algorithms, can often be encoded compactly, efficiently and expressively as data rather than, say, as lots of if statements.
Ãã¢ã«ã´ãªãºã ãã¤ã¾ãã¢ã«ã´ãªãºã ã®ç´°ããªé¨åã¯ããã°ãã°ãã¼ã¿ã¨ããç°¡æ½ã§ãå¹ççã§ã表ç¾è±ããªå½¢ã«è¨å·åããã¾ããããã¯ããã¨ãã°ãå¤ãã®ifæã¨ããå½¢ã§ã¯ããã¾ããã
âã¢ã«ã´ãªãºã ããã¢ã«ã´ãªãºã ã®ç´°ããã¨ããã¯ãããããã® if æã®ãããªãã®ã§æ¸ããããããã¼ã¿ã¨ãã¦æ¸ããã»ãããå¹çããå¼·åã«è¨å·åãããã¨ãã§ãããã¨ãããããã¾ãã
å
ã®è¨³ã¯ãæ¥æ¬èªã¨ãã¦ããããããªããã®ã«ãªã£ã¦ãã¾ãã
A classic example of this is parsing tables, which encode the grammar of a programming language in a form interpretable by a fixed, fairly simple piece of code.
Ãå¤å ¸çãªä¾ã§è¨ãã°ã表ã®ãã¼ã¹ã§ããããã¯ããã°ã©ãã³ã°è¨èªã®ææ³ããå®å½¢ã®ããªãåç´ãªã³ã¼ãçã«ãã£ã¦èª¬æå¯è½ãªå½¢å¼ã«è¨å·åãããã¨ã§ãã
âå ¸åçãªä¾ã¨ãã¦ã¯ããã¼ã¸ã³ã°ãã¼ãã«ã¨ãããã®ãããã¾ããããã°ã©ãã³ã°è¨èªã®ææ³ããå®åã®ããªãåç´ãªã³ã¼ãã«ãã£ã¦è§£éã§ããå½¢ã«è¨å·åãããã®ã§ãã
"parsing tables" ã¯å
¨ä½ã§ã²ã¨ã¤ã®åè©ã§ããåè©-ç®çèªã§ã¯ããã¾ãããã¾ãã"interpretable" ã¯ã³ã¼ãã«ãã£ã¦ã解éãã§ãããã¤ã¾ãèªã¿è¾¼ãã§ããã«å¾ã£ã¦åä½ã§ããã¨ããæå³ã§ãã説æãã§ã¯ããã¾ããã
Finite state machines are particularly amenable to this form of attack
Ãç¹ã«ãã®ãããªåé¡ã«åãçµãã¨ãã«ã¯æéç¶æ æ©æ¢°ãæ¡ç¨ããã¦ãã¾ãã
âãã®æã®ããæ¹ã¨ãã¦ã¯æéç¶æ æ©æ¢°ãç¹ã«æè»ã«ä½¿ãã¾ãã
"amenable" ã¯ãå¾é ãªããã¤ã¾ããæè»ã«ä½¿ãããããã¨ãããã¨ã§ãã
can be constructed profitably as a data-driven algorithm
Ãçç£çãªå½¢ã¨ãã¦ãã¼ã¿é§åã®ã¢ã«ã´ãªãºã ã«ãªãã¾ã
âãã¼ã¿é§åã¢ã«ã´ãªãºã ã«ãããã¨ã§ããçµæãå¾ãããã§ããã
"profitably" ã¯ããå©çãå¾ããããããªå½¢ã§ãã¨ãããã¥ã¢ã³ã¹ã§ãã
One of the reasons data-driven programs are not common, at least among beginners, is the tyranny of Pascal.
Ããã¼ã¿é§åããã°ã©ã ãä¸è¬çã§ãªãçç±ã®ä¸ã¤ã¯ãå°ãªãã¨ãåå¿è ã«ããã¦ã¯ãPascalã«ããå§æ¿ã§ãããã
âãã¼ã¿é§åããã°ã©ã ãï¼å°ãªãã¨ãåå¿è ã®éã§ï¼ä¸è¬çã§ãªãçç±ã®ã²ã¨ã¤ã¯ãPascalã®ç¬è£ã§ãã
"at least among beginners" ã¯åã«ãããã¾ãã
This flies in the face of the theories of Turing and von Neumann
Ããã®ãã¨ã¯ãã¥ã¼ãªã³ã°ã¨ãã©ã³ã»ãã¤ãã³ã®çè«ã®åã«ã¯ãã¡æ¶ãã¦ãã¾ãã¾ã
âããã¯ãã¥ã¼ãªã³ã°ããã©ã³ã»ãã¤ãã³ã®çè«ã«çã£åããå§å©ã売ã£ã¦ãã¾ã
"fly in the face of" ã¯ãçã£åãã対ç«ãããã§ããããã¯ç§ã調ã¹ãã¨ãããªã®ã§ããããããã¯ãã¶ãçèªãããªãããã¨ããã»ã³ã¹ãå¿
è¦ã§ããé©å½ã«è¨³ãã§ã£ã¡ä¸ããã®ã¯ããããã¾ããã
I cannot recommend an implementation style more highly
Ãããé«åº¦ãªæ¬¡å ã®å®è£ æ¹æ³ãæ¨å¥¨ãããã¨ã¯ã§ãã¾ãã
âããã»ã©ãå§ãã§ããå®è£ ã¹ã¿ã¤ã«ã¯ããã¾ãã
"highly recommend" ã¨ããçµã¿åããã¯ããèããã®ã§ããã"recommend ... more highly" ã¯ãã®æ¯è¼ç´ã§ããããã以ä¸ãå§ãã§ããªãããã¤ã¾ããã¨ã¦ããå§ãã§ãããã¨ãããã¨ã§ãã
Maybe that's it:
Ã以ä¸ã§ãã
âãã£ã¨ãããããã¨ãªã®ã§ãããã
ãã㯠":" ã«ç¶ããã®ã«å¯¾ãã¦ã®æã§ãã
by construction
Ããã«ãæã«
âçµã¿ç«ã¦æ¹ã«ãã£ã¦
ãçµã¿ç«ã¦æ¹ã工夫ãããã¨ã§ï¼å¤éã¤ã³ã¯ã«ã¼ããé¿ããããï¼ãç¨åº¦ã®æå³ã§ãã
but it's usually done wrong in practice
Ãæ®éã¯ééã£ãçµæã¨ãªãã¾ã
âå®éã«ã¯æ£ããéç¨ãããªããã®ã§ã
"in practice" å®éã«ãã£ã¦ã¿ãã¨ã"done wrong" æªãããæ¹ã§è¡ããããã¨ããæå³ã§ããå ã®è¨³ã§ã¯ãªããééã£ãçµæãã«ãªãã®ããããã¾ããã