Author : Drew Sikora
Page : 1 Next >>
by Drew Sikora
Introduction
After much procrastination, I finally got around to purchasing Code Complete at my local Barnes & Noble (gotta love that store...). Of course, in my usual habit of digesting about 4-5 computer books at once, I've only read about 125 pages. Anyway I was cruising through Section 4 on building routines when I came across something called PDL. Reading further I was amazed that someone had actually taken this technique and named it for professional use. We'll get to what PDL is in a moment, but first a little background.
Before this article I also wrote The Art of Code Documentation, which, I can't help but say, got rave reviews including an email from a guy at LithTech saying how they needed more people who followed these rules in the industry. If you read this article and disagree, please, please email me and let me know why. This includes all my other articles. I am extremely open to critiscm and have a seperate email account for my articles, so don't be afraid to clutter up my inbox. Anyway in this article I laid down the basics of how to comment and then document your code so others could read it. The best part about this article is (I think) the fact that it was from experience only, rather than a take-off of a section of a book, like this is. I highly suggest you read it first - I won't directly reference anything from it, but I will assume you know, are familiar with, and accept common documenting practices.
Now that we are all ready, let's begin!
What is PDL?
What is PDL? Well that's a reasonable enough question. I sure as hell had no idea what PDL was when I first started reading about it. In fact, the meaning of its name can be quite misleading. PDL stands for Program Design Language. Now you may notice how short this article is if you glance to the menu on the right. Obviously you can assume that PDL really isn't a full-fledged language with it's own functions and variables and such - no way could I teach it in such a simple lesson. There is a reference you can make to PDL - it is a common coding technique where you write out code in plain english, known as pseudocode, or fake code (literally). That's exactly what PDL is.
When we talk about high-level languages, we talk about languages where the instructions you type represent English (as an example) more and more. Believe it or not, but QBasic is a higher-level language than C++! In case you don't understand this, I'll sidetrack just a bit to make sure everyone is clear. When we reference languages in terms of levels, the reference is the English language. The closer the code comes to looking like English, the "higher" up it is. If we were to compare ways to print a word on the screen in C++ and QBasic, you'll realize why QBasic is a higher language than C++:
C++ - cout << "hello world" << endl;
QBasic - PRINT "hello world"
You tell me which is the higher level language. Obviously PRINT is much, much closer to the English word (okay, so it's exact) than cout is. To be official, the ratio of high-level-language statements to equivilent assembly code in QBasic is 1:5, while in C++ it's only 1:2.5
Okay, enough delineating - back to the main topic. So as I was saying, PDL is the highest-level language you will ever see until they conceive computers that can interpret human speech as code. When you code in PDL, you are coding in plain English (or whatever language you use) and not one single bit of computer code. In fact, this ambiguity let's you code PDL and then use it to construct a routine in any computer language since you never used a peice of language specific code. Wonderful! But how the hell is all this supposed to help you? Nothing could ever run it! Be patient, I haven't instructed you on how to use it yet, have I?
Using PDL Correctly
Like anything else in this freakish world, PDL can be easily abused. How sad. But to avoid this tragedy I'll cue you in on what to do and what not to do. Let's kick off this section by dumping you with an example of using PDL, then I'll break it down for you to understand, pointing out do's and dont's along the way. Shall we?
Here's a little excercise - look at examples A and B and see if you can discern which PDL code is correct and which is not.
Example A
Lock the drawing surface to prevent access by other programs and check for errors.
Get mem_pitch of the surface using (int)ddsd.lPitch.
Get *video_buffer to draw to using ddsd.lpSurface.
if pixel_mode == random then
Create three random values for x, y and color.
else if pixel_mode == linear then
Increment x value by 1.
Create a random value for color.
end if
video_buffer[x+y*mempitch] = color
Unlock the surface and check for errors.
return 1
Example B
Prevent the drawing surface from being accessed by other programs while we draw.
Retrieve the memory pitch of the surface memory so we do not draw out of bounds.
Retrieve the location of the surface so that we can draw to it successfully.
If the pixel mode is set to random
Create three random values that we can assign to the x, y and color attributes of the pixel.
Else if the pixel mode is set to linear
Update the horizontal position of the pixel.
Create a random value for the color of the pixel.
End If
Plot the pixel on the screen.
Release the surface for general use once again.
Return TRUE that the pixel was plotted.
Obviously the above two code examples document the plotting of a pixel on the screen. I chose this for an example because it is a basic task a lot of you should be familiar with. Now, were you able to figure out which was the bad PDL and which was the good? If not go back and read the end of the first section again, where I clue you in on a few of things that you should not find in PDL. Even still, I think I made the distinction rather obvious.
The answer is Example B. Surprised? I hope not. Example A could not possibly fit my current description of PDL in any way whatsoever. I specifically pointed out how PDL is considered a high-level language, and some of the lines in Example A are anything but. Here are some of the things that make Example A a PDL nightmare:
- Although you may not realize it, the term "Lock a surface" can easily be attributed to DirectX and its Lock() function. The same applies to the term Unlock. Since we want our PDL to be as ambiguous as possible, Example B cleans this up by using the terms prevent and release. Anyone who wants to argue that release is also DirectX specific will break their noses running into a brick wall - in this context, release does not describe the freeing of COM components.
- Direct reference to variable names in Example A is also a no-no. And following close behind them are direct code examples! Ack! Once again ambiguity must reign supreme, and there's no way you can use that code in Visual Basic or any other language.
- Also present is the reference to a language-specific feature - the C pointer. This immediatly kills any sense of ambuguity since all other language options for writing this routine fall out.
- Then we have another direct code example for plotting the pixel, again unnacceptable.
- Finally we have the ending "return 0". Another language-limiting statement, and one that explains absolutly nothing of its true purpose.
After going through above points, and comparing them to Example B, you'll fully understand what PDL should look like. If you really think about it, you could code the pixel-plotting routine in any language capable of handling the task. Wonderful! Notice how the routine is nothing but English - nor variable names, no comparison signs, no code whatsoever. Of course, this brings us to a final point about good PDL use.
It's very easy to go overboard when writing in PDL. You may get a bit too descriptive and realize that coding what you wrote may be a nightmare! Therefore writing PDL can be considered an iterative process. Write it once, then start to break it down as far as you can go without quite reaching code level and while maintaining the basic rules of PDL.
PDL Uses
Part 1: Routine Review
Now thay we know how to construct PDL routines correctly, we can learn how best to use them. One of their uses is routine review. Project managers and lead programmers don't like to waste time going over endless lines of code to learn whether or not the routine does what they want it to, or what it's spec'd out to do. Consider how much easier it would be
Page : 1 Next >>