View Full Version : Program structure - all opinions welcome
andyede
03-05-2005, 02:48 PM
I was reading an artical today which got me thinking abit about program structure. The writer explained alot about how he personally likes to set out programs and why and i was wondering how other people like layout their scripts. It something i thought about alot when i was just starting to learn, "am i doing this right" but with noone to give me lessons i just found the way that worked best for me. Although my programing style changes from almost every new program i write the overall structure stays very similar dispite a few areas that i have found better way to do. for example...
I only really ever programed for CGI and i tend to use one subroutine to do each page or process that need to happen. I used to print the results at the bottom of each subroutine but i found it made them messy when i wanted to do anything other than just print out simple html.
When i started using template pages more often i wrote a subroutine that handled the template page and plugging my html into it but that was starting to get limited when i wanted to add javascript to the head tags for certain functions. By now (not too long ago) i was stubling into the world of modules and OOP programming so i wrote my own module to deal with HTML printing and template pages.
This module, although very simple, can now handle inputting a large amount of JS and will also define CSS styles which now gives me loads of control over page layout and stucture all from the comfort of my main program. i'm still adding to this module all the time.
anyway thats a little off subject but the point remains that my program structure is pretty much the same.
Has anyone got any personal favorate layouts/structures they use all the time for a particular reason?
netroact
03-06-2005, 02:41 AM
I am certainly no expert on CGI/Perl, and I might not even understand the question. But, you asked for opinions.
I believe that structure has more to do with personal style and preference. Of course, server resource efficiency is important. I love the Perl language, because it is very analytical and practical, and I'm a very analytical type person.
As I learn more and more about programming Perl, my code writing becomes more and more condensed. And, I am finding more and more ways of doing things. My structure is starting to evolve into my own style.
I have learned the hard way to always saturate my scripts with comments for
future reference. I am amazed at how many times I call the same routine to several different scripts.
My problem is that I am always working so fast to get programs competed, that I don't have time to polish the structure. I always tell myself that I will come back later, and rework the code for efficiency and clarity.
I recently reviewed a free perl program I downloaded from the web. The author called numerous scripts from the user interface. Most people would have incorporated all the code into one or two scripts, and called subroutines. He turned his routines into separate scripts named accordingly. I guess that was his style. It certainly was easy for me to analyze the code. He made several mistakes in the code that called errors, but it was easy for me to make corrections. Maybe I'm wrong, but I'm thinking that if I can analyze the program so easily, the Perl interpreter probably can as well.
mlseim
03-06-2005, 05:09 AM
I learned most Perl by looking at other people's code.
When I look back at some older scripts I've written, it
sort of makes me cringe. I certainly have gotten better
at condensing things.
The biggest change I've made lately is using some of
the modules that my webhost has installed. I never realized
how many I had at my disposal until I looked into it.
There are modules that make the hardest things so easy
to do, especially photo stuff (PerlMagik). Of course,
I have to be careful because not everyone has use of
the modules, but for my own stuff, this has really been
a big thing to me.
I found I like trying to keep everything in one script and
use subroutines... my own preferred style. It really helps
when I need to pass the script on to someone else. I
don't have to zip together 10 separate scripts.
I'm not so good with comments, but I should be ... they
are important.
And then there are the serious programmers that are very
strict about program layout as far as indentations and
white-space with the actual program script. I've taken
C++ type courses that have a whole class devoted to
just the layout of code in the "visual sense". I certainly
don't follow the rules.
But, there is great satisfaction in writing a script and
seeing it work .... that's the best :thumbsup:
netroact
03-06-2005, 06:17 AM
But, there is great satisfaction in writing a script and seeing it work .... that's the best
I know what you mean there. My friends and family don't understand the fun
I have programming on the computer. And then the satisfaction when the job is done.
I get lost in PerlMagick. There is so much stuff you can do with that interface. I do hosting, and I am developing a member section that will soon be full of free graphics applications for members, thanks to the ImageMagick module.
Something I still use, but I don't see anymore, is .lib files. Sometimes I like to organize my scripts with those, and then I don't have all those subroutines crammed on the same page. It gets too crowded and confusing for me.
PHP is probably a lot more secure, but I tried it for a few applications, and I came back to Perl. For me, Perl is much more practical.
andyede
03-06-2005, 01:38 PM
Lib files are something i never used but thinking about it i should. I have some pretty standard subroutines that i use all the time, like error handling and stuff but whats alway put me off is the lack of control you then have over that subroutine. different programs need different levels of error handling so you then can't edit that sub too much incase other sctipts that use it are no longer compatable. I guess what would work best in this kind of situation would be to write a very complex script that would be able to handle all possible out comes needed and maybe turn it into a module but that wouldn't really be nessesary.
I think alot of my program structure comes from when i learnt. The first thing i read was perl and CGI for the www which although using only basic programs sets them out in a simple way with subs for each page you see. I have experimanted with other ideas like breaking down the main subs into other ones to make them smaller but i never really saw the point. Unless there are processes that are performed over and over again like html printing or error pages then all that you end up doing is making your program jump about all over the place.
what do other people think about how many functions you should put into one program? I like to make one script do as much as i can but i've noticed that when you see alot of applications on websites they seem to break them down into lots of smaller programs that do more specific tasks.
netroact
03-06-2005, 04:23 PM
The first thing i read was perl and CGI for the www which although using only basic programs sets them out in a simple way with subs for each page you see
That was the first book I read as well. Very good starter book.
but i've noticed that when you see alot of applications on websites they seem to break them down into lots of smaller programs that do more specific tasks.
I have been noticing just the opposite. It seems like that code is getting more condensed, and they are putting it all in one script. There are exceptions, like I noted above.
andyede
05-05-2005, 11:15 AM
I was reading the artical (http://www.ukuug.org/events/linux2002/papers/html/php/index.html) that Jeff Mott posted in this (http://www.codingforums.com/showthread.php?t=58390) thread and thought i would reserect this thread with an issue that was discussed as a disadvantage of PHP. This isn't a comparison between perl and php just another programming style issue.
the part of the artical that got me thinking was section 2 (http://www.ukuug.org/events/linux2002/papers/html/php/index.html#section_2) (Separation of Presentation from Business Logic), basically the seperating of the HTML to be displayed by your program from the code that does the usful stuff. I'm really wondering what method people prefere to getting and printing data and what advantages they see for that method and also if anyone has any prefered templating systems methods/modules/subs.
Like i said above i have a custom module for loading a template and adding CSS and javascript before printing the results (when it works....pattern matches still need attention) but the way this currently works is by taking the HTML you want to display all in one big slurp at the end. You can't add to t bit by bit which i guess limits your options of program design somewhat (possible update). However this does suit my prefered programming style which is basically - get all the data - do stuff too it - loop through complex stuff and print it out.
Does anyone have a method they prefer to this or another templating method that is more powerful/ gives more control?
Jeff Mott
05-06-2005, 12:46 AM
Does anyone have a method they prefer to this or another templating method that is more powerful/ gives more control?I generally use the HTML::Template (http://search.cpan.org/~samtregar/HTML-Template-2.7/Template.pm) module. This gives you a lot of control over how data is included in a page and also gives you almost complete separation of programming versus layout.
VortexCortex
05-19-2005, 08:52 AM
When I set out to make a new program... I usually list all the features I want the application to have.
I then write pseudo code for each feature, designing file formats and interfaces as needed.
I put all of my features in a tree structures to show which features rely or build upon other features.
Next, I decide which language(s) would best suit each set of features.
After that, I code and test each feature as functions/methods of objects (OOP for portablility/reusablility/name space preservation)
Then I draw (paper and pencil) the (g)UI, deciding how the user will interact with the features I've designed.
Finally, I code the GUI (or web pages) using the objects I created and tested earlier... This results in much less testing of the final product.
As an additional last step, I archive everything I've coded by feature and language for later refrence. Since I use OOPs I usually don't have to code the same functionality twice. (unless porting from one language to another).
P.S. when outputting dynamic html I prefer to write my own XHTML 1.0 compliant functions. For example If I have a navigation bar, I'll code it into a sub function that accepts a hash of title/url key/value pairs and outputs XHTML to the browser.
A website I designed simply called different functions depending on the query sent to a central CGI script.
my $q = new CGI;
my $page = $q->param('page');
$page = $page ? $page : 'home';
#other code
if ( $page eq 'home' ) {
heading('Home');
navbar( ... );
links( ... );
news( time );
copyright();
}
if ( $page eq 'games' ) {
heading('Games');
navbar( ... );
links( ... );
gamesList( ... );
copyright();
}
#etc..
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.