It's interesting to observe the different indentation conventions in various programming languages. Recently, I came across a code snippet from the PHP manual that caught my attention:
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
The indentation of each case statement being inside the switch block seems logical as it enhances readability and clearly shows the structure.
However, when I tested a similar JavaScript switch statement in JSLint:
switch (i) {
case "apple":
alert("i is apple");
break;
case "bar":
alert("i is bar");
break;
case "cake":
alert("i is cake");
break;
}
JSLint flagged an error suggesting that the code should be formatted like this instead:
switch (i) {
case "apple":
alert("i is apple");
break;
case "bar":
alert("i is bar");
break;
case "cake":
alert("i is cake");
break;
}
This new layout aligns each case statement with the switch block, which feels unconventional and not as clear as the previous version. Is JSLint simply following a specific convention or is there a deeper reason why the indentation differs?