1 / 32

Code Formatting

Code Formatting. By: Taylor Helsper. Outline. Introduction Data Types C++ Program Structure Formatting Basics Examples Conclusion Questions. Introduction. What is this lecture? Part of a CSE 4000 Independent Study Course “Practical Issues in Software Engineering” What’s the point?

oberon
Download Presentation

Code Formatting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Code Formatting By: Taylor Helsper

  2. Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions

  3. Introduction • What is this lecture? • Part of a CSE 4000 Independent Study Course • “Practical Issues in Software Engineering” • What’s the point? • To provide practical information to students in Software Engineering topics

  4. Data Types • Int • Short for Integer • It's just a whole number • These are Ints • 10,000 • -1 • 314 • These aren't Ints • 1.1 • "HELLO" • 'f'

  5. Data Types • Float / Doubles • Numbers with decimals! • What's the difference? Doubles are twice as big. • These are Floats/Doubles • 1.1 • 0.0 • 3.14159265 • These Aren't • "HELLO" • 'i'

  6. Data Types • Bool • Short for Boolean • True/False • Any number != 0 equals True • These are Bools • 0 • 1 • True • False • These Aren't • 'HELLO' • 'f'

  7. Data Types • Char • A single letter • These are Chars • 'p' • 'i' • ‘e' • These aren't • "Hello"

  8. Data Types • String • 0 or more characters • These are strings • "I <3 Pie" • "Intermediate is the most amazing class in the history of classes" • "The internet is a series of tubes." • These aren't • 1 • 1.1 • True

  9. Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions

  10. C++ Program Structure • Main Function Name of the function where C++ starts Number of arguments to the program int main(int argc, char** argv) { cout << “Hello World!”; } Strings of the arguments

  11. C++ Program Structure • Arguments Example

  12. C++ Function Structure • Functions Name of the function Function parameters string get_message(int a) { string s = “”; if ( a < 10 ) { s = “Less than 10”; } else { s =“>= 10”; } return s; }

  13. C++ Function Examples • Good functions…

  14. C++ Function Examples • … help code readability

  15. Return Types • Functions are different in C++ than in Python • Return Types must be defined • Definitions look like this • The return the type must always be defined. int function_name( float arg1, bool arg2 ) { return 1; }

  16. Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions

  17. Formatting Basics • Consistency • Very important for readability • Makes debugging much easier • Easier for others to review your code

  18. Formatting Basics • Whitespace • Each indent should represent a new level of logic • Allows code to be read much easier

  19. Formatting Basics • Good Variable Names • Name variables according to what they represent • Long variable names are good if they are helpful

  20. Formatting Basics • Whitespace and Naming Example void f(int n) { int s=0; int e=1; For (int b=0;b<n;b++){ cout << e; e = s + e; s = e; }} void fib(int num) { int start=0; int end=1; for (int n=0; n<num; n++) { cout << end; end = start + end; start = end; } }

  21. Formatting Basics • Comments • Help with debugging • Allow others to more easily understand the code

  22. Outline • Introduction • Data Types • C++ Program Structure • Formatting Basics • Examples • Conclusion • Questions

  23. Examples • Good Code • Bad Code

  24. Examples codequiz.com

  25. Example #1 Write a function that takes one number and returns the square of the number Ex. Input: 2; Returns 4

  26. Example #2 Write a function that takes three numbers and returns the average Ex. avg(double d1, double d2, double d3)

  27. Example #3 Write a function to return the area of a square. The function will take the height and width of the square as parameters.

  28. Example #4 Write a function that takes a number grade and prints the associated letter grade. Ex. 95 prints “A” 80 prints “B”

  29. Conclusion • Be consistent • Formatting helps you and others understand your code • Start good habits now

  30. Questions?

More Related