CMSC 202 Coding Standards | Assignment


Added on  2019-09-30

12 Pages2619 Words461 Views
CMSC 202 Coding Standards1.C++ Coding StandardsMost organizations that produce computer code have a set of standards or conventions that programmers are expected to follow. The purpose of these standards is make programs readable and maintainable. While no two organizations' standards are identical, it is important that all programmers within an organization know and follow the same standards. The following standards are to be followed in CMSC 202.A portion of every project grade is based upon how well these standards are followed. It is your responsibility to understand these standards. If you have any questions, ask your Instructor or a TA.1.1.File Names and ExtensionsThere are multiple file extensions used to designate C++ source code files and header files. For this course, the file extensions.cppfor source code files and.hfor header files shall be used. No other extensions are permitted.For every project, a file containing themain()function is required. This file shall be named after the project and have a.cpp file extension. For example, themain()function for Project 3 should be in a file namedproj3.cpp.Auxiliary files (e.gproj3aux.cpp) and header files (e.g.proj3aux.h) are permitted, 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 important because the graders will look for that file to run your program. The graders will not runa.outor any other executable. The executable name is controlled by your Makefile — get it right.For most projects, you will be creating your own classes. For these projects, the following standards also apply:All C++ classes shall be defined in separate header files, with each header file named after the class it contains and having a.hextension.CMSC 202 – Computer Science I for MajorsPage 1

For example, a class that represents a Car object would be namedCarand 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 having a.cppextension. For example, the implementation of theCarclass would be contained in the fileCar.cppor car.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 using the#ifndef,#define, and#endifpreprocessor directives. For example, 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 files they require. For example, if Foo.h refers to iostream and stringobjects, it must include the iostream and string header files — it may not rely on other source files to include the headers on itsbehalf.Source files (header or implementation) shall not include any header files they donotrequire. For example, if Foo.h does not refer to any string objects, it should not include the string header file.1.3.Class DefinitionThe following standards must be followed when a class is defined within it's header (.h) file:All class names shall begin with an uppercase letter. Multi-word class names must follow the variable and function naming convention described below.CMSC 202 – Computer Science I for MajorsPage 2

A class definition shall have at most oneprivate,protected, andpublicsection. Thepublicsection must be first, followed by theprotectedsection, 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 given below.Class methods must have completefunction header commentsin the header file. Fewer comments are required in the.cppfile.Class methods must beconstwhenever possible.Class data members begin withm_and follow the variable naming conventions 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, if your program needs a variable to represent the radius of a circle, call itradius,notrorrad.Variables names shall begin with lowercase letters.Single-letter variables shall not be used, except as simple for-loop indices, 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 in numStudents, or asnras innrStudents.If commented, variables shall be declared one per line. Comments mustappear on the same line without wrapping (use two lines if necessary).CMSC 202 – Computer Science I for MajorsPage 3

Separate "words" within variable names with mixed upper and lowercase. 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 including a noun, whenever possible. For example, getName() or setNetPay().Function prototypes shall include parameter names as well as types. If default values for parameters are provided, they shall be specified in the prototype.Parameters shall be passed by reference whenever appropriate. In addition, parameters passed by reference shall be constwhenever appropriate.Constants shall be used for "magic" numbers and strings whenever possible. For example, even well-known mathematical constants shouldbe given a name:const double PI = 3.14159;. Some constants found 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 to adhere to these standards will result in point deductions from your project submission.1.5.1.Use of WhitespaceThe prudent use of whitespace (blank lines as well as spaces) goes a long way to making your program readable.Use blank lines to separate unrelated sections of code and thereby group 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++) totalStudents += class[i];cout << "total students = " << total students CMSC 202 – Computer Science I for MajorsPage 4

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
CMSC 202 Coding Standards C++ Assignment

Object Oriented Programming Assignment: Championship Simulation