ããã§è§¦ããã·ã¹ãã ãããç¨åº¦ã¯åãã®ã§ããµã³ãã«ãä½ãå§ãã¾ãããããã¨ãããã¾ã¼ãéåã¨ãã°ããããªã¼ã
ããã¯å ¨é¢çã«åã®ç£ç£è²¬ä»»ã§ããããããããã
ãã¶ããããã ããã
ãããããããããã°ããã¦ã
ãã¾ãããã°ããã¼ãªãã¯ãã ãã
ã¿ãããªæ¹éããè¨ã£ã¦ãªããããé²è¡ã³ã¼ããæ¸ããªãã¨ãã£ãã¢ããã¤ã¹ã説æä¸è¶³ã§ãã¾ãä¼ãããªãã£ãããã§ãã
å 容ï¼
- é²è¡ã³ã¼ãããããã
- é²è¡ã¨å¥ç´ï¼ã³ã³ãã©ã¯ãï¼ã¯éããã ã
- ã¢ãµã¼ã·ã§ã³ã使ãã°ãããªã
- å®ç´ãªã¢ãµã¼ã·ã§ã³ãä½ã£ã¦ã¿ã
- ã©ã¤ãã©ãªã³ã¼ãã¨ãã«ãã·ã¹ãã
âé²è¡ã³ã¼ãããããã
é²è¡ã³ã¼ãã«é¢ãã¦è¨ãã°ãæ´æ¦ã®å士ã®ãããªè·æ¥ããã°ã©ããã俺ã¯èª°ãä¿¡ããã¼ï¼ãã¿ãããªã³ã¼ããæ¸ãããã§ããã
function sum(x, y) {
var objectUsed = false;
if (x === undefined) {
alert("第1å¼æ°ãundefinedã ã£ã¦ããã²ã§ã¼ãªããã®ãã±ã");
throw new Error("ãã± 1");
}
if (x === null) {
alert("第1å¼æ°ãnullã ã£ã¦ããã©ãããã¦ããããã®ã¿ã³ã");
throw new Error("ã¿ã³ 1");
}
if (typeof x == 'object' && x instanceof Number) {
alert("å¼æ°ã«objectã¯ããããããã¡ããè¨ç®ã¯ãã¦ãã£ãã©ã");
objectUsed = true;
x = x.valueOf();
}
if (typeof x != 'number') {
alert("ã°ã©ã©ã¼ãæ°ãããã¼ç¬¬1å¼æ°å ¥ãããªï¼ ãã«ã");
throw new Error("ãã« 1");
}
if (isNaN(x)) {
alert("NaNã¯è¨ç®ãã¦ãããã¼ã£ã¦ããã®ãã²ã");
throw new Error("ãã² 1");
}
// 第1å¼æ°ã®ãã§ãã¯çµäºif (y === undefined) {
alert("第2å¼æ°ãundefinedã ã£ã¦ããã²ã§ã¼ãªããã®ãã±ã");
throw new Error("ãã± 2");
}
if (y === null) {
alert("第2å¼æ°ãnullã ã£ã¦ããã©ãããã¦ããããã®ã¿ã³ã");
throw new Error("ã¿ã³ 2");
}
if (typeof y == 'object' && y instanceof Number) {
if (objectUsed) {
alert("ã ã¼ãããobjectã¯ãããã£ã¦è¨ã£ããã"
+ "ãããããããã¢ã³ãã³ã¿ã³ã");
throw new Error("ã¢ã³ãã³ã¿ã³");
} else {
alert("å¼æ°ã«objectã¯ããããããã¡ããè¨ç®ã¯ãã¦ãã£ãã©ã");
y = y.valueOf();
}
}
if (typeof y != 'number') {
alert("ãã®ééãããã¾ã§æ¥ã¦ã¯ã±ããããã¼ç¬¬2å¼æ°ã£ã¦ã®ã¯ã"
+ "ã©ããããªã§ã¦ã±ã³ã ãã¡ã¼ããã«ã");
throw new Error("ãã« 2");
}
if (isNaN(y)) {
alert("ãããã£ããªã¼ãNaNã¯è¨ç®ãã¦ãããã¼ã£ã¦ããã®ãã²ã");
throw new Error("ãã² 2");
}
// 第2å¼æ°ã®ãã§ãã¯çµäºalert("ãã¼ããã¾ã¼ããã ããã");
return x + y;
}
ã¿ãããªâ¦â¦ è¨å¤§ãªé²è¡ã³ã¼ãã«ç´ï¼ã¾ãï¼ãã¦ããæ¬è³ªçã«ã¯ä½ããã£ã¦ãããããè¦ããªããªã£ã¦ãã¾ãã®ã§ããããã®å ´åãªãå®ã¯ï¼
ã£ã¦æ¸ãã°ãããã§ãããå¼æ°ãæ£å½ã§ãããã¨ãä¿è¨¼ããã®ã¯é¢æ°ãå¼ã¶å´ã®è²¬ä»»ã§ãã
function sum(x, y) {
return x + y;
}
[追è¨]ä¸ã®ä¾ã¯ã¸ã§ã¼ã¯ãªãã§ãããã£ã¦èª¤è§£ã®ãã¨ã«ãªãããããã¾ãããã³ã¡ã³ãæ¬ã«è£è¶³èª¬æãããã®ã§ããã¡ããåç §ãã¦ãã ããã[/追è¨]
âé²è¡ã¨å¥ç´ï¼ã³ã³ãã©ã¯ãï¼ã¯éããã ã
ã¨ã¯ãã£ã¦ãï¼
- ã©ãã®èª°ããå¼ã°ããããåãã£ã¦äºæ¸¬ã§ããªãã
- å¼ã¶å´ã責任ãæããã¦ããã¨ããä¿è¨¼ããªãã
ã¨ãããããäºæ ã¯ããã¾ãã1ã®å ´åã¯ãé²è¡ã³ã¼ãã¨ããããã¯ãå½è©²é¢æ°ãªãã¡ã½ããã®è²¬åã®ä¸é¨ã¨ãã¦å¼æ°ããã®ä»ã®å ¥åãã¼ã¿ã®ãã§ãã¯ãå¿ è¦ã§ãã
åé¡ã¯2ã®ã±ã¼ã¹ã§ããæ§å説ããããã¯èªåã¨ååãä¿¡ããç«å ´ãªãã°ããªã«ãããªãããæ£è§£ã§ããããã¯æ¥µè«ã ã¨ãã¦ã*1ããã¤ã¾ã§ãæ®ããããªãã§ãã¯ã³ã¼ãã¯æå¿ãã¾ãããã¡ã¤ã¤ã¼å çã®å¥ç´é§åããã°ã©ãã³ã°ã®çºæ³ã«åºã¥ãã¹ãã§ãããã
å¥ç´ããã§ãã¯ããã³ã¼ãã¯ãéçºä¸ã¯æ©è½ããã¦ãæçµçã«ã¯åãå¤ããããã«ããããã§ããEiffelãDè¨èªã§ã¯è¨èªæ©è½ã«ããå¥ç´ããµãã¼ããã¦ãã¾ããxUnitã®æ¹å¼ã§ã¯ãå¥ç´ãã§ãã¯ã«ç¸å½ããã³ã¼ãï¼ã¦ããããã¹ãï¼ãå¤ã«åºãã¦ãæ¬æ¥ã®ãã¸ãã¯ãæ±ããªãããã«ãã¦ãã¾ãã
ãããããã¨ã®ã½ã¼ã¹ã³ã¼ãã«åãè¾¼ãæ¹å¼ããããªã«æªãã¯ããã¾ãããæã®Cè¨èªã®ã¢ãµã¼ã·ã§ã³ã®ãããªç´ æ´ã¯æ¹å¼ã§ããã£ããå½¹ã«ç«ã¡ã¾ããããã§ãJavaScriptåãã®å¤å ¸çï¼åç´ãªã¢ãµã¼ã·ã§ã³ï¼è¡¨æï¼ãå°ä¸æéã§ã§ã£ã¡ããã¦ã¿ã¾ããã
âã¢ãµã¼ã·ã§ã³ã使ãã°ãããªã
å ã®éå°é²è¡çsumã¨åçã«å®å ¨ãªé¢æ°ã¯ãã¢ãµã¼ã·ã§ã³ãç¨ãã¦æ¬¡ã®ããã«æ¸ãã¾ãã
function sum(x, y) {
ASSERT(isNormalNumber(x));
ASSERT(isNormalNumber(y));return x + y;
}
isNormalNumberã¯æ¬¡ã®ãããªé¢æ°ã§ã(((x != null)
ã¯ãªãã¦ã大ä¸å¤«ãªããã§ããããã®æ¸ãæ¹ãã¤ãã£ãªã åãã¦ãã¾ãã))ã
å種ãã¼ã¿åãã§ãã¯é¢æ°ã¯åãã£ã¦æºåãã¦ã©ã¤ãã©ãªåãã¦ããã¨ããã§ãããã
function isNumber(x) {
return typeof x == 'number' || (x != null && x instanceof Number);
}function isNormalNumber(x) {
return isNumber(x) && !isNaN(x);
}
ã¢ãµã¼ã·ã§ã³æ©è½ã®è¦ä»¶ã¨ãã¦ãã¨ãããã次ã®3ã¤ãè¦æ±ãã¾ãããã
- ä¸è¦ã«ãªã£ããåãå¤ãã
- æå®ãããæ¡ä»¶å¼ãå½ã®ã¨ãã¯ä¾å¤ã§å¼·å¶çµäºããã
- ã½ã¼ã¹ã³ã¼ãã®å ´æï¼ãã¡ã¤ã«åãè¡çªå·ï¼ã表示ã§ããã
âå®ç´ãªã¢ãµã¼ã·ã§ã³ãä½ã£ã¦ã¿ã
ã¢ãµã¼ã·ã§ã³ã®1çªç®ã®è¦ä»¶ãåãå¤ãå¯è½ãã¯ãããã¹ãå¦çã§åãè½ã¨ãã¦ãã¾ãã°ããã§ãããï¼ãªã«ããå®ç´ã ãããªï¼ãä¾ãã°ï¼
> perl -i.asserted.js -n -p -e "s/ASSERT\s*\(//" sum.js
ã¨ããã°ãæåå"ASSERT"ãå«ãè¡ãåé¤ããã¾ã*2ããã®åç´ãªæ¹æ³ãæ©è½ããããã«ã¯ãã¢ãµã¼ã·ã§ã³ãè¤æ°è¡ã«æ¸¡ã£ã¦æ¸ãã®ã¯ç¦æ¢ã§ããã
// ããã¯ãã¡
function sum(x, y) {
ASSERT(isNormalNumber(x) &&
isNormalNumber(y)); // ãã®è¡ã ãæ®ã£ã¦ãã¾ãreturn x + y;
}
ASSERTã«æ¸¡ãããæ¡ä»¶å¼ãå½ã®ã¨ãã«ä¾å¤ãæããã«ã¯ãããããé¢æ°ãæ¸ãã°ããã®ã§ããã©ã3çªç®ã®è¦ä»¶ãã½ã¼ã¹ã³ã¼ãã®å ´æã表示ã§ãããããã«ã²ã¨å·¥å¤«ããã¾ãããã®ç¹ãããã¹ãå¦çã¨çµã¿åããããã¨ã«ãã¦ãASSERTã®å®ä½ã¨ãªãé¢æ°ãã¾ãæ¸ãã¦ããã¾ãã
/* assert.js */function __assert__ (location, cond, msg) {
if (!msg) {
msg = "";
}
if (!cond) {
var s = "Assertion failed!\n" + location + " " + msg;
alert(s);
throw new Error(s);
}
}/* ASSERTã¨ããæååãããã«å¼ã£ããããªãããã®ããªã㯠*/
window["ASS" + "ERT"] = function (cond, msg) {
__assert__("", cond, msg);
}
[追è¨]ããªãã¯ã追å ãã¾ããã[/追è¨]
é¢æ°ASSERTãå®ç¾©ãã¦ããã®ã§ããã®ã¾ã¾ã§ã使ãã¾ããããã½ã¼ã¹ã³ã¼ãã®å ´æã¯è¡¨ç¤ºã§ãã¾ããããªãªã¸ãã«ã®ã½ã¼ã¹ãã¡ã¤ã«ã次ã®ãããªã¹ã¯ãªããã§å¦çãã¾ãã
#! /bin/perl#
# fixcode
#
for (@ARGV) {
my $file = $_;
next unless (open(FILE, $file));
while () {
s/ASSERT\s*\(/__assert__("$file:$.:",/g;
print;
}
close(FILE);
}
> perl fixcode.pl sum.js > sum.debug.js
ããã¨ãsum.debug.jsã¯æ¬¡ã®ããã«ãªãã¾ãã
function sum(x, y) {
__assert__("sum.js:2:",isNormalNumber(x));
__assert__("sum.js:3:",isNormalNumber(y));return x + y;
}
firebugãRhinoãªã©ã使ã£ã¦ããã°ãä¾å¤ãçºçãããã½ã¼ã¹ã³ã¼ãã®å ´æã¯ç¹å®ã§ããã®ã§ãããã¯ç¡é§ãªå¦çã®ããã«æãã¾ããããç¶æ³ã«ããããã¨ãè¨ããªãã®ã§ãã
âã©ã¤ãã©ãªã³ã¼ãã¨ãã«ãã·ã¹ãã
JavaScriptã®ã©ã¤ãã©ãªãæ§æããå ´åã«ã次ã®ã¸ã¬ã³ããããã¾ãã
- éçºä¸ã¯ãçãè¤æ°ã®ã½ã¼ã¹ãã¡ã¤ã«ã®ã»ããæ±ããããã
- 使ãã¨ãã¯ãã½ã¼ã¹ãã¡ã¤ã«ã1ã¤ã«ã¾ã¨ã¾ã£ã¦ããã»ãã使ããããã
æã ã®å ´åã¯ããã«ãããã»ã¹ï¼makeãå©ç¨ï¼ã®ä¸é¨ã§æ¬¡ã®ãããªä½æ¥ãè¡ã£ã¦ãã¾ãã
cat $(JS_SRC_LIST) > mylib.js
ããã§$(JS_SRC_LIST)
ã¯ãããã¤ãã®JavaScriptãã¡ã¤ã«ã並ã¹ããªã¹ããä¾ãã°ãassert.js foo.js bar.js baz.js
ã§ãã
mylib.jsããã©ã¦ã¶ãªã©ã«ãã¼ãããã¦å®è¡ããã¾ãããmylib.jsã®è¡çªå·ãåãã£ã¦ãããã¨ã®ã½ã¼ã¹ï¼foo.js, bar.js, baz.jsã®ã©ããï¼ã®è¡çªå·ã¯åããã¾ãããããã§ãåã«catãã代ããã«ã
ã®ããã«ããã°ãã¢ãµã¼ã·ã§ã³ãããã¨ã®ã½ã¼ã¹ã³ã¼ãè¡çªå·ãç¹å®ã§ãã¾ãã
perl fixcode.pl $(JS_SRC_LIST) > mylib.js
ãããã«ãã¦ããã½ã¼ã¹ã³ã¼ããããã¹ãå¦çã§æ¸ãæããã¨ããä¹±æ´ãªæ段ã使ã£ã¦ããã®ã§ããã«ãã·ã¹ãã ã«çµã¿è¾¼ã¾ãªãã¨ç ©éã§ãã£ã¦ãããªããã¨ã«ãªãã§ãããã
ãããªå®ç´ãªã¢ãµã¼ã·ã§ã³ãã©ãã¾ã§å½¹ã«ç«ã¤ãï¼ ä»æ¥æãã¤ããã ããªã®ã§ãããã¾ããã使ã£ã¦ã¿ã¦ã¾ãå ±åãã¾ãã
*1:ãã¼ã ã®è½åãç¶æ³ã«ãã£ã¦ã¯ãæå¤ã¨ãã¾ãããã¾ãã
*2:ãã¨ã®ãã¡ã¤ã«ã¯ sum.js.asserted.js ã¨ãªãã¼ã ããã¾ãã.jsæ¡å¼µåã2ã¤ãã£ã¦æ°æã¡æªãã§ãããDojo Toolkitã§ããdojo.js.uncompressed.js ãªãã¦ååã使ã£ã¦ãã¾ããã