Saturday, January 11, 2014

New Check idea: VariableNameLengthCheck


Reason to check code:
- code are not intuitive;
- easy to miss usage during code review small variables are easy to miss in big expressions
- hard to put mouse pointer in Eclipse to see usages ("Toggle Mark Occurrences"), description see item "Mark occurrences".
- in methods/c-tors names of arguments are used to auto-generate code in place of call. Reasonable names will give developers quick understanding how to use method/c-tor without reading Javadoc. Example is here, item "Content assist can insert argument names automatically".

How checkstyle could help here:
Task is possible to be done by means of Checkstyle, and it is clear style problem.
Checkstyle never did analyse of English grammar and meaning of variable names - that is not very simple task.
All we need there is teach checkstyle make abbreviations and short forms of full type names, Eclipse somehow do it ("Content assist for variable, method parameter and field name completions", "Less typing for assignments"), so could borrow some ideas from it.

Examples of code:

0) Is there way to calculate sort name from type ?
class ApplicationPermissions  == allowed short form => perm or permission or appPerm

1)
private String getFeature(Permissions p) {
String className = this.getClass().getPackage().getName();
String perspective = this.getClass().getName();
return className + ":" + p + ":" + perspective;
}

2) Comparison of same type variables. Looks like we need to skip that cases from validation.
As it will be hard to find proper names for both variables.

public static ApplicationPermissions getLess(ApplicationPermissions p1, ApplicationPermissions p2) {

int result = p1.compare(p2);

if (result == 0) {
return p1;
}
if (result > 0) {
return p2;
} else {
return p1;
}
}

3)  Is there any way  restrict odd names usage ? base on set of short names but forbidden to use , see example below:

ApplicationPermissions(String str, int level) {
this.str = str;
this.level = level;
}


4) Any ability to do exception for "Object o", is it reasonable ?
private void addPermissionEntry(String permAliase, Object o, ApplicationPermissions permission,
ApplicationPermissions defaultPermission) {
List<PermPair> permPairsList = null;

if (this.permMap.containsKey(permAliase)) {
permPairsList = this.permMap.get(permAliase);
} else {
permPairsList = new LinkedList<PermPair>();
this.permMap.put(permAliase, permPairsList);
}
permPairsList.add(new PermPair(o, permission, defaultPermission));
}

5) Allow usage f small names as they are FOR indexes.
for (int i = 0; i < samples.length; i++) {
if (font.canDisplayUpTo(samples[i][1]) == -1) {
supported[i] = true;
}
}
more complicated:
for (int i = 0; i < rows.length; i++) {
String rowstring = rows[i];
String[] values = rowstring.split("\t");
for (int j = 0; j < values.length; j++) {
String value = values[j];
if (startRow + i < rowCount && startCol + j < columnCount) {
dataTable.setValueAt(value, startRow + i, startCol + j);
}
}
}
and 
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Assert.assertTrue(sh.isFilled(i, j));
}
}

6) clear sign one letter variable is assigned to good named variable/field

public PermPair(Object o, ApplicationPermissions p, ApplicationPermissions defaultPermission) {
this.feature = o;
this.permissions = p;
this.defaultPermissions = defaultPermission;
}

7) no excuse for short names for arguments as they could be used in code generation and other hint for Developers to let him skip reading of. See example from "6)"

8) We need to allow lover-cased CamelCase(short form) for variable from Classname, example:

Commandline cl = buildCommandLine(command);
executeCommandLine(cl, tableName, numberOfRecords);

9)

No comments:

Post a Comment