SE 3800 Coding Standard
In addition to correctness, solutions will be graded for style and
design. Additional detail is given below, but here are the key points:
More specifically:
Standards Specific to C and C++ Projects
- File names are to be all lower case. This ensures your projects will
build on Unix as well as Windows.
- Use the extensions .cpp and .h for C++ sources.
- Use -Wall in your C and C++ builds (to generate all warnings)
and ensure your code compiles with no warnings. In most cases, the
warnings indicate issues that make your code unstable.
- Avoid C libraries in C++ programs. In particular, do not
use printf, scanf, atoi, and other C functions
in C++ code. Generally use std::string over C-style
(null-terminated) strings.
- Follow
the standard
structure for header files:
- Use #ifndef...#endif to ensure each header file is processed
at most once in each compilation unit.
- Do not include "using namespace XXX" declarations in header files.
- The first include in an implementation (.cpp or .c) file
needs to be the header file giving the interface. For instance, the
first include for mycontainer.cpp must
be mycontainer.h.
- Include project-specific headers before standard
headers; for instance, mycontainer.h
before stdexcept.
- Use forward declarations (such as class Money;) where
possible to avoid including one header file in another.
- If forward declarations do not work and a piece of code depends
on a particular header, explicitly include that header. For example,
if a class uses string, then #include
<string> in the header file for that class, even though
the header file might include other header files that also
include string.
- Generally separate interfaces from implementations, but it is
acceptable to define short, non-virtual methods in header files.
- In C++, use C++17 constructs where appropriate; for instance,
use nullptr rather than NULL.
Note there is no rule that only one class appear in a particular source
file.
Git Repositories
Some projects will require you to create Git repositories. Standards for
these:
- Do not push build products. In particular, never push
the Debug folder from Eclipse. Every Eclipse project should
include a .gitignore with the content
Debug/
- The repository name needs to be all lower case letters with no
spaces. This facilites processing multiple repositories.
Note that using non-standard build tools generally leads to checking in
build products by mistake. Use the approved build tools.
Note:
By default, Windows is configured to not show extensions such as
".cpp" for file names. This causes problems for engineering
students. See
here
to enable showing extensions.