![]() Unfortunately, IDEs like Dev-C++ and Xcode often default to using a mix of tabs and spaces to perform their auto-indentation, which really combines the worst aspects of both methods of indentation. The default tab stop in most Unix editors is set at eight spaces, so if you decide to indent with tabs you should really figure out how to change that setting in your editor. I tend to use tabs when writing my own code but spaces when writing example code for the courses I teach, because I want the code to look the same no matter what editor a student is using to view it. Once again, I don't particularly care which method you use as long as you use it consistently. Programmers disagree violently about which is superior the following scene from the HBO show Silicon Valley S03E06 does a decent job of describing the pros and cons of the two methods: SpacesĬode can be indented using either tabs or spaces. If you get in the habit of always coding them, that mistake won't happen.) Indentation with Tabs vs. In fact, it doesn't hurt to code the braces even if they're not strictly necessary - after all, you might later insert another statement into the loop or decision body and forget to add the braces. For example, an if and its entire body is considered a single "statement" by the compiler. (What constitutes a single "statement" in C++ is sometimes confusing for students, since a "statement" is not necessarily a single line of code. That means the braces around the if body in the examples above are optional. Note that a loop or decision body that contains only a single statement does not need to be delimited by braces. However, the most imoprtant thing is to pick one of these two styles and use it consistently throughout your programs. Here are two common styles of indentation, both of which are acceptable for this class: generally need to be delimited by braces. BracesĬlass definition bodies, function and method definition bodies, loop bodies, decision bodies, etc. Two spaces is the absolute minimum for readability, while more than four spaces is pointless. In my opinion, three or four spaces is optimal. Each nested structure must have its own level of indenting. ![]() should be indented in a consistent fashion. (Or you should also consider the need for it as a reason to factor out the code section into its own function.) Code IndentationĬlass definition bodies, function and method definition bodies, loop bodies, decision bodies, etc. Obviously, use of Section documentation is partly a matter of personal judgment, but programs should include at least a minimal amount of Section Documentation. You should put a blank line before any Section Documentation and indent it the same amount as the code block that it describes. Your programs should use a moderate amount of Section Documentation for functions (such as main()) that are long or have several distinct tasks. Calculate and store averages and standard deviations of measurements. Decide if gizmos or widgets are to be used. Loop to accept and process user-supplied glucose measurements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |