Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Coding Style and Best Practices: Descriptive Names, Whitespace, Braces and Comments, Slides of C programming

The importance of good coding style and provides best practices for naming variables, using whitespace, handling braces, and writing comments. The author emphasizes the benefits of clear, descriptive names, proper indentation, consistent use of braces, and informative comments.

Typology: Slides

2010/2011

Uploaded on 10/06/2011

christina
christina 🇺🇸

4.6

(23)

148 documents

1 / 5

Toggle sidebar

Related documents


Partial preview of the text

Download Coding Style and Best Practices: Descriptive Names, Whitespace, Braces and Comments and more Slides C programming in PDF only on Docsity! 1 Why think about style? • By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race. -- Alfred North Whitehead, An Introduction to Mathematics. • Moral: program with transparent style – Consistent style – Intuitive style Names • Descriptive names for globals, short names for locals – Comment with declaration is useful for (theElementIndex = 0; theElementIndex < numberOfElements; theElementIndex++) elementArray[theElementIndex] = theElementIndex; for ( i = 0; i < nelems; i++) elem[i] = i; vs. •Local variables – e.g., loop variables, temporary variables have conventional short names: int npending = 0; // current length of input queue a = (b + c*d) * (1+e); vs. total = (parts + labor*hourlyRate) * (1 + taxRate); 2 Names (contd) • Consistency important class UserQueue { int noOfItemsInQ, frontOfTheQueue, queueCapacity; public int noOfUsersInQueue() {…} } vs. class UserQueue { int nitems, front, capacity; public int nusers() {…} } Names (contd) • Long lived and important variables should be introduced with a comment stating its function/meaning in the program • Short mnemonics tend to be good for local variables as context helps for these – e.g., “np” rather than “nodepointer” if mnemonic is part of consistent scheme • Longer names for long-lived/global variables with less context surrounding their usage, also something should indicate that they are not local • Consistency: e.g., related vars named “maxphysaddr” and “lowestaddress” is bad style • Variables should be nouns. Functions should be verbs.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved