Programming Style. Quick Look. Features of an Effective Style. Naming Conventions

Similar documents
Perl Basics. Structure, Style, and Documentation

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

V6 Programming Fundamentals: Part 1 Stored Procedures and Beyond David Adams & Dan Beckett. All rights reserved.

Introduction to Programming Style

COSC345 Software Engineering. Documentation

Excel programmers develop two basic types of spreadsheets: spreadsheets

Exercises: Instructions and Advice

Guidelines for Coding of SAS Programs Thomas J. Winn, Jr. Texas State Auditor s Office

QUIZ Friends class Y;

CSE 143. Programming is... Principles of Programming and Software Engineering. The Software Lifecycle. Software Lifecycle in HW

Programming Assignment IV Due Monday, November 8 (with an automatic extension until Friday, November 12, noon)

File Triage. Work Smarter in Word, Excel, & PowerPoint. Neil Malek, MCT-ACI-CTT+

The compiler is spewing error messages.

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

How To Get Your Word Document. Ready For Your Editor

Introduction to Programming

Type Checking and Type Equality

Racket Style Guide Fall 2017

Reliable programming

Time Saving Programming. John Markham, Janet Albers

1 Getting used to Python

Section 2. Sending s

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

These are notes for the third lecture; if statements and loops.

How to Use Your Autoresponder Series for Maximum Affiliate Profits

1 Robust Database Design. 2 Why is it so difficult? 3 Where does it all start? 3.1 Objects

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Reactive Programming with RxJS 5

Computer Organization & Assembly Language Programming

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Learning to Program with Haiku

An Overview of Visual Basic.NET: A History and a Demonstration

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

First-Order Translation Checklist

Lecture 4 CSE July 1992

CS350 : Operating Systems. General Assignment Information

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Content Curation Mistakes

A Step by Step Guide to Postcard Marketing Success

This chapter is intended to take you through the basic steps of using the Visual Basic

EXAMINING THE CODE. 1. Examining the Design and Code 2. Formal Review: 3. Coding Standards and Guidelines: 4. Generic Code Review Checklist:

CS350 : Operating Systems. General Assignment Information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

High Quality 4D Development. David Adams

Software Design and Analysis for Engineers

Creating Word Outlines from Compendium on a Mac

Functions and Decomposition

If Statements, For Loops, Functions

Week - 01 Lecture - 04 Downloading and installing Python

CSE 230 Intermediate Programming in C and C++ Functions

Tips for Reducing Formula Size

INTRODUCTION. 2

Thanks For Downloading This Special Report!

How to Improve Your Campaign Conversion Rates

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Proofwriting Checklist

Part II Composition of Functions

Lecture 19 CSE August You taught me Language, and my profit on t is I know how to curse. William Shakspere, The Tempest, I, ii.

How to Set up a Budget Advanced Excel Part B

1. Please, please, please look at the style sheets job aid that I sent to you some time ago in conjunction with this document.

CSE 11 Style Guidelines

Intro. Scheme Basics. scm> 5 5. scm>

Secrets of Profitable Freelance Writing

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

10 C Language Tips for Hardware Engineers

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

THINGS. Setup Guide. David Allen Company

Introduction. A Brief Description of Our Journey

A Beginner s Guide to Successful Marketing

3.3 Web Graphics. 1. So why are graphics important?

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Chapter 4: Writing and Designing a Complete Program. Programming Logic and Design, Third Edition Introductory

Federal Plain Language Guidelines

Architectural Documentation 1

A Beginner s Guide to Programming Logic, Introductory. Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

Java By Comparison. Extracted from: Become a Java Craftsman in 70 Examples. The Pragmatic Bookshelf

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Testing is a very big and important topic when it comes to software development. Testing has a number of aspects that need to be considered.

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

CS 370 Statements D R. M I C H A E L J. R E A L E F A L L

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

5. Application Layer. Introduction

Refreshing Your Affiliate Website

CSE 142/143 Unofficial Style Guide

Chamberlin and Boyce - SEQUEL: A Structured English Query Language

Functions: Decomposition And Code Reuse

How to approach a computational problem

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Lab 7: OCaml 12:00 PM, Oct 22, 2017

CS 3 Introduction to Software Engineering. 3: Exceptions

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

Programming Assignment IV

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012

Best Practices for. Membership Renewals

CSS Crash Course for Fearless Bloggers by Gill Andrews

CS112 Lecture: Defining Instantiable Classes

Computing and compilers

The New C Standard (Excerpted material)

Transcription:

Programming Style Quick Look An effective programming style helps you write code that is easier to understand, debug, maintain, and port from system to system. This article discusses the general features of an effective style, and offers specific suggestions for how to develop one. Features of an Effective Style There are a few general features of an effective style. Good programming style produces code that is: Explicit Is always clear what is happening and why. Consistent Uses the same conventions throughout a given system. Modular Written, whenever possible, as if it will need to be moved into another, unrelated system. These general qualities lead to a handful of specific guidelines: Use clear, consistent naming conventions for tables, fields, variables, arrays, forms, methods, semaphores, named selections, and sets. Use a clear, consistent method structure. Include lots of comments. Use white space. Use parameters and few, if any, global variables. Naming Conventions Using consistent names for your variables, arrays, tables, methods, forms and other objects is essential in order to simplify programming and maintenance. It doesn t matter too much what conventions you adopt, as long 1997 David Adams and Dan Beckett 1

as they make sense and you use them consistently. Choose conventions that pack the information you need into your object names. Here are some of the kinds of information you can store in object names: Data type of a variable or array. Membership in a code module. Programmer who created the object. Code to distinguish commands from functions. Method Structure The 4D method editor does not impose any structure on your code. You can make your code easier to read and modify if you adopt a standard structure of your own. Here is a description of the structure we used for all the code in our book Programming 4th Dimension: The Ultimate Guide. Method comments Routine name Routine name and parameters Usage notes Declarations Parameter declarations Variable declarations Initialization Assignment of parameters to local variables Initialization of non-parameter local variables Main code Code of routine This structure makes it easy to know where to find variable declarations, to figure out the parameters a routine uses, and to determine the routine s purpose. Comments Writing comments as you work is a helpful way of developing your algorithm. If your code is too complicated to comment, it s too complicated. Almost no one goes backs and comments later. If you do not comment your code, you make it needlessly difficult for another programmer to work with it. (Six months down the road, chances are you ll be that other programmer.) Comments have no effect on execution speed in a compiled application, so there is no disadvantage to using them liberally. Consider this code: 2 1997 David Adams and Dan Beckett

If (Current date>$1) $0:=$2*$3 $0:=$2 There is no way to determine what this code does, or what the data types of the parameters are. This is a five line routine and the problem is bad enough to make the code completely unreadable. Imagine how much the problem would be magnified in a 200 line routine! Adding comments is the first step in improving this code: Update_Accounts (Date;Real;Real) : Real Update_Accounts (Due date;amount due;interest rate) : New total This routine is applied to all accounts at the end of each month. If the account is past due the customer s interest rate is applied to their balance. If (Current date>$1) $0:=$2*$3 $0:=$2 White Space Dense, compact code is hard to read. Using white space makes routines much easier to read, and does not make them slower. Putting white space above and below major control structures like If, Case of and For makes the code easier on the eye. Breaking code into functional blocks visually distinguishes the method s functional subunits. The following routine adds customer records to a series of sets based on purchase levels and payments. We show two versions of the routine that differ only in their use of white space. 1997 David Adams and Dan Beckett 3

CREATE SET([Customers];"Low purchases") CREATE SET([Customers];"Medium purchases") CREATE SET([Customers];"High purchases") CREATE SET([Customers];"No money received") For ($i;1;records in selection([customers])) GOTO SELECTED RECORD([Customers];$i) If ([Customers]Total Sales=[Customers]Amount_due) ADD TO SET([Customers];"No money received") Case of : ([Customers]Total Sales<500) ADD TO SET([Customers];"Low purchases") : ([Customers]Total Sales<2500) ADD TO SET([Customers];"Medium purchases") ADD TO SET([Customers];"High purchases") End case End for This dense grouping of code is hard to read. It is difficult to determine where the If, For, and Case structures start and stop. Here is the same code with more white space: CREATE SET([Customers];"Low purchases") CREATE SET([Customers];"Medium purchases") CREATE SET([Customers];"High purchases") CREATE SET([Customers];"No money received") For ($i;1;records in selection([customers])) GOTO SELECTED RECORD([Customers];$i) If ([Customers]Total Sales=[Customers]Amount_due) ADD TO SET([Customers];"No money received") Case of : ([Customers]Total Sales<500) ADD TO SET([Customers];"Low purchases") : ([Customers]Total Sales<2500) ADD TO SET([Customers];"Medium purchases") ADD TO SET([Customers];"High purchases") End case End for The white space makes the routine easier to read and the relevant control structures simpler to discern. 4 1997 David Adams and Dan Beckett

