Embedded Microcomputer Systems
Embedded Microcomputer Systems ECE 4437
Popular in Course
Popular in Electrical Engineering
This 12 page Class Notes was uploaded by Karolann Wiegand on Saturday September 19, 2015. The Class Notes belongs to ECE 4437 at University of Houston taught by Staff in Fall. Since its upload, it has received 83 views. For similar materials see /class/208312/ece-4437-university-of-houston in Electrical Engineering at University of Houston.
Reviews for Embedded Microcomputer Systems
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/19/15
v2 C Code Conventions for ECE 4436 amp 4437 adapted from Code Conventions for the Java Programming Language httpjavasuncomdocscodeconv 1 Introduction 11 Why Have Code Conventions Code conventions are important to programmers for a number of reasons 80 of the lifetime cost of a piece of software goes to maintenance Hardly any software is maintained for its whole life by the 01iginal author Code conventions improve the readability of the software allowing engineers to understand new code more quickly and thoroughly If you ship your source code as a product you need to make sure it is as well packaged and clean as any other product you create 4 Indentation Four spaces should be used as the unit of indentation 41 Line Length Avoid lines longer than 80 characters since they39re not handled well by many terminals and tools Note Examples for use in documentation should have a shorter line length 7 generally no more than 70 characters 42 Wrapping Lines When an expression will not fit on a single line break it according to these general principles Break after a comma Break before an operator Prefer higherlevel breaks to lowerlevel breaks Align the new line with the beginning of the expression at the same level on the previous line v2 If the above rules lead to confusing code or to code that s squished up against the right margin just indent 8 spaces instead Here are some examples of breaking function calls someFunctionlongExpressionl longExpressionZ longExpression3 longExpression4 longExpressionS var someFunctionlongExpressionl someFunctionZlongExpressionZ IongExpression3 Following are two examples of breaking an arithmetic expression The first is preferred since the break occurs outside the parenthesized expression which is at a higher level longNamel longName2 longName3 longName4 longNameS 4 Iongname6 PREFER longNamel longName2 longName3 longName4 longNameS 4 Iongname6 AVOID Following are two examples of indenting function declarations The first is the conventional case The second would shift the second and third lines to the far right if it used conventional indentation so instead it indents only 8 spaces CONVENTIONAL INDENTATION someFunctionint anArg int anotherArg float yetAnotherArg double andStillAnother INDENT 8 SPACES TO AVOID VERY DEEP INDENTS static void horkingLongFunctionNameint anArg int anotherArg float yetAnotherArg double andStillAnother Line wrapping for if statements should generally use the 8space rule since conventional 4 space indentation makes seeing the body difficult For example DON39T USE THIS INDENTATION if conditionl ampamp conditionZ ll condition3 ampamp conditionM lllcondition5 ampamp condition6 BAD WRAPS doSomethingAboutIt MAKE THIS LINE EASY TO MISS USE THIS INDENTATION INSTEAD if conditionl ampamp conditionZ ll condition3 ampamp condition4 lllcondition5 ampamp condition6 doSomethingAboutIt v2 OR USE THIS if conditionl ampamp conditionZ ll condition3 ampamp conditionM lllcondition5 ampamp condition6 doSomethingAboutlt Here are three acceptable ways to format ternary expressions alpha aLongBooleanExpression beta gamma alpha aLongBooleanExpression beta gamma alpha aLongBooleanExpression beta gamma 5 Comments C programs can have two kinds of comments those delimited by and Comments should be used to give overviews of code and provide additional information that is not readily available in the code itself Comments should contain only information that is relevant to reading and understanding the program For example information about how the program is built or in what directory it resides should not be included as a comment Discussion of nontrivial or nonobvious design decisions is appropriate but avoid duplicating information that is present in and clear from the code It is too easy for redundant comments to get out of date In general avoid any comments that are likely to get out of date as the code evolves NoteThe frequency of comments sometimes re ects poor quality of code When you feel compelled to add a comment consider rewriting the code to make it clearer 51 Comment Formats Programs can have four styles of comments block singleline trailing and endofline v2 511 Block Comments Block comments are used to provide descriptions of les functions data structures and algorithms Block comments may be used at the beginning of each le and before each function They can also be used in other places such as within functions Block comments inside a function should be indented to the same level as the code they describe A block comment should be preceded by a blank line to set it apart from the rest of the code Here is a block comment A 512 SingleLine Comments Short comments can appear on a single line indented to the level of the code that follows If a comment can t be written in a single line it should follow the block comment format A singleline comment should be preceded by a blank line Here s an example of a single line comment if condition Handle the condition 513 Trailing Comments Very short comments can appear on the same line as the code they describe but should be shifted far enough to separate them from the statements If more than one short comment appears in a chunk of code they should all be indented to the same tab setting Here s an example ofa trailing comment in C code ifa i return TRUE special case else return isPrimea works only for odd a 514 EndOfLine Comments The comment delimiter can comment out a complete line or only a partial line It shouldn t be used on consecutive multiple lines for text comments however it can be v2 used in consecutive multiple lines for commenting out sections of code Examples of all three styles follow if foo gt 1 Do a double flip else return false Explain why here if bar gt 1 Do a triple flip else return false 7 Statements 71 Simple Statements Each line should contain at most one statement Example argv Correct argC Correct argv argC AVOIDl 72 Compound Statements Compound statements are statements that contain lists of statements enclosed in braces quot statements quot See the following sections for examples The enclosed statements should be indented one more level than the compound statement The opening brace should be at the end of the line that begins the compound statement the closing brace should begin a line and be indented to the beginning of the compound statement Braces are used around all statements even single statements when they are part of a control structure such as a if else or for statement This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces v2 73 return Statements A return statement with a value should not use parentheses unless they make the return value more obvious in some way Example return return defaultsize return size size defaultsize 74 if ifelse if elseif else Statements The if else class of statements should have the following form if condition statements if condition statements else statements if condition statements else if condition statements else statements Note if statements always use braces Avoid the following errorprone form if condition AVOIDl THIS OMITS THE BRACES H statement 75 for Statements A for statement should have the following form for initialization condition update statements An empty for statement one in which all the work is done in the initialization condition and update clauses should have the following form v2 for initialization condition updateL When using the comma operator in the initialization or update clause of a for statement avoid the complexity of using more than three variables If needed use separate statements before the for loop for the initialization clause or at the end of the loop for the update clause 76 While Statements A while statement should have the following form while condition statements An empty while statement should have the following form while condition 77 doWhile Statements A do while statement should have the following form do statements while condition 78 switch Statements A switch statement should have the following form switch condition case ABC statements falls through case DEF statements break case XYZ statements break default statements break v2 Every time a case falls through doesn t include a break statement add a comment where the break statement would normally be This is shown in the preceding code example with the falls through comment Every switch statement should include a default case The break in the default case is redundant but it prevents a fallthrough error if later another case is added 8 White Space 81 Blank Lines Blank lines improve readability by setting off sections of code that are logically related Two blank lines should always be used in the following circumstances Between sections of a source file One blank line should always be used in the following circumstances Between functions Between the local variables in a function and its rst statement Before a block or singleline comment Between logical sections inside a function to improve readability 82 Blank Spaces Blank spaces should be used in the following circumstances A keyword followed by a parenthesis should be separated by a space Example while true Note that a blank space should not be used between a function name and its opening parenthesis This helps to distinguish keywords from function calls A blank space should appear after commas in argument lists All binary operators except should be separated from their operands by spaces Blank spaces should never separate unary operators such as unary minus increment quotquot and decrement quotquot from their operands Example v2 acd aabCd while d s n printsizequotsize is quot foo quotnquot The expressions in a for statement should be separated by blank spaces Example for exprl expr2 expr3 Casts should be followed by a blank space Examples myFunction byte aNum double x myFunctionint cp 5 int i 3 l 9 Naming Conventions Naming conventions make programs more understandable by making them easier to read They can also give information about the function of the identifierfor example whether it39s a constant variable define which can be helpful in understanding the code Identifier Type Rules for Naming Examples Functions should be verbs in mixed case with the run Functions first letter lowercase with the first letter of each runFast internal word capitalized getBackground Variables are in mixed case with a lowercase rst int i letter Internal words start with capital letters char C Variable names should not start with underscore 7 f loat HIEWidth or dollar sign characters even though both are Variables v2 allowed Variable names should be short yet meaningful The choice of a variable name should be mnemonic that is designed to indicate to the casual observer the intent of its use Onecharacter variable names should be avoided except for temporary quotthrowawayquot variables Common names for temporary variables are i j k m and n for integers c d and e for characters static int MIN7WIDTH 4 The names of variables declared constants and of static int ANSI constants should be all uppercase with words MAX7WIDTH 1 J by J quot7quot ANSI constants 999 should be avoided for ease of debugging Constants static int GETiTHEiCPU 1 10 Programming Practices 101 Providing Access to Variables in a Module Don39t make any variable global without good reason Often variables don t need to be explicitly set or gotten o en that happens as a side effect of function calls 103 Constants Numerical constants literals should not be coded directly except for l 0 and l which can appear in a for loop as counter values 104 Variable Assignments Avoid assigning several variables to the same value in a single statement It is hard to read Example fooBarfChar barFoo1char 39C39 AVOIDI v2 Do not use the assignment operator in a place where it can be easily confused with the equality operator Example if c d AVOIDl should be written as if c d l O Do not use embedded assignments in an attempt to improve runtime performance This is the job of the compiler Example d a b c r AVOIDl should be written as a b C d a r 105 Miscellaneous Practices 1051 Parentheses It is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems Even if the operator precedence seems clear to you it might not be to othersyou shouldn t assume that other programmers know precedence as well as you do if a b m c AVOIDI if a ampamp c RIGHT 1052 Returning Values Try to make the structure of your program match the intent Example if booleanExpression return true else return false should instead be written as return booleanExpression v2 Similarly if condition return x return y should be written as return condition 2 x y 1053 Expressions before 239 in the Conditional Operator If an expression containing a binary operator appears before the in the ternary operator it should be parenthesized Example x gt O x x 1054 Special Comments Use xxx in a comment to ag something that is bogus but works Use FIXME to ag something that is bogus and broken
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'