Function Overloading and Commenting Code

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=g876P_7z06o



Duration: 1:15:05
157 views
3


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.







Tags:
csci 40
function overloading
c++
comments
commenting code