Download presentation
Presentation is loading. Please wait.
Published byAllen Jordan Modified over 9 years ago
1
W 1 L 1 sh 1 TCTI-V2CCPP1-10 C en C++ Programmeren Daniel Telgen Wouter van Ooijen Site: https://cursussen.sharepoint.hu.nl/fnt/8/TCTI-V2CCPP1-10/default.aspx
2
W 1 L 1 sh 2 Boeken Boeken: In zee met C L.v.Moergestel 978-9039524794 C++ from the Beginning J. Skansholm (2nd Edition) 978-0201721683 These books are highly recommended, but they are not strictly needed.
3
W 1 L 1 sh 3 lesprogram Week 1: 1. objects, names 2. statements, layout Week 2: 1. functions, decomposition 2. lists, specifications Week 3: 1. memory, testing 2. Fagan inspection Week 4: 1. class, copy-constructor 2. operators1 Week 5:3. operators2 4. inheritance Week 6: 5. virtual methods 6. exceptions, trial exam C C++ good programming practices
4
W 1 L 1 sh 4 C lessons LessonSubjectBook Week 1 lesson 1Objects, names Week 1 lesson 2Statements, layout Week 2 lesson 1Functions, decomposition Week 2 lesson 2Lists, specification Week 3 lesson 1Memory, testing Week 3 lesson 2Fagan inspection
5
W 1 L 1 sh 5 Assignments Read the ‘overzicht’ document! Summary: Individual Fixed deadline: 24:00 before next week’s lesson By email, fixed header, one file attached Each assignment is rated Ratings are averaged No second chance !
6
W 1 L 1 sh 6 Code exist only to be run? #define P(X)j=write(1,X,1) #define C 39 int M[5000]={2},*u=M,N[5000],R=22,a[4],l[]={0,-1,C-1,-1},m[]={1,-C,- 1,C},*b=N, *d=N,c,e,f,g,i,j,k,s;main(){for(M[i=C*R- 1]=24;f|d>=b;){c=M[g=i];i=e;for(s=f=0; s =0&&k =16!=M[k]> =16))a[f++ ]=s;if(f){f=M[e=m[s=a[rand()/(1+2147483647/f)]]+g];j=j<f?f:j;f+=c&- 16*!j;M[g]= c|1 b++?b[-1]:e;}P(" ");for(s=C;--s;P("_") )P(" ");for(;P("\n"),R--;P("|"))for(e=C;e--;P("_ "+(*u++/8)%2))P("| "+(*u/4)%2 );} http://www.de.ioccc.org/main.html
7
W 1 L 1 sh 7 Code is there to be read! n Code is n written once, n re-written a few times, n read many many times. n Reading starts immediately after you type in the first version.. Most of the cost is in reading, not in writing. To lower software cost: make reading easier, event when that takes more effort (cost) in the writing.
8
W 1 L 1 sh 8 How to make code readable n Names (today) n Names must help the reader to understand the code. n Format (layout) n The layout must match the statement structure. n The layout must be consistent. n Comments n Comments must explain what the reader needs to know but can not easily read from the code. n Structure n The code must be structured to be understandable. n Known concepts n Use concepts and techniques that are familiar (datastructures, patterns, real-world analogues).
9
W 1 L 1 sh 9 Things in C that have names (identifiers) n Variable n Function n Typedef n Parameter n Struct n Struct element n Macro n Enumerate n (Label) n (File) Compilation only (unique per file) Compile and link (can be shared)
10
W 1 L 1 sh 10 C compilation process : separate files, separate objects #define MESSAGE ”Hello” int main( void){ printf( "m [%s ] @ %X\n", MESSAGE, MESSAGE ); f(); } #define MESSAGE ”Hello” void f( void ){ printf( "f [%s ] @ %X\n", MESSAGE, MESSAGE ); } compile link main.c msg.c msg.omain.o stdlib.lib main.exe main.hex main
11
W 1 L 1 sh 11 C compilation process : use a header file to share text #include ”msg.h” int main( void){ printf( "m [%s ] @ %X\n", MESSAGE, MESSAGE ); f(); } #include ”msg.h” void f( void ){ printf( "f [%s ] @ %X\n", MESSAGE, MESSAGE ); } compile link main.c msg.c msg.omain.o stdlib.lib main.exe main.hex main #define MESSAGE "Hello" msg.h Now only one place to maintain!
12
W 1 L 1 sh 12 The multiple inclusion problem #include ”message.h”... #include ”basics.h”... #include ”basics.h” #include ”message.h”... main.c message.h message.c typedef unsigned char byte;... basics.h
13
W 1 L 1 sh 13 #ifndef basics_h #define basics_h typedef unsigned char byte;... #endif Avoiding the multiple inclusion problem #include ”message.h”... #include ”basics.h”... #include ”basics.h” #include ”message.h”... main.c message.h message.c basics.h This is an idiom.
14
W 1 L 1 sh 14 Per-compilation named things n Typedef n Parameter n Struct n Struct element n Macro n Enumerate In each compilation unit, each such thing must be defined once before it is used. When you want to share a definition between files you put it in a header file (never cut-n-paste!!). Header files use the #ifdef/#define/#endif idiom to prevent multiple-inclusion problems.
15
W 1 L 1 sh 15 C compilation process: sharing a run-time object extern char *msg; int main( void ){ printf( ”%s\n”, msg ); } char *msg = ”Helllo world”; compile link main.c msg.c msg.omain.ostdlib.lib main.exe main.hex Hello world main
16
W 1 L 1 sh 16 C compilation process: sharing a run-time object extern char *msg; int main( void ){ printf( ”%s\n”, msg ); } int msg = 0x1234; compile link main.c msg.c msg.omain.ostdlib.lib main.exe main.hex main To the linker a name is just an address.
17
W 1 L 1 sh 17 C run-time named things n Variable n Function Can be n Declared : state that it exist (provide the external properties: type, prototype) n Defined : declare and create it (provide code, room, initial data, etc) n Used
18
W 1 L 1 sh 18 C run-time named things Can be n Declared : state that it exist n Defined : declare and create it n Used Rules: 1. In each compilation unit, each object must be declared before it is used. 2. In each application, each object that is used must be defined once. 3. Declarations must be in header files, header files must use the #ifndef idiom.
19
W 1 L 1 sh 19 Variable n Variable n Global, outside a function, or n Local, within a function n By default it is a definition, extern makes it a declaration n Function n Typedef n Parameter n Struct n Struct element n Macro int saved_x; extern int x_saved_counter; void save_x( int a ){ saved_x = a; x_saved_counter++; } unsigned int power_of_two( unsigned int pow ){ unsigned int i, result = 1; for( i = 0; i < pow; i++ ){ result = 2 * result; } return result; }
20
W 1 L 1 sh 20 Function n Variable n Function n With a body it is a definition n Header-only is a declaration n Typedef n Parameter n Struct n Struct element n Macro unsigned int strlen( const char * p ){... } unsigned int power_of_two( unsigned int pow ); void f( void ){ int n, x; n = strlen( ”Hello world” ); x = power_of_two( n ); }
21
W 1 L 1 sh 21 Reading a small function void f( int a, int b ){ g( a ); while( b-- ){ (void) h(); } n What does this function do? n Why does it what it does? n Is it correct? n When and why should you call it? n What parameter values should you pass to it?
22
W 1 L 1 sh 22 Now try again void ps2_command_send_replies_ignore( int command unsigned int n_replies ){ ps2_byte_send( command ); while( n_replies-- ){ (void) ps2_byte_receive(); } n What does this function do? n Why does it what it does? n Is it correct? n When and why should you call it? n What parameter values should you pass to it?
23
W 1 L 1 sh 23 Choosing names n Old code (and old interfaces!) often used very short names. You know why? (NOT because they liked short names) n Names must help the reader to understand the code. n (note: the programer himself is the first reader!) What's in a name? that which we call a rose By any other name would smell as sweet;
24
W 1 L 1 sh 24 Guiding principles n Use english. n A thing must be a noun. n A pure function (no side effects) must be a noun. n A function that achieves something (side effects) must be a verb. Predicates must be constin ‘is’ ( is_even(), is_authorized(), is_active_alarm(), string_is_valid_integer() ). n A name must document its interface (use), not its internals (implementation). n Multi-word names: n The words must be separated. n The order of the words is larger-thing-first.
25
W 1 L 1 sh 25 Choosing a name When applicable and/or needed: n Tell what it is or does. (print, count, select) n Tell what it returns. (sum, average, first, key, password) n Tell what it requires (parameters: width, xy, name, list) n Tell what type it returns (char, short, int, long) n Tell what types it needs (char, string)
26
W 1 L 1 sh 26 What is wrong? x = lowerchar( y ); n Lower and char are not separated n Name does not contain a verb What does this function do, 1. Test whether y is a lowercase character? 2. Converter y to a lowercase character? Q: What would be a good name in each of the two cases?
27
W 1 L 1 sh 27 Separating the words in a name n Most programming languages do not allow spaces in identifiers. n Justputitalltogetherwithoutseparatorsisnotagoodidea n INTHEBEGINNINGPRINTERSHADNOLOWERCASE n camelCaseIsNamedAfterTheHumpsOfACamel n PascalCaseAlsoCapitalizesTheFirstLetter n Adding_Underscores_Makes_Reading_Easier n but_with_underscores_who_needs_the_captials n Use capitals (only) when needed: TCP_checksum_calculate() Use lowercase and underscores, uppercase only when needed. Don’t use a leading underscore, this is often used to distinguish a ‘special name’. http://en.wikipedia.org/wiki/Naming_conventions_(programming)
28
W 1 L 1 sh 28 Libnds keys input functions Comments please! http://libnds.devkitpro.org ; select “ Keypad and Touch pad ”
29
W 1 L 1 sh 29 ‘errors’ n camelCase. n Keys... except for scanKeys (but touchRead does have the verb last!) n keysDownRepeat should not contain a verb! n keysSetRepeat set should be last uint32 keys_current( void ); uint32 keys_down( void ); uint32 keys_down_or_repeating( void ); uint32 keys_held( void ); void keys_repeat_set_delay_interval( u8 delay, u8 interval ); uint32 keys_up( void ); void keys_scan( void ); void touch_read( touch_position *position ); n Do you see what those int32’s mean?? n The explanations are not realy helpful
30
W 1 L 1 sh 30 Read Book n Chapter 4 n Chapter 11 n Paragraph 17.5
31
W 1 L 1 sh 31 Assignment Go to http://palib-dev.com/manual.html. Make a report (in word) with a three-column table of all function in the following sections. Column one contains the original function name, column two contains the name you would give the functuion, according to the naming conventions exmplaing in the lesson, column three contains your motivation for choosing that name. You might need to check the documentation of a function to find out what it realy does. Sections: 1. Special Controllers 2. Key input system 3. Sound and MP3 functions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.