I was recently doing some CGI-BIN programming in Perl. Since I was new to CGI-BIN programming, I did what every other programmer does when he is new to something, he borrows somebody else’s code to see how it is done. I borrowed come Perl CGI-BIN code from our local CGI-BIN expert at work.
When writing my code I tried to stay close to the style that he was writing in. I did notice that he has a style that is very different than mine. He likes to put any long sections of code in a subroutine. Where as I do not like to break up these sections in to subroutines. My code looks kind of like this:
--------------
--------------
--------------
IF ....... THEN .....
------------
------------
------------
------------
IF ...... THEN ......
-----------
-----------
-----------
-----------
ELSE
------------
------------
------------
------------
END IF
-------------
-------------
Where as his code looks like this:
--------------
--------------
--------------
IF ....... THEN .....
Call Subroutine1
ELSE
Call Subroutine2
END IF
-------------
-------------
where the code in the IF-THEN-ELSE constructs were moved to two subroutines.
Now none of these subroutines were going to be called from more than one place in the code. I prefer to write subroutines only when they save writing the same code in different part of the program. To me subroutines are designed to save the amount of code you are writing.
My friends idea of subroutines is to make the main part of your code shorter and easier to read. This is fine if the sections of code that you are going to move into a subroutine are fairly long.
I prefer to not use a subroutine unless I need to write the same code many places. For me the subroutines break the flow of the structure of the program. I view it sort of like parsing a tree. Just as with a left- or right-handed rule, you keep moving down a tree until you come to a leaf node and then you back up and take the next branch, coding flows out like a tree structure going down all of the IF-THEN statements until an end is reached and then brought back up the tree with and END IF statement. One never parses a tree by individual levels of depth at a time.
I wanted to bring this up because it shows two distinct coding styles and why they are done that way, and maybe it will get you thinking about your personal coding style and how you feel about it versus others.