Some Thoughts On Programming Style

Authors

Publication

Pub Details

Date

Pages

See all articles from QL Hacker's Journal 25

In QHJ #24 Tim talks about a colleague’s style of writing Perl and contrasts it with his own. I have spent several years as a programming and system development lecturer within my company’s internal training department and nothing seems to cause more grief/criticism/etc., etc., as differences of programming style.

I tend to use procedure calls in preference to the use of deep nesting of ‘if..then..else..endif’ structures as does Tim’s colleague. I do this for a number of reasons and even if the procedure may only be called once in the entire program (incidentally this approach is taken by Kernighan and Ritchie in ‘The C Programming Language’ and by Kernighan and Plauger in ‘Software Tools in Pascal’).

My reasons are these:

  • it is sometimes inconvenient to read deeply nested ‘if..else..endif’ or ‘while..endwhile’ constructions;
  • this approach works very well with the program design method that I prefer to use (Jackson Structured Programming, aka. JSP);
  • if suitable procedure names are chosen the clarity of the code is often improved;
  • the style is closer to the object-oriented programming approach that I would prefer to use;
  • the arguments about inefficiency (“It’s wasteful to set up a stack frame and call code that could have been inline.”) take little account of the maintenance benefits that can accrue from well-designed and named procedures.

I find something like this much easier to follow (and debug!),

    procedure DoLotsOfThingsTo(var A : AType);

var
i : integer;

procedure DoOneThingTo(var A : AType );

begin
A.A := ...;
A.B := ...;
end {DoOneThingTo};

begin
for i := 1 to SizeOfAType do
DoOneThingTo(A);
...
end {DoLotsOfThingsTo};

(the above also shows one reason that I like Pascal – the ability to nest procedure declarations – I miss it a lot when I use C, C++ or things like Visual Basic).

Incidentally, Question: can you nest procedure definitions in SuperBasic?

Answer: Yes you can:

    1000 define procedure testa
1010 :
1020 define procedure testb
1030 print "In testb"
1040 end define testb
1050 :
1060 print "In testa (1)"
1070 testb
1080 print "In testa (2)"
1090 :
1100 end define testa

works perfectly, printing out,

    In testa (1)
In testb
In testa (2)

as expected.

As I said in my article on parameters and parameter passing mechanisms I think that most languages would be better if they were designed so that procedures and functions (in the SB or Pascal sense) could only access local variables or parameters – even for read access only.

Products

 

Downloadable Media

 

Image Gallery

People

No people associated with this content.

Scroll to Top