I consistenly get comments that my code is un-readable…I’ve updated my style to below…any suggestions for improvement?
/*
Check - Checks the user input text against regular expressions
*/
// Regular expressions
var patterns =
{
name: /^[a-zA-Z-\s]{1,20}$/, // checks full name
email: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{1,4}$/, // checks for valid email form
pass: /.{6,40}/, // checks for password length
url: /^[-\w&:\/\.=\?,#+]{1,}$/, // checks for valid url form
aml: /<(.+)_([a-z]){1}>$/ // checks for aml form
};
function check_item(reg1,text,id,res)
{
if(!reg1.exec(text.value))
{
o2(id,res);
return 0;
}
return 1;
}
function check_aml(text)
{
if(a=patterns.aml.exec(text))
{
if(a[2]=='p')
{
return 0;
}
else if (a[2]=='f')
{
return 1;
}
}
else
{
return 2;
}
}
// checks for empty text
function check_empty(text,id,res)
{
for(var d=0;d<text.length;d++)
{
if(text[d].value=='')
{
o2(id,res);
return 0;
}
}
return 1;
}
// checks if two text entries are the same
function check_same(text1,text2,id,res)
{
if((text1.value)!=(text2.value))
{
o2(id,res);return 0;
}
o2(id,'');
return 1;
}
Here’s a suggestion (foolishly subjective):
While this reflects my personal preference for javascript code indentation, it doesn’t mean that it will improve crappy code. And, yeah, that’s crappy code. It’s soooo much unreadable with variable names like a, o2, text, …
I mean you wrote
if(a = patterns.aml.exec(text)). Do you know the difference between=,==and===in javascript?For me a code is readable when someone that hasn’t written this code (like me) is looking at it and immediately knows what this code does. I must confess that this is not the case with your code. I have to actually think and waste time understanding the meaning of it.