CMSC 202 Coding Standards C++ Assignment

Added on - 09 Oct 2019

  • 12


  • 2866


  • 91


  • 0


Showing pages 1 to 4 of 12 pages
CMSC 202Coding Standards1.C++ Coding StandardsMost organizations that produce computer code have a set of standards orconventions that programmers are expected to follow. The purpose of thesestandards is make programs readable and maintainable. While no twoorganizations' standards are identical, it is important that all programmerswithin an organization know and follow the same standards. The followingstandards are to be followed in CMSC 202.A portion of every project grade is based upon how well these standards arefollowed. It is your responsibility to understand these standards. If you haveany questions, ask your Instructor or a TA.1.1.File Names and ExtensionsThere are multiple file extensions used to designate C++ source code filesand header files. For this course, the file extensions.cppfor source codefiles and.hfor header files shall be used. No other extensions are permitted.For every project, a file containing themain()function is required. This fileshall be named after the project and have a.cppfile extension. Forexample, themain()function for Project 3 should be in a filenamedproj3.cpp.Auxiliary files (e.gproj3aux.cpp) and header files (e.g.proj3aux.h) arepermitted, but must be named appropriately.The executable for a project shall be named after the project. For example,the executable for Project 3 must be namedproj3. This is very importantbecause the graders will look for that file to run your program. The graderswill not runa.outor any other executable. The executable name iscontrolled by your Makefile — get it right.For most projects, you will be creating your own classes. For these projects,the following standards also apply:CMSC 202 – Computer Science I for MajorsPage1
All C++ classes shall be defined in separate header files, with eachheader file named after the class it contains and having a.hextension.For example, a class that represents a Car object would benamedCarand would be defined in the header fileCar.horcar.h.All C++ classes shall be implemented in separate source code files,with each file named after the class it implements and havinga.cppextension. For example, the implementation of theCarclasswould be contained in the fileCar.cpporcar.cpp.Member functions shall only be implemented in an implementation file.That is, no inline methods are permitted.1.2.File OrganizationEvery header file shall beguardedto prevent multiple inclusions usingthe#ifndef,#define, and#endifpreprocessor directives. Forexample, the header fileFoo.hwould be guarded as follows:#ifndef FOO_H#define FOO_H// Code for Foo.h goes here#endifSource files (header or implementation) shall include all header filesthey require. For example, ifFoo.hrefers toiostreamandstringobjects, it must include theiostreamandstringheaderfiles — it may not rely on other source files to include the headers on itsbehalf.Source files (header or implementation) shallnotinclude any headerfiles they donotrequire. For example, ifFoo.hdoes not refer to anystringobjects, it should not include thestringheader file.1.3.Class DefinitionThe following standards must be followed when a class is defined within it'sheader (.h) file:CMSC 202 – Computer Science I for MajorsPage2
All class names shall begin with an uppercase letter. Multi-word classnames must follow the variable and function naming conventiondescribed below.A class definition shall have at most oneprivate,protected,andpublicsection. Thepublicsection must be first, followed bytheprotectedsection, and lastly theprivatesection.Every data member of a class shall beprivate.Global variables shall not be used; global symbolic constants (const)may be used.Class methods shall follow the function naming conventions givenbelow.Class methods must have completefunction header commentsinthe header file. Fewer comments are required in the.cppfile.Class methods must beconstwhenever possible.Class data members begin withm_and follow the variable namingconventions below. Example:int m_length;int m_nrStudents;1.4.Variable, Constant, and Function DeclarationsThese standards detail how variables and constants should be declared,proper function prototype declarations, and naming conventions.Variables names shall be meaningful and descriptive. For example, ifyour program needs a variable to represent the radius of a circle, callitradius,notrorrad.Variables names shall begin with lowercase letters.Single-letter variables shall not be used, except as simple for-loopindices, temporary pointer variables, and in special cases such as x-and y-coordinates.The use of obvious, common, meaningful abbreviations is permitted.For example,numbercan be abbreviated asnumas innumStudents,or asnras innrStudents.CMSC 202 – Computer Science I for MajorsPage3
If commented, variables shall be declared one per line. Comments mustappear on the same line without wrapping (use two lines if necessary).Separate "words" within variable names with mixed upper andlowercase. For example,grandTotal.Function names shall be separated with mixed upper and lowercase.For example,processError()orprintCheck().Function names should be "active," beginning with a verb and includinga noun, whenever possible. For example,getName()orsetNetPay().Function prototypes shall include parameter names as well as types. Ifdefault values for parameters are provided, they shall be specified inthe prototype.Parameters shall be passed by reference whenever appropriate. Inaddition, parameters passed by reference shall beconstwheneverappropriate.Constants shall be used for "magic" numbers and strings wheneverpossible. For example, even well-known mathematical constants shouldbe given a name:const double PI = 3.14159;. Some constantsfound in formulas, for example Celsius = (5/9)*(Fahrenheit-32), have nomeaningful names and are not considered "magic".1.5.DocumentationThe following sections detail the required program documentation. Failure toadhere to these standards will result in point deductions from your projectsubmission.1.5.1.Use of WhitespaceThe prudent use of whitespace (blank lines as well as spaces) goes a longway to making your program readable.Use blank lines to separate unrelated sections of code and therebygroup logically related lines of code together. For example, the followingcode fragment is difficult to read and points would be deducted:// count total number of studentsfor (int i = 0; i < size; i++)CMSC 202 – Computer Science I for MajorsPage4
You’re reading a preview

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Unlock This Document