Slide 1CommentsWhat Comments are NotTypes of CommentsTypes of CommentsTypes of CommentsTypes of CommentsTypes of CommentsTypes of CommentsThings to CommentMaintaining CommentsMaintaining CommentsMaintaining CommentsMaintaining CommentsMaintaining CommentsMaintaining CommentsMaintaining CommentsMaintaining CommentsMaintaining CommentsMore Commenting “DON’Ts”More Commenting “DON’Ts”More Commenting “DON’Ts”More Commenting “DON’Ts”More Commenting “DON’Ts”More Commenting “DON’Ts”More Commenting “DON’Ts”Commenting “DOs”Commenting “DOs”Commenting “DOs”Commenting “DOs”Commenting “DOs”Commenting “DOs”Other Commenting SuggestionsCommenting Control StructuresCommenting FunctionsCommunicating in Code:CommentingProgramming Studio, Fall 2017Tanzir AhmedNote: several examples in this lecture taken from The Practice of Programming by Kernighan and PikeCommentsAn internal documentation mechanismDocumentation of the code stays with and close to the codeComments should complement good coding style, not replace itThe better written your code, the fewer comments you will needPoor commenting is a waste of time and sometimes harmfulWhat Comments are NotDesign documentsAPI referencesSpecificationsPadding to increase your “lines of code”Places to tell jokes to future programmersTypes of CommentsRepeat of the CodeRepeating what code does or stating the obvious is useless//loop through all Teamsfor(i=0;i<NumTeams;i++) //add that team’s players to total TotalPlayers += Team[i].NumPlayers;Types of CommentsRepeat of the CodeRepeating what code does or stating the obvious is useless//Find total number of players in leaguefor(i=0;i<NumTeams;i++) TotalPlayers += Team[i].NumPlayers;Types of CommentsExplanation of the codeCan be a sign that the code is difficult to understandDon’t comment bad code – rewrite itIf the explanation is too long, code should be rewritten/* Update the attenuation due to multiple scattering whenever there is a valid layer hit. The next intersection layer hit will be skipped over and the intersection point will generate a new vector and the last vector created will be stored */for(i=IntersectLayer-1;i<NumLayersHit;i++) { if (isValidHit(r)) { Attenuation.Update(Layer[i++].HitPoint(genVector(r))); }}Types of CommentsMarker in the CodeUsed as notes to the developer//***** FIX THIS ROUTINEOften have key phrases to search onUsed to visually separat e code blocksAs a style element, e.g. function header blocksTypes of CommentsSummary of the codeShort statement summarizing several lines of code.Useful for quick scanning over code to find areas where things are happeningProvides a global “map” to the codeTypes of CommentsDescription of the code’s intentBest type – explains the why , not the howComments should add something that is not immediately evident from the codeUnderstanding the intent of code is usually the issue – it’s much easier to tell exactly what the code is doingThings to CommentFunctionsGlobal variablesCan be tough to keep track ofCode that is truly complicatedMight require lots of explanation, references to algorithmsMaintaining CommentsComments need to be maintained as code is edited!Conflicts between comments and code cause tremendous difficultyCommenting styles can assist with maintenance/*************************//* *//* My comments *//* *//*************************/Maintaining CommentsComments need to be maintained as code is edited!Conflicts between comments and code cause tremendous difficultyCommenting styles can assist with maintenance/************************* * * * My comments * * * *************************/Maintaining CommentsComments need to be maintained as code is edited!Conflicts between comments and code cause tremendous difficultyCommenting styles can assist with maintenance/************************ * * My comments * ************************/Maintaining CommentsComments need to be maintained as code is edited!Conflicts between comments and code cause tremendous difficultyCommenting styles can assist with maintenance/************************ My comments ************************/Maintaining CommentsComments need to be maintained as code is edited!Conflicts between comments and code cause tremendous difficultyCommenting styles can assist with maintenanceBlocks of commentsLining up commentsMaintaining CommentsDifficulty lining up comments:int Capacity; // Number of cats we could keepint NumCats; // Number of cats in the housefloat CatFood; // Monthly cost of cat foodMaintaining CommentsDifficulty lining up comments:int Capacity; // Number of cats we could keepint NumCats; // Number of cats in the housefloat CatFood; // Monthly cost of cat foodfloat BoardingCosts; // Cost to board cats per dayMaintaining CommentsDifficulty lining up comments:Difficult to maintain over time, so tend to degrade with modificationLeaving enough space often leads to short commentsMaintaining CommentsComments often lastDon’t use comments you don’t want others to seeDon’t expect comments to really be “temporary”Exceptions: temporary “markers”If markers are left in code, be sure they will be foundMore Commenting “DON’Ts”Don’t include useless commentsMOV AX, 723h ; R.I.P.L.V.BMore Commenting “DON’Ts”Don’t include useless commentsMOV AX, 723h ; R.I.P.L.V.B(Beethoven died in 1827 = 723h)More Commenting “DON’Ts”Don’t include useless commentsAvoid endline commentsFor one line of code, tend to be repetitive not much to say about one line of codeFor multiple lines of code, tend to be difficult to matchWhich lines does the comment “belong” to?Difficult to say too muchNot much roomMore Commenting “DON’Ts”Don’t include useless commentsAvoid endline commentsDon’t use too many commentsCan actually obscure the code itself!No set “ideal”, but one comment about every 10 lines or so is probably right.More Commenting “DON’Ts”Don’t include useless commentsAvoid endline commentsDon’t use too many
View Full Document