260 likes | 429 Vues
10/19/11. TAGD. Schedule. Announcements TWIG Replay Writing Readable Code. Announcement. TWIG. Replay. Writing Readable Code. When writing code, correctness is obviously most important. However, making it readable might be second most important. Why?. Writing Readable Code 2.
E N D
10/19/11 TAGD
Schedule • Announcements • TWIG • Replay • Writing Readable Code
Writing Readable Code • When writing code, correctness is obviously most important. • However, making it readable might be second most important. • Why?
Writing Readable Code 2 • Because one of the main jobs for programmers is maintaining code. • Adding new features • Bug fixing • Etc. • If you don’t write clean code, you will make the above tasks hard. • You will someday forget why you wrote your code the way you did!
“Correct” Code #include <iostream> using namespace std; #define tetmp= //OMDHAF int main(){ char tmp; for(inti=0;i<4;i++){te i%2==0?te 'e':te 'i';cout<<tmp;} cout<<'o'; }
“Correct” Code • Can you tell what that code does? • Possibly, but it’s certainly not easy. • We can definitely do better.
Indentation & Spacing • The compiler can read our code just based upon the syntax. • Technically, we don’t need to space code at all. • Humans can’t read it that way. • Proper spacing is key. • Each statement on one line. • Indent each code block outwards. • Space out statements.
Better Code #define tetmp= //OMDHAF int main() { char tmp; for(intI = 0; I < 4; i++) { teI % 2 == 0 ? te 'e‘ : te 'i'; cout << tmp; } cout << 'o'; }
Defines • Defines are a powerful tool for simplifying how code looks. • But it can also make it harder to read. • How it works: • #define name statement • Now, every time you put the name down, the compiler will turn it into the statement.
Defines part Deux • How can we use it? • Cover up namespaces • #define STRING std::string • Give names to weird constants • #define PI 3.14159 • …as well as even more advanced stuff! • However, use defines sparingly • Try to use c++ constructs instead when possible • e.g. using and const
Even Better Code #define IS_EVEN % 2 == 0 //OMDHAF int main() { char tmp; for(inti = 0; i < 4; i++) { tmp = i IS_EVEN ? tmp = 'e‘ : tmp = 'i'; cout << tmp; } cout << 'o'; }
Operators • You may notice that there’s an odd statement in the middle of the code. • What is it? • A special operator called the conditional. • Conditional operator • Condition ? True_result : False_result • This is basically shorthand for the if statement you are used to.
More Operators • There are 18 classes of operators in C++. • Basic: +, - • Bitwise: &, | • Pointers: ., & • Etc. • It’s a good idea to know all of them. • However, use the odd ones only when necessary. • For example, avoid the conditional for readability.
Even More Better Code #define IS_EVEN % 2 == 0 //OMDHAF int main() { char tmp; for(inti = 0; i < 4; i++) { if(I IS_EVEN) tmp= 'e‘ else tmp= 'i'; cout << tmp; } cout << 'o'; }
Naming • Variable names are an important part of code. • Helps us know what a variable represents. • Choose long, useful names • 8-24 characters is a good length • Name it based on what is stored inside. • User_name, account_number
More about Naming • Don’t name variables “temp” or “tmp” • All variables are temporary • If your variables aren’t, that means that they will be around after your program ends. • This is called a memory leak = bad! • It’s OK to use i, j, k for loops • Established mathematical convention • If you have a better name though, use it.
Yet Even More Better Code #define IS_EVEN % 2 == 0 //OMDHAF int main() { char chorus; for(inti = 0; i < 4; i++) { if(I IS_EVEN) chorus = 'e‘ else chorus = 'i'; cout << tmp; } cout << 'o'; }
Commenting • Even though we can read what our code does now, there’s still a problem. • Why does it do that? • If we had commented it, we’d know. • Comments allow us to leave notes behind. • To both others and ourselves to read. • Can help us remember why we did what we did.
Comments on Commenting • Some good comments to leave include: • On functions: summary, arguments, returns • On classes: summary, data members, member functions • Also good for marking places to come back to later. • Don’t leave comments that don’t help with code readability!
Bestest Code #define IS_EVEN % 2 == 0 //This prints the chorus to “Old McDonald” int main() { char chorus; for(inti = 0; i < 4; i++) { if(I IS_EVEN) chorus = 'e‘ else chorus = 'i'; cout << tmp; } cout << 'o'; }
Code output • If you’re curious, that code we cleaned up prints: • “eieio”
Obfuscation • Sometimes, you might actually want to make code hard to read. • Protect parts of source code. • Annoy your friends! • If you do, just do the opposite of everything I’ve said here. • But you should feel bad doing it.
Homework • Take a really old piece of code you have written and clean it up. • You will also see how hard it is to remember what your code was for. • Send me speaking topics! • Otherwise, I’ll do another boring one like this.