Comment Control Structures One of the easiest ways to make your code easier to follow is to comment the ends of control structures, like, End for and End case. When a routine contains nested control structures it becomes difficult to determine which End belongs with which starting statement. Without care you can end up with code like this: For ($LoopCounter;1;Records in selection([customers]) Imagine lots of code here. This For ends with an. 4D will not complain about this problem interpreted, but it will never execute the code in your For loop. (Obviously we never make this kind of mistake. But it, uh happened to, er a friend of ours. Yeah, a friend.) Commenting the end of a control structure with the statement that starts it is a good way to make your code easier to read and debug. Here is the example routine we ve been looking at commented in this way: CREATE SET([Customers];"Low purchases") CREATE SET([Customers];"Medium purchases") CREATE SET([Customers];"High purchases") CREATE SET([Customers];"No money received") For ($i;1;records in selection([customers])) GOTO SELECTED RECORD([Customers];$i) If ([Customers]Total Sales=[Customers]Amount_due) ADD TO SET([Customers];"No money received") ([Customers]Total Sales=[Customers]Amount_due) Case of : ([Customers]Total Sales<500) ADD TO SET([Customers];"Low purchases") : ([Customers]Total Sales<2500) ADD TO SET([Customers];"Medium purchases") Case testing [Customers]Total Sales ADD TO SET([Customers];"High purchases") End case Case testing [Customers]Total Sales ([Customers]Total Sales=[Customers]Amount_due) End for ($i;1;records in selection([customers])) Even in this short routine the control structure comments help reinforce the structure of the routine. In a longer routine comments like these are essential. 1997 David Adams and Dan Beckett 5

Labeled Parameters We recommend that you always copy method parameters into local variables with meaningful names. 4D automatically assigns parameters to local variables $1, $2, and so on. These names are completely meaningless. It takes virtually no time, and in most cases, very little memory, to copy parameters into local variables: $TablePtr:=$1 Apart from legibility, copying parameters to local variables serves two other purposes. First, it makes it much easier to change your parameter list. If you use explicit references to $1, $2, and so on in a routine and then need to modify the parameter list, modifying your existing routine is often tedious and error prone. If you copy parameters into local variables, all you need to do is change your parameter declarations and your assignments. There is no need to edit the code and take a chance on introducing errors. Let s look at the first example rewritten to use labeled parameters: Declare parameters and their label variables: C_REAL($0;$New_total) C_DATE($1;$Due_date) C_REAL($2;$Amount_due) C_REAL($3;$Interest_rate) Assign parameters to labels and initialize result variable: $New_total:=0 This variable is returned in $0 as the result of this function. $Due_date:=$1 $Amount_due:=$2 $Interest_rate:=$3 If (Current date>$due_date) $New_total:=$Amount_due*$Interest_rate $New_total:=$Amount_due $0:=$New_total Even without introductory comments, this routine is highly readable. Declare Everything 4D Compiler can type your variables and arrays for you. This is a bug, not a feature, as far as we are concerned. Mistyped variables and misspelled variable names are by far the biggest cause of programming errors. They are also the least interesting errors you can imagine. So, you should explicitly declare every single variable, array, and parameter yourself using compiler directives. Don t leave the job of typing variables up to 4D Compiler. This cannot be stressed heavily enough. In fact, we re going to say it again. 6 1997 David Adams and Dan Beckett

4D Compiler can type your variables and arrays for you. This is a bug, not a feature, as far as we are concerned. Mistyped variables and misspelled variable names are by far the biggest cause of programming errors. They are also the least interesting errors you can imagine. So, you should explicitly declare every single variable, array, and parameter yourself using compiler directives. Don t leave the job of typing variables up to 4D Compiler. Avoid Global Variables Avoid using global variables (process and interprocess variables) when possible. You can often use parameters instead. Global variables are the cause of many subtle and slippery bugs and make code hard to maintain and more error prone. Global variables are the right way to store state information, such as whether the current process is running in a Web connection. Also, given how 4D works, globals are the only way to work with variable data on forms. On the other hand, globals are the wrong way to pass data between methods in the same process. This is what parameters are for. Use parameters instead of global variables. This is arguably the most important principle in this chapter. Parameters make it much easier to track the flow of stored values through the system. Using them will save you hours when debugging or updating a system. Also, using parameters is essential for creating modular code. Keep Methods Short Long routines are difficult to read, hard to edit, hard to reuse, and take longer to load than several shorter routines of the same overall size. Whenever someone says, I hope ACI fixes the 32K method limit we say, Yes, hopefully they ll make it 8K. Short, modular routines are easy to understand, debug, and deploy for new purposes. Sometimes, however, it makes sense to write a long method. This doesn t mean they re easy to work with: long routines are harder to work with even if they are justified. Develop the habit of writing short routines that perform distinct tasks. Write Design Documentation Method comments are essential, though often inadequate for expressing all the useful design information about a system. If you write formal system specifications, then you should also have full design documentation. Documentation also helps to clarify your thinking about your system, which often improves the design. Even modest programmer documentation is enormously helpful. The kinds of things you should document are: 1997 David Adams and Dan Beckett 7

Special codes. If you use any special codes in your system, always document these thoroughly. Few things are harder to figure out than someone else s codes. Table rules. Whatever rules you define for your tables need to be explicitly documented. Documenting these rules is actually a simple way of designing them. There is no need for anything complicated, just write out the rules narratively if you like. Each customer record requires a unique ID, and cannot be purged unless all of its related invoices are also purged. Structure choices. Ideally, you should document every table, field, and relation in your system. 4D does not provide facilities for this internally, so you need to do it in external documentation. If documenting everything is too ambitious, at least document the complex and non-standard features. Your conventions. Document your naming conventions and other design standards. This makes them easier to understand and easier for a new programmer to begin using. Use Insider Keywords 4D Insider is a great cross-referencing tool, but it doesn t allow you to attach attributes to objects. One simple way to add keywords to code and forms is by using specially named variables. You can define variables used for no other purpose than cross-referencing with 4D Insider. Here, for example, are a few keyword variables used to identify a programmer and the last version of the database the object was modified: <>kwstevejobs:=true <>kwversion321:=true You can include these variables in any code segment, or place them on any form. When you locate one of these variables in 4D Insider, you ll see every place they re used. This is a simple, flexible, lightweight way of implementing simple version control and object keywords in your 4D systems. 8 1997 David Adams and Dan Beckett

The <>kwhelpsystem variable and the objects that use it. 1997 David Adams and Dan Beckett 9

10 1997 David Adams and Dan Beckett