• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

ADVERTISEMENT

Volume Number: 13 (1997)
Issue Number: 6
Column Tag: Tips & Tidbits

Jun 97 - Tips & Tidbits

by Steve Sisak

Prettyprinting Complex Expressions

The main problem with trying to read a complex expression in a program is trying to discern its structure. When you are writing such an expression, think of it as a tree, with the operators located where the branches join, and the operands attached to them. If you arrange your code to reflect something of this tree structure in two dimensions, the result becomes much easier to understand.

Let's start with a simple example. Compare

IF (NewState > OfState^.NrStates) OR (NewState = 0) AND
	(OfState^.NrStates > 1) THEN

with

IF
		(NewState > OfState^.NrStates)
	OR
		(NewState = 0) AND (OfState^.NrStates > 1)
THEN

Note that the AND-expression and its subexpressions were not broken out onto separate lines, because they are simple enough that their clarity would not be helped by this.

See how gracefully this technique copes with increasing numbers of subexpressions:

RETURN
		(ThePoint.x >= TheRect.left)
	AND
		(ThePoint.x < TheRect.right)
	AND
		(ThePoint.y >= TheRect.top)
	AND
		(ThePoint.y < TheRect.bottom)

Imagine what a mess this would look like all run together:

IF
		NOT EqualFixedRect
			(
				OldEntry^.DeviceIntersect,
				NewEntry.DeviceIntersect
			)
	OR
		NOT BlockEqual
			(
				(*Src1 :=*) ADR(OldEntry^.DeviceMapping),
				(*Src2 :=*) ADR(NewEntry.DeviceMapping),
				(*Count :=*) SIZE(gxMapping)
			)
	OR
		(
			OldEntry^.DeviceBitmap.width 
		<> 
			NewEntry.DeviceBitmap.width
		)
	OR
		(
			OldEntry^.DeviceBitmap.height 
		<> 
			NewEntry.DeviceBitmap.height
		)
	OR
		NOT EqualColorSet
			(
				OldEntry^.DeviceBitmap.set,
				NewEntry.DeviceBitmap.set
			)
	OR
		NOT EqualColorProfile
			(
				OldEntry^.DeviceBitmap.profile,
				NewEntry.DeviceBitmap.profile
			)
THEN

or this one:

IF
			(
					ThisDevice^^.gdPMap^^.pixelSize
			=
					OtherDevice^^.gdPMap^^.pixelSize
			)
	AND
			(
					(QuickDraw.gdDevType IN ThisDevice^^.gdFlags)
			=
					(QuickDraw.gdDevType IN OtherDevice^^.gdFlags)
			)
	AND
			(
					(
							QuickDraw.dontMatchSeeds
					IN
							flags
					)
		OR
					(
							ThisDevice^^.gdPMap^^.pmTable^^.ctSeed
					=
							OtherDevice^^.gdPMap^^.pmTable^^.ctSeed
					)
			)
THEN

Such an arrangement also makes it easier to attach comments explaining what the subexpressions are for:

IF
		(Windows.FrontWindow() = NIL)
			(* no other windows active *)
	AND
		(LastPalette() <> Windows.WindowInFront)
			(* I have palettes *)
THEN

By the way, these are not made-up examples, they are all from actual working code. This isn't about exercises for kiddies in classrooms. It's about real-world programming!

Lawrence D'Oliveiro
LDO@waikato.ac.nz

 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live