ITKv4 StyleChangeProposal: Difference between revisions
From KitwarePublic
Jump to navigationJump to search
No edit summary |
|||
Line 74: | Line 74: | ||
==Proposed CXX File for Testing KWStyle== | ==Proposed CXX File for Testing KWStyle== | ||
<pre>void Function() | <pre> | ||
void Function() | |||
{ | { | ||
int i; | int i; | ||
Line 99: | Line 100: | ||
} | } | ||
} | } | ||
</pre> | </pre> |
Revision as of 15:59, 27 July 2010
Importance of consistent style
It is recognized that preferences in coding style varies widely from developer to developer. It is, however, important that the entire ITK toolkit have a consistent well defined style so that the entire toolkit appears to have been written by a single developer.
Guidelines for changing the style
- Should be similar to the existing style
- Changes from existing style should be driven to ease maintenance of the the consistent style
- (Others)
Current Status
- The current style document was written 7 years ago and has not been updated since then, and is a great reference document, but contains some ambiguity/inconsistencies
- The de-facto enforcement is that the code passes the KWStyle validation tester, and it appears that in at least one case (indentation) the KWStyle program is taking advantage of ambiguities in the style document to have different rules for indenting functions based on namespace and
- function declarations are nested inside namespace and class levels, and are currently KWStyle requires that the braces have a 2 space indentation
- function definitions are not nested inside the class level, and KWStyle requires that the braces are at column zero of the function
- KWStyle has been given too much authority in dictating the style rather than just checking it. We need to recognize that code that does not pass KWStyle may still be compliant with the standard, AND more importantly that making code pass KWStyle is in conflict with the guidelines.
Proposal
- Rules should be as simple to follow as possible, with as few conditionals as possible (i.e. all functions should have braces start at either column 0 or column 2 irregardless of if it is a declaration or a definition, or if it is nested inside a class)
- Rules should be compatible with automated reformatting utilities (uncrustify)
- Rules should be at least partially compatible with a wide range of editors (vim, emacs, VS) autoformatting capabilities (This may require more extensive style changes to make this occur)
- KWStyle should be updated to allow passing of the style.
Experience
The attached uncrustify (http://uncrustify.sourceforge.net/) configuration file closely matches the KWStyle (except for the inconsistent function brace indentation). http://www.itk.org/Wiki/images/6/62/Uncrustify_itk.txt File:Uncrustify itk.txt
#!/bin/bash ## Author Hans J. Johnson ## The goal of this script is to develop a consistent system of auto-formating ## and format testing such that if uncrustify is run, then the code will pass ## the KWStyle format checker. ## http://www.itk.org/Wiki/ITKv4_StyleChangeProposal #### Loop to auto check uncrustify. ITK_DIR=$1 ## The base directory of ITK LOCAL_DIR=$(dirname $0) RESULT_FILE=test.logger KWSTYLEBIN=~/src/KWStyle-build/bin/KWStyle UNCRUSTIFYBIN=~/local/bin/uncrustify OWF=${LOCAL_DIR}/KWStyle/ITKOverwrite.txt RULES=${LOCAL_DIR}/KWStyle/ITK.cvs.kws.xml rm -rf ${RESULT_FILE} cd ${ITK_DIR}/Code for i in $(find . -name "*.cxx" |fgrep -v orig; find . -name "*.h"|fgrep -v orig; find . -name "*.txx"|fgrep -v orig); do #for i in $(find . -name itkHexahedronCellTopology.cxx); do ORIG=${i}.orig.cxx if [ ! -f ${ORIG} ]; then cp ${i} ${ORIG} fi ${KWSTYLEBIN} -v -o ${OWF} -xml ${RULES} ${ORIG} >> /dev/null 2>&1 ORIG_STATUS=$? ${UNCRUSTIFYBIN} -c ${LOCAL_DIR}/uncrustify_itk.txt -l CPP -f ${ORIG} -o ${i}.temp >> /dev/null 2>&1 diff ${ORIG} ${i}.temp > /dev/null 2>&1 if [ $? -eq 0 ]; then rm ${i}.temp else mv ${i}.temp ${i} fi ${KWSTYLEBIN} -v -o ${OWF} -xml ${RULES} ${i} |tee ${i}.kwerrors CLEANED_STATUS=$? if [ ${CLEANED_STATUS} -ne 0 ]; then echo "${i}: ${ORIG_STATUS} --> ${CLEANED_STATUS}" |tee -a ${RESULT_FILE} fi echo "========" done |tee ${LOCAL_DIR}/ERRORS.logger echo "PLEASE COMPILE AND TEST ITK"
Proposed CXX File for Testing KWStyle
void Function() { int i; } class C { public: void ClassMethod() { int i; } }; void FunctionWithLocalClass() { class LocalClass { public: void LocalClassMethod() { int i; } } }