Tuesday, October 19, 2010

Mixed Content Object Naming

Don't use the same name for multiple objects of different scope and type, and don't give names to objects that are incongruous with their types or meanings.
Try to give each variable and object names that identify their scope, type and meaning.

Sad Code:
<%
Dim MyVar: MyVar = 0
Dim MyVar_A: MyVar_A = 0
Dim MyVar_B: MyVar_B = 0
%>
<script type="text/javascript">
function DoStuff() {
 MyVar_X = txtMyVar.innerText + intMyVar.value + MyVar.value;
}
</script>
<html>
<input type="text" id="MyVar" value="<%=MyVar%>" />
<input type="hidden" id="intMyVar" value="<%=MyVar_A%>" />

<table><tr><td id="txtMyVar"><%=MyVar_B%><td></tr></td>
</html>

Happy code:
<%
Dim mnMyVar: mnMyVar = 0
Dim mnMyVar_A: mnMyVar_A = 0
Dim mnMyVar_B: mnMyVar_B = 0
%>
<script type="text/javascript">
function DoStuff() {

 var nMyVar;

 nMyVar = tdMyVar_B.innerText + hidMyVar_A.value + txtMyVar.value;
}
</script>
<html>
<input type="text" id="txtMyVar " value="<%=mnMyVar%>" />
<input type="hidden" id="hidMyVar_A" value="<%=mnMyVar_A%>" />

<table><tr><td id="tdMyVar_B"><%=mnMyVar_B%><td></tr></td>
</html>