Function Overloading and Commenting Code
Today we hit up two big topics:
1) How to comment your code properly.
2) How to overload a function
Commenting is one of those style things that reasonable people disagree on and yet are often unreasonable in their disagreement. There's only a couple cardinal sins that I will set down for this class:
A) Don't do comments that just repeat what the code says:
int x = 6; //This creates an integer named x, and initializes it to 6
B) Every function and big block of code should have a short note explaining at a high level what it is doing. For example:
//This finds the shortest path to all cities in America
void dijkstra() { ... }
C) If you ever do something that would cause a reasonable person to ask a question about it, like "what the hell does 'npttf' mean??" that's a sign you need a comment there. It's the WTF standard - any time someone looks at your code and says WTF, you probably need a comment there. For example, this code...
a = a ^ b;
b = a ^ b;
a = a ^ b;
...is baffling to anyone who has not seen it before. So you should really put a comment in there explaining you're doing a Swap operation without a temp.
D) If something will break your code if changed, 100% put a giant warning sign on it in a comment: //WARNING: IF YOU CHANGE THIS VALUE EVERYTHING WILL BREAK! DO NOT TOUCH!
Maybe it's a sign your code shouldn't be written like that, but hey. The warning is appreciated.
Second, we talked about function overloading, specifically how C++ allows functions with the same name but different parameter types to exist at the same time. (This is different from C, in which no function names can be the same.) We ran through the rules at a high level of how C++ picks one function to call over the other. Basically, it tries to pick the closest match, but the rules for this can become obscure, so try to avoid situations where you're writing code that requires a C++ trivia expert to figure out which function it will call.
We finished by talking about the homework, and doing a doubly-nested for loop with strings.
Other Videos By Bill Kerney
2021-09-17 | Making Video Games Interactive + Blueprints Coding |
2021-09-17 | Doubly Nested Loops + Chars + Strings |
2021-09-17 | Ethical Theories I |
2021-09-17 | Introducing Colorslib |
2021-09-16 | Voronoi Relaxation / Lloyd's Algorithm |
2021-09-15 | How to Approach Writing Code + Lvalues vs. Rvalues |
2021-09-15 | Truth Tables II + Denying the Antecedent + Affirming the Consequent |
2021-09-14 | Under the Hood I + Lighting + Special Effects in UE4 |
2021-09-14 | Introducing Readlib |
2021-09-14 | C++ I/O and Digital Logic |
2021-09-13 | Function Overloading and Commenting Code |
2021-09-13 | Truth Tables - AND, OR, NOT |
2021-09-11 | Call by Reference |
2021-09-10 | Modus Ponens and Modus Tollens |
2021-09-10 | SVN, Voronoi |
2021-09-10 | GDC Talks, Landscape III, Material Functions |
2021-09-10 | Combinatorics, Inclusion/Exclusion |
2021-09-08 | Functions Part II |
2021-09-08 | Arguments + Invalid/Valid/Sound |
2021-09-07 | Voroni |
2021-09-07 | CSG, Sprinting, Water (Old and New) |