Data Structure and Algorithm Design II
Chapter 1: C++ Development and Language Review
Xingang (Ian) Fang
Sections
C++ Language Review
C++ Language Review
C++ convention and style recommended in this course
Type of errors
C++ Convention in This Course
Claimer: The convention and style is for teaching only. It is not necessarily the best practice
Following a convention makes your learning process easier
Naming convention
Variable, function/method names: lowerCamelCase
Type/Class names: UpperCamelCase
Constant names: ALL_CAPS_WITH_UNDERSCORES
File names: lower-case-with-hyphens, or lower_case_with_underscores
File extensions: .cpp, .hpp
Namespace names: lower_case_with_underscores, a.k.a. snake_case
No capital letters and spaces in file names!
Coding style
Indentation: 2 spaces
Braces: K&R style - opening curly brace on the same line as the statement that opens the block
Spaces: no space before comma, one space after comma
Line length: 120 characters
…
C++ Convention (cont’d)
Conventions in this course (cont’d)
Documentation and comments
You should differentiate documentation and comments
Make it readable, ideally following a style like JavaDoc or Doxygen
Add documentation to essential public class/functions/methods especially when they are part of an API. API related items are usually in your header file so your documentation will be in your header file.
Add to declarations, not definitions.
Do not abuse comments! No need to comment on well-known blocks of code or code that is self-explanatory, or code that has a name.
Generally finished code should have no comments!
C++ standard
Stick to the C++ 14 standard
g++ flag:
-std=c++14
Testing
Some are provided in projects
Strongly recommended to write your own tests
Adding asserts is a simple way to test your code
Types of Errors in C++
Syntax error
A.k.a compilation error
Compiler cannot understand your code
Smart usage of error messages can help you debug but it is not always straightforward
Good IDE/Editor will highlight syntax errors in your editing window but it may require some configuration
Runtime error
Related concept: exception
Compiler can understand your code but the program fails when it is running
Crash, segmentation fault, freeze, etc.
Logic error
Compiler can understand your code and the program runs without any problem, but the program does not do what you want it to do
Problems it causes
Wrong output
Wrong behavior
Waste of resources
too slow
too much memory (possible memory leak)
The most difficult type of error to debug