190 likes | 214 Views
Learn how to name code structures and commands effectively, optimize expressions/statements, ensure consistency with idioms, handle magic numbers, and improve commenting techniques. Explore searching techniques, sorting algorithms, library functions, O-notation, lists, and trees.
E N D
C Structures and Commands Mark Canda CS 265
Naming • Descriptive globals and short locals. • Clarity can be achieved though brevity. • Ex. for (theElementIndex = 0; theElementIndex < numberOfElements; theElementIndex++) elementArray[theElementIndex] = theElementIndex; Compared to: for ( i = 0; i < nelems; i++) elem[i] = I;
Naming Cont. • Give all things that are related to one another related names but show their differences and relationship. • Use verbs for functions followed by nouns. • Make sure your names aren’t misleading.
Expressions/Statements • Indenting will make your code much easier to read and organize. • Ex: for(n++;n<100;field[n++]=‘\0’); *i = ‘\0’ ; return(‘\n’); Make it into: for (n++; n < 10; n++) field[n] = ‘\0’ ; *i = ‘\0’ ; return ‘\n’ ;
Expressions/Statements Cont. • Make sure your expressions are in natural form. • Ex: if(!(x < y) || !(x >= z)) Compared to just: if((x >= y) || (x < z)) • Parenthesize even when you don’t have to. This will help to keep certain expressions clear. • Ex: if (x & MASK == BITS)
Expressions/Statements Cont. • You don’t have to cram everything into one line. • Make sure your code is clear. You don’t have to worry about the size of your code as long as it is clear and gets the job done. • Be careful of code that have side effects. • Ex: The operator ++ has side effects. 1. It returns a value 2. Modifies a variable
Consistency/Idioms • Make sure your indentations are consistent and again use braces or parentheses even when they aren’t needed to keep things clear. • Use C/C++ idioms to keep things consistent. • When dealing with multi-way decisions, else if statements are the recommended way to express them, although switch statements may also be used.
Function Macros • Avoid using function macros. They cause a lot of problems and causes bugs in your program that you won’t pick up until later. • In the case that you do use a function macro, just make sure to parenthesize the argument and macro body. • Ex: #define square(x) ((x) * (x))
Magic Numbers • Magic numbers are the constants, array sizes, character positions, conversion factors, and other literal numeric values that appear in programs. • Any number that’s not a 0 or 1 should be given a name. • Don’t use macros to define a number, instead use constants. You may use an enum statement or define it with the use of const.
Magic Numbers Cont. • Use character constants whenever you can instead of using integers. • Ex: Using NULL instead of 0 str = 0; should rather be str = NULL; • You want to leave integers to literally be just integers and use explicit constants. • Use the sizeof operator when determining the size of an object. • Ex: char buf[1024] ; fgets(buf, sizeof(buf), stdin);
Commenting • Don’t state what is obviously happening. Comments are used to clarify the code not recite it. • Comment functions and global variables. Use the comments to explain what’s happening in functions. • If the code is confusing and causes you to write a lengthy comment, rewrite your code. • When your code changes make sure your comments follow and not contradict it.
Searching • Sequential/Linear Search-searches through each element until it finds the one it wants • This type of search is better used for arrays with a few amount of elements. • There are different routines to do sequential searches for different data types. • Ex: strchr, strstr, find algorithms • Binary Search-searches in an orderly way and is much faster than linear search • The elements must be sorted in this type of search.
Sorting • quicksort function-works by partitioning an array into two groups of elements then recursively sorting the two groups • This sorting is the fastest way to sort elements because it doesn’t have to compare all the elements to each other. • There are several different variations of quicksort.
Libraries Functions • qsort- can sort any data type; needs a comparison function whenever comparing two values • The comparison function uses void* pointers since the values can be any data type. • bsearch-can also search any data type and like qsort, besearch needs a pointer to a comparison function
O-Notation • O-notation-the standard notation to compare the running times and space requirements of algorithms independently of programming language and other factors
Lists • Lists are exactly the size they needs to be to hold its contents and can be rearranged by exchanging pointers • Lists are beneficial to use when you have data that is susceptible to change unlike arrays whose data are static. • You will be able to add items, delete items and find specific items in a list.
Trees • tree-a hierarchical data structure that stores a set of items in which each item has a value and is pointed to by exactly one other except for the root • in-order-executes after visiting the left subtree and before visiting the right subtree • post-order-executes on the current node after visiting the children • pre-order-executes on the current node then visits the left subtree before the right
THE END Questions?
Source • Kernighan, Brian W., and Rob Pike. The Practice of Programming. Addison-Wesley, 1999. Print.