éçºã¤ãã¬ã¼ã·ã§ã³åé
éçºã¤ãã¬ã¼ã·ã§ã³ãæ©ãããã°ãããªãã®åé¡ãåæã«è§£æ±ºããããã¨ä¿¡ãã¦ãã¾ãããªããæè¿ãä»ã®è¦ç´ ã軽è¦ãããã¦ããããç¹ã«ã¤ãã¬ã¼ã·ã§ã³é度ã«å½±é¿ããªããããªãã¨ãããã¦ããæ°ããã¦ãã¦ãä¿¡ä»°ã¨ãã®ã¬ãã«ãããããªããã¨ããæ°ããã¦ããã®ã§ãã¡ãã£ã¨æ¸ãã¦ã¿ããããªã¨ã主㫠C++ ã®è©±ã§ãã
ä»äºã¨ããã¦ãã¨è¯ãå¤æåãæ±ããããããã¾ãããå¤æã¨ããã®ã¯çµæ§é£ããã§ããããã¢ããã¼ã A 㨠B ã§æ©ãã æã«ãæãéããã°ä¸¡æ¹ã§ããããã¾ããéçºã¤ãã¬ã¼ã·ã§ã³ãç¡éã«éãããã¨ãå¿ è¦ã¨ãããå¤æåã¯ã¼ãã«æ¼¸è¿ãã¦ããã¾ãããã£ããã
2æã§å¤æ´ã®æ£å½æ§ãé«éã«ç¢ºèªã§ããããã«ãã
make (ããã®ä»ã®ãã«ãã³ãã³ã)ã¦ãã£ãããã«ããã§ãã¦ã make check (ããã®ä»ã®ãã¹ãã¹ã¯ãªãã)ã¦ãã£ããé ããªããã¹ããå ¨é¨èµ°ããã¨ããä½å¢ã好ãã§ãã試ãããã«ã¯ãã£ã¡ã®ãã£ã¬ã¯ããªã§ make ãã¦ãã£ã¡ã§ build.sh ãèµ°ããã¦ããã㧠bin/test foobar ã¨ãããã¨ã§ããâ¦â¦ã¿ãããªã®ã¯ã¨ã¦ãè¯ããªããåç´ã«æ°ãããã¼ã ã«å ¥ã人ã¨ãã«ã¨ã£ã¦ãå³ããã§ãããâ¦â¦
ãã®ããã«ã¯(æ£ç´ã©ããã¨æããã©)ãã¹ãã®ç¶²ç¾ æ§ãæ¨ã¦ã¦è¯ãã¨ããèãã¦ã¾ããã¾ãçæ³çã«ã¯ CI ã§ã¯ç¶²ç¾ çãªãã¹ããèµ°ããã©ãæå ã§ã¤ãã¬ã¼ã·ã§ã³åãæã®ãã¹ãã¯ãã®ãµãã»ãããã¿ãããªã®ãè¯ãã®ã§ãããã CI ã¨ããã° CI ã®æéã¡ãã£ã¨åãã¨ãã好ãã§ã好ããªãã ãã©ãã®æéãã¶ãæ¬æ¥ã®ä½æ¥ã«åããæ¹ãè¯ãã±ã¼ã¹ãå¤ãã¨æãã®ã§ãä¿¡ä»°ã ãªãã¨æããããã§ãããã¾ãã
ã¤ãã¬ã¼ã·ã§ã³ãé«éåãããããªãã©ããªåªåã§ãããã®ã§ãã¡ããã¨è¤æ°ããã»ããµä½¿ã£ã¦ä¸¦åã«ãã¹ããèµ°ãä»çµã¿ãæ´ãããããã¾ãã
ç´°ããã¦ããããã¹ããé¿ãã
ã¦ããããã¹ããã¡ããã¨æ¸ãã¦ãã¦ãããªããçµå±ããã使ã£ã¦ãã main ã®é¨åã¨ãã Python ã¤ã³ã¿ã¼ãã§ã¤ã¹çããã¦ããããã®æ¥ç¶é¨åã«ãã°ãåºããããã®ã§ããªããçµå± end-to-end ã§èµ·ããæåã§ãã¹ãããã®ãä¸çªç¢ºå®ã ã¨æã£ã¦ãã¾ãã Ruby ãªããã¯ã»ã¨ãã© (å ¨é¨ã ã£ãï¼) ã®ãã¹ãã Ruby ã§æ¸ããã¦ãã¨æããã§ããããããªæ°æã¡ã§ãã
ãªããªãããããã©ããã¨ã¯æãã®ã§ãããç´°ããããã¦ããããã¹ããæ¸ããªãããã«ãã¦ãã¾ããç´°ãããã¹ãæ¸ãã¦ãã¨ã³ã±ãæã«ãã®å ´æã®ç¹å®ãã©ã¯ã«ãªããªã©ã®ã¡ãªãããããã®ã§ããããã¹ããã³ã±ã確çã£ã¦ãããªã«é«ããªãã¦ãã³ã±ãæã«ãããæéã®ç¯ç´ã«å¯¾ãã¦ãã¡ããã¨ã¦ããããã¹ããæ¸ãã³ã¹ãã®æ¹ãé«ãã¨ãããã¨ã¯çµæ§ããããã«æãã Ruby ã®ä¾ã®ããã«ã end-to-end ã®ãã¹ãã®æ¹ããç´°ããã¦ããããã¹ããæ¸ããã楽ãªã±ã¼ã¹ã¯ç¹ã«ã AST ãèªåã§æ§ç¯ããããããã¹ãããã AST ã«ãªã£ã¦ãããã§ãããã³ã¼ããæ¸ããæ¹ãã©ã¯ãã¨ãã話ã
ããããæå°åãã
ãã«ãæéãæå°åãããã¦ãããããµã¤ãºã£ã¦çµæ§å¹ãã¨ããä¿¡ä»°ãæã£ã¦ããã®ã§ããªãã¹ãããããå°ãããã¦ãã¾ããæã¯ãããæ¸ãã¨æä½1ã¤ã¯ã¯ã©ã¹ããã£ãã®ã ãã©ãæè¿ã¯é¢æ°2,3å宣è¨ããã¦ãã ããã¿ãããªãã¨ãå¤ãã
ä¿¡ä»°ãªã®ã§æ¬å½ã«å¹ãã¦ãã®ãã¯ãããã¾ããããããããªã³ãªã¼ã©ã¤ãã©ãªã¨ã使ã£ãæã«ããã®é¨åã ãæããã«ã³ã³ãã¤ã«ãé ãã£ããããã®ã§ãä¸ã®ä¸ã®äººãã£ã¨ããããã·ã³ãã«ã«ãã§ããã°ããã㧠template 使ããªãããªã©æ°ãã¤ãã¦ãããã¨ãªãâ¦â¦ã¨ããæã£ã¦ãã¾ãã
ãã¨ã¾ãããè¨ããããã¨ã§ãããéå°ãªæ½è±¡åã¨ãã§ããã YAGNI!
ããããã§ãã¯ãã
ã¤ãã¬ã¼ã·ã§ã³ãæéåãããã®ã§ãã£ã¦ãã¿ã¤ãã³ã°ãæéåãããã®ã§ã¯ãªãã®ã§ãå²ã¨ããã assertion ç¸å½ã®ã³ã¼ããå ¥ãã¾ããä¾ãã°
CHECK_LE(0, i); CHECK_GT(x_.size(), i); return x_[i];
ã®ãããªã³ã¼ããæ¸ãã¾ãã x_.at(i) ãããã°ãã§ãã¯ããä¸ã§å¤±æããã throw ãã¦ãããã®ã¯ãã¡ããç¥ã£ã¦ããã®ã§ãããä¾å¤ãèµ·ããæã«ãã©ãã§èµ·ããä¾å¤ããããã¯ãã¬ã¼ã¹ããç¹å®ãã¦ã gdb 㧠i ã®å¤ãã©ããªã£ã¦ããããªã©ãããã®ãçµæ§æéãªã®ã§ãå¼ã³åºãå ã®è¡æ°ã¨ãæ¯è¼ãã¦ããå¤ã表示ãã¦ããããä¸è¨ã®(glogç±æ¥ã®)ãã¯ãããæ°ã«å ¥ãã§ãã
ã¾ãã CHECK_EQ(x, y) << z ã¿ãããªãã¨ãã§ãã¦ã z 㯠x != y ã®æã ãè©ä¾¡ããã¦è¡¨ç¤ºãããã®ã§ããããã®æ operator<< ã x, y, z ã®å ¨ã¦ã«å®ç¾©ããã¦ããªãã¨ãããªãã®ã§ãããã©ããããã©ãç©æ¥µçã« stringify ããã³ã¼ããæ¸ãããã«ãã¦ãã¾ãã
æé©åãªãã·ã§ã³ã¤ãã§éçº
ããã¯ã±ã¼ã¹ãã¤ã±ã¼ã¹ã§ãç¹ã«åæã®é ã¯ããã -O ã¯ã¤ããã«ä½æ¥ãã¾ããããã¹ãæéãå¢ãã¦ããã¨ãåºæ¬çã«å¸¸ã«æé©åãªã³ã§ä½æ¥ãã¦ããã¾ã«èµ·ãã gdb ã§ã®ãããã°ä½æ¥ãã¤ãããªãã®ã¯ææ ¢ãããã¨ããã¹ã¿ã¤ã«ã«ãããã¨ãå¤ãã§ããä¸è¨ãã¯ãã§ããããã ãã«ãã§ãã¯ãå ¥ã£ã¦ããã¯ããªã®ã§ã gdb ãå¿ è¦ãªå¯è½æ§ãè½ã¨ãã¦ããâ¦â¦ã¯ãã§ãã
ãã¨ã大ããããã¸ã§ã¯ããªã -g ãåã£ã¦ãã¤ãã¬ã¼ã·ã§ã³ãæéåã㦠printf å ¥ãã¾ãã£ãæ¹ããããã°ãããããã¨ããæãããã¾ãã LLVM ã§éãã§ãæã¨ãã¯ãããçµå±ä¸çªè¯ãããã«æãã¾ããã
ãã¨ããªãªã¼ã¹ãã«ãã§æ¶ãã assertion ãã¨ããæ¦å¿µãå«ãã§ããã»ã¼ 99.9% ããã©ã¼ãã³ã¹ã«å½±é¿ããªãããã«ãå¯ä½ç¨ã®ããå¼ã®è¨è¿°ãé¢åã«ãªããããã解決ããã¨åç §ãããªãå¤æ°ãã§ãããã¨ããã£ã¦å«ãã NDEBUG æã ãèµ·ããã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãã§æéãæ¶è²»ãããããã®ãå«ãã
ãªãã¹ã auto ã使ããªã
auto ãæ¸ãã¦ãæã¯ã©ã¯ãªãã§ãããèªãã§ãæã«ãããã³ã¹ããå¢ããã®ã§ãæçåå²ç¹ã¯ 20 æåããããããªãã¨æã£ã¦ãã¾ãã iterator ãªããã¯ããã©ããããæèããèªæãªãã¨ãå¤ãã®ã§ã auto 使ããã¿ãããªæ°æã¡ã§ãã¾ãã
auto ã«éãããã«ã¼ãå å ã std::transform ãä¸é æ¼ç®ã¿ãããªãã¤ããæ¸ãã®ãã©ã¯ã«ãªããããã«ãåãªãç´æ´ãªã«ã¼ããåå²ã®æ¹ãå¯èªæ§ãé«ãã¨æãããã¨ãå¤ãã¦ãä»ã®äººãã使ãé »åº¦ãä½ããªããã¨ããæãã¾ãã
ä¸æ¹ã§ using namespace ã¯ãã¾ãå®å®³ãç¡ãã¦ã³ã¹ãè¯ãã¨æã£ã¦ãã(ç¹ã« std ã¯ã¹ã¿ã¤ã«ãéãã®ã§ç¡ãã¦ãæç½)ã®ã§ãããã³ã¼ãã£ã³ã°è¦ç´ã§è¨±ããã¦ãªãã®ã§è«¦ãã¦ãã¾ãâ¦â¦
ã¾ã¨ã
ãªããä»ã«ãããæ°ãããã®ã§ãããä»ã¨è¦æ¯ã¹ã¦ã¦ãèªåã®ã¹ã¿ã¤ã«ãéãã¨æãããã¨ãå¤ãã¨ããã並ã¹ã¦ã¿ã¾ãããä»ã®äººã®å·¥å¤«ã¨ããæ°ã«ãªãã¨ããã§ãã