CS 241 Data Organization Coding Standards



Similar documents
CSE 308. Coding Conventions. Reference

Conditionals (with solutions)

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Moving from CS 61A Scheme to CS 61B Java

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Informatica e Sistemi in Tempo Reale

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

JavaScript: Control Statements I

Introduction to Programming II Winter, 2014 Assignment 2

The programming language C. sws1 1

Install Java Development Kit (JDK) 1.8

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Stacks. Linear data structures

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Sample CSE8A midterm Multiple Choice (circle one)

Top 10 Bug-Killing Coding Standard Rules

Sources: On the Web: Slides will be available on:

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

FEEG Applied Programming 5 - Tutorial Session

Pseudo code Tutorial and Exercises Teacher s Version

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Help on the Embedded Software Block

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Flowchart Techniques

Technical Writing - Definition of Bad and Good Code

PA2: Word Cloud (100 Points)

Lecture 22: C Programming 4 Embedded Systems

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Introduction to Python

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

So far we have considered only numeric processing, i.e. processing of numeric data represented

Official Android Coding Style Conventions

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Code Refactoring and Defects

CISC 181 Project 3 Designing Classes for Bank Accounts

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Decision Logic: if, if else, switch, Boolean conditions and variables

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Software Engineering Techniques

VB.NET Programming Fundamentals

Some Scanner Class Methods

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Software documentation systems

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS106A, Stanford Handout #38. Strings and Chars

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

Tutorial on C Language Programming

J a v a Quiz (Unit 3, Test 0 Practice)

Chapter 1: Key Concepts of Programming and Software Engineering

Chapter 3. Input and output. 3.1 The System class

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Selection Statements

Iteration CHAPTER 6. Topic Summary

Ch 7-1. Object-Oriented Programming and Classes

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

csce4313 Programming Languages Scanner (pass/fail)

C++ INTERVIEW QUESTIONS

A Test Suite for Basic CWE Effectiveness. Paul E. Black.

General Software Development Standards and Guidelines Version 3.5

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Fireworks 3 Animation and Rollovers

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

C++FA 5.1 PRACTICE MID-TERM EXAM

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Cobol. By: Steven Conner. COBOL, COmmon Business Oriented Language, one of the. oldest programming languages, was designed in the last six

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Variables, Constants, and Data Types

Astro Space Center. Profsoyuznaya st., 84/32 Moscow, Russia, USSR. FAX: PHONE: ikimaileesoci..

How to Write a Simple Makefile

Programming Languages CIS 443

Chapter 8 Selection 8-1

13 File Output and Input

Access Queries (Office 2003)

Module 10. Coding and Testing. Version 2 CSE IIT, Kharagpur

An Incomplete C++ Primer. University of Wyoming MA 5310

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

ECE 341 Coding Standard

Chapter 2 Introduction to Java programming

ECE 122. Engineering Problem Solving with Java

CSCI 1301: Introduction to Computing and Programming Summer 2015 Project 1: Credit Card Pay Off

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

A Lex Tutorial. Victor Eijkhout. July Introduction. 2 Structure of a lex file

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Taxi Service Coding Policy. Version 1.2

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

Programming languages C

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

PL / SQL Basics. Chapter 3

Transcription:

CS 241 Data Organization Coding Standards Brooke Chenoweth University of New Mexico Spring 2016

CS-241 Coding Standards All projects and labs must follow the great and hallowed CS-241 coding standards. These standards do not necessarily represent the best nor the only good way to write C code. If you have experience programming, then these standards may not be the standards you are used to using. However, in this class, these are the standards we will use.

Primary Reasons for Defined Standard 1. A standard makes it easier for the instructors to read your code. 2. A class standard makes it easier for a grader to recognize when a program does not use a consistent standard. Often when each student is allowed to define his or her own standard, students switch standards multiple times in a single project. It is tedious for a grader to deduce each persons standard and then check for self-consistency. 3. It is good practice to learn to follow a standard.

Coding Standard: Naming All variables not declared const must start with a lowercase letter. All variables declared with const and #define macro definitions must be all uppercase letters. All program scope variables must be given descriptive names. For example, use wordcount, charcount, and linecount, not wc, cc and lc. Function and block scope variables should usually have descriptive names, but this is not a strict requirement. A character read from a stream is commonly named c. Integer loop indexes are often named i, j, and k.

Coding Standard: Function Comments At the top of every function, there must be a comment block with the following information: /* ************************************ * Each parameter s type and name : * input and / or output, * its meaning, * its range of values. * Function s return value. * Description of what the function * does. * Function s algorithm ************************************ */

Coding Standard: File Comments At the top of.c source file, there must be a comment block with the following information: /* ************************************ * Your first and last name * * Description of what the file * is used for and how to use it. * ************************************ */

Coding Standard Open Brackets Open brackets will be placed at the beginning of a line (not at the end). /* OK */ if (x == 5) y = y +1; /* Not CS - 241 standard */ if (x == 5) y = y +1;

Coding Standard Closing Brackets Closing brackets will be indented on a line with no other commands. The only exception being comments placed on the line with a closing bracket. if (x == 5) y=y +1; /* Comment here OK */ else if ( x == 7) y=y +2; if (x == 5) y=y +1; /* Next line is */ /* not 241 standard */ else if ( x == 7) y=y +2;

Coding Standard Blocks and Whenever a structure spans more than one line, brackets must be used. For example: /* OK */ if (x == 5) y=y +1; /* OK */ if (x == 5) y=y +1; /* Not CS - 241 standard */ if (x == 5) y=y +1;

Coding Standard - Indenting Code blocks will be indented to show the block structure with two spaces per level. Tab characters shall not be used for indenting. All statements within a block must be indented to the same level.

Coding Standard 80 Character Line Max No line shall be more than 80 characters. The best way to avoid overly long statements is by not doing too much in a single statement. /* Way too long! */ if ( getvolume ( length1, width1, height1 ) > getvolume ( length2, width2, height2 )) printf (" box 1 is bigger \n"); else printf (" box 2 is bigger \n"); /* Use temporary variables */ int volume1 = getvolume ( length1, width1, height1 ); int volume2 = getvolume ( length2, width2, height2 ); if ( volume1 > volume2 ) printf (" box 1 is bigger \n"); else printf (" box 2 is bigger \n");

Fixing Too Long a Line Example 2 /* Too long and repeats expression */ if ( stack [ topofstack - 1] == * stack [ topofstack - 1] == + stack [ topofstack - 1] == - stack [ topofstack - 1] == / ) /* Much better! */ char c = stack [ topofstack - 1]; if (c == * c == + c == - c == / ) Another case where a temporary variable can shorten a line and improve readability. Creating the temporary variable c also improves code maintenance: If the code changes so that the comparison needs to check stack[topofstack] or stack[topofstack-2], then the code with a temporary variable only requires a single change while the original requires 4 changes.

Fixing Too Long a Line Example 3 There are times when breaking a long statement in to multiple statements is more awkward than keeping the long statement. In such cases, the statement should be broken in a logical place and each line over which the long statement is continued must be indented. The indenting must be at least 2 spaces, but can be more spaces it that improves readability. The example below, indents the second line so that the comparisons match up. /* long line broken without indenting! */ if ( commandoption == f commandoption == c commandoption == d commandoption == g ) /* indented to line up */ if ( commandoption == f commandoption == c commandoption == d commandoption == g )

Dead Code Elimination Code that is unreachable or that does not affect the program should be eliminated. This includes: Dead stores Variables declared, but never read, #includes never used Functions never called. int foo ( void ) int x, i; /* i is never read */ i = 1; /* dead store */ x = 1; /* dead store */ x = 2; return x; x = 3; /* unreachable */

Replace Needlessly Deep Nestings if (c == * ) multiply (); else if (c == + ) add (); else if (c == - ) subtract (); else if (c == / ) divide (); else error (); if (c == * ) multiply (); else if (c == + ) add (); else if (c == - ) subtract (); else if (c == / ) divide (); else error ();

Avoid Code Duplication Code duplication is when two or more sequences of code are either identical or differ by a small percentage. Code duplication is generally considered a mark of poor or lazy programming style: Contributes to code bulk which interferes with comprehension. Cause update anomalies: Any modification to a redundant piece of code must be made for each duplicate separately. At best, coding and testing time are multiplied by the number of duplications. At worst, some copies may be missed, and bugs thought to be fixed may persist in duplicated locations. Best practice: avoid code duplication with a reusable function or library.

Example of Functionally Duplicate Code int sum1 = 0; int sum2 = 0; for ( int i =0; i <4; i ++) sum1 += array1 [i]; average1 = sum1 /4; for ( int i =0; i <4; i ++) sum2 += array2 [i]; average2 = sum2 /4; The two loops should be rewritten as the single function: int calcaverage ( int array []) int sum = 0; for ( int i =0; i <4; i ++) sum += array [i]; return sum /4;

Keep Function Size Functional Source code, as it appears on the display, becomes an extension of the programmers mind: it is used to organize, remember, and articulate thoughts. It is fine for a list of constants or simple statements to scroll off the screen, but when complex logic spans more lines than fit on the display (usually about 40), then the code becomes difficult for a human to think with. Best practice is to extract logical units from such code and place each unit in a function, even when such functions are only called from one place in the program.

Last Word... Use clean coding standards as you code. Of course, only the end product is graded; however, if you take the time to maintain proper indenting and formatting as you write your code, then you will find coding easier. When you show clean code to an instructor, the instructor is more likely to be friendly and will require less time to help you.

Structs struct Point int x; int y; ; struct Point pt; /* Not CS241 standard */ struct Point int x; int y; pt; struct Point int x; int y; pt; struct Point int x; int y; pt;