PDA

View Full Version : PROGRAMMERS: Longest Subroutine?



XyZspineZyX
09-18-2003, 09:10 PM
S! All!

In preparing the next release of the RAF74 IL2/FB Dynamic Campaign System (which will be open source) I've been doing a ton of work 'cleaning up' the code. I'm changing from unstructured to structured error handling, breaking up some duplicate code so that I can re-use the same code over and over again instead of duplicating it, and breaking up overly-long subroutines.

In doing so, I broke up the bane of the program - I broke one subroutine that was 749 lines long into four subroutines (two of which may be broken up yet further - and some duplicate code still exists that I will consolidate, shortening it yet more).

We've all done it - started a subroutine that over time grew and grew well beyond it's original scope but that for whatever reason we were hesisitant to break up.

So what is the record? Mine is 749. Thank the Lord I've finally bitten the bullett and broken it up! I just hope the program still works http://ubbxforums.ubi.com/infopop/emoticons/icon_wink.gif

I'm also trying to simplify the user-interface as much as I can.

The next release will be a good one - and the mission generator will be functonal.

RAF74_Wall-dog

<a href="http://www.doghousecomputing.com/scorchedearth">http://www.doghousecomputing.com/scorchedearth/images/parsersmall.jpg

XyZspineZyX
09-18-2003, 09:10 PM
S! All!

In preparing the next release of the RAF74 IL2/FB Dynamic Campaign System (which will be open source) I've been doing a ton of work 'cleaning up' the code. I'm changing from unstructured to structured error handling, breaking up some duplicate code so that I can re-use the same code over and over again instead of duplicating it, and breaking up overly-long subroutines.

In doing so, I broke up the bane of the program - I broke one subroutine that was 749 lines long into four subroutines (two of which may be broken up yet further - and some duplicate code still exists that I will consolidate, shortening it yet more).

We've all done it - started a subroutine that over time grew and grew well beyond it's original scope but that for whatever reason we were hesisitant to break up.

So what is the record? Mine is 749. Thank the Lord I've finally bitten the bullett and broken it up! I just hope the program still works http://ubbxforums.ubi.com/infopop/emoticons/icon_wink.gif

I'm also trying to simplify the user-interface as much as I can.

The next release will be a good one - and the mission generator will be functonal.

RAF74_Wall-dog

<a href="http://www.doghousecomputing.com/scorchedearth">http://www.doghousecomputing.com/scorchedearth/images/parsersmall.jpg

Zayets
09-18-2003, 09:24 PM
What's the language? C++,VB? I never have subroutines more than 2 , let's say 3 pages which should be about max 300 lines including fancy carriage returns for a good readability of the code. The "cleanest" languages are C++ and Java IMHO. Good luck with your project.

Zayets out

http://www.arr.go.ro/iar81c.JPG

XyZspineZyX
09-18-2003, 11:07 PM
C++ is definitely not the "cleanest" object-orientated language .. to much pointers, to many unhandled crashes.
IMHO Java is much better but comes at its price.

As stated above, if a subroutine start to have more than 100 lines of code, then I start to look for ways to break it up. Always tried to stay below 200-300 lines (except in Assembler).

BTW: Size does not matter /i/smilies/16x16_smiley-wink.gif

Zayets
09-19-2003, 07:22 AM
weasel,
didn't meant "cleanest" for memory management. I was talking about code look.The most ugliest code stays in scripting langauages and that includes SQL.Yes , I find myself 80% in Java now. 3 years ago was C++ 80% , how time flies /i/smilies/16x16_smiley-happy.gif


Zayets out

http://www.arr.go.ro/iar81c.JPG

XyZspineZyX
09-19-2003, 07:55 AM
You guys, you kill me! Talking about dinky Java, SQL, C++ languages and subroutines... If you want to see some grand-daddy subroutines, take a look at some of the dinosaur scientific codes that were developed in the 1960s and 1970s. I once wrote a Fortran 5 subroutine that contained 100 continuations of a statement function declaration, BEFORE the first executable statement.

I've seen single subroutines of well over 2000 lines of spaghetti Fortran. In fact, I once worked with a guy, who when he saw my attempt at an architectural plan for a major new code, said "Subroutines? You don't want to use them, do you? They never work. Oh no, just put everything in the main. That works for me!"

I kid you not, and that was in 1989.

Aaah, computers.

IV/JG7_4Shades

PS: Isn't this a little OT, Wall-dog? /i/smilies/16x16_smiley-happy.gif

Zayets
09-19-2003, 08:18 AM
Well,what subroutines are now? What would you define as subroutine in C++ or Java? I have seen myself few Fortran and Cobol programs and I can tell you that I could not understand the whole.small pieces , yes but not the whole program.And I have no problems reading code since I did good years assembling for z80 and after that for 8088. Most of them for industrial purposes.and lso remember the huge (numbered) Basic programs where every subroutine was indeed in the "main". Remember GOSUB 1290/RETURN and so on or GO TO's which a actually exists even nowadays. Hehehehe , good times , I was paid royaly for doing these stuffs. Those times are gone , so sad.

Zayets out

http://www.arr.go.ro/iar81c.JPG

XyZspineZyX
09-19-2003, 08:49 AM
I share your pain Zayets /i/smilies/16x16_smiley-happy.gif

I remember those BASIC programs with the 260 different possible variable names!! LMAO!!! I guy I used to work with was writing a program and he needed over 260 variables, so he had a little piece of paper ruled into a grid, and he was ticking/shading/checking boxes all day to make sure he wasn't overwriting a variable that was live!

Oh no, I think I must be revealing my age! :-0

Back in the box 4Shades!

XyZspineZyX
09-19-2003, 08:50 AM
A subroutine is ok when it fits in a couple of screenfulls. At most.

http://sivusto.servepics.com/~lahnat/werre2s.jpg

prkl

XyZspineZyX
09-19-2003, 08:52 AM
Zayets wrote:
- The most ugliest code
- stays in scripting langauages

Utter crap. Try Python.


http://sivusto.servepics.com/~lahnat/werre2s.jpg

prkl

fluke39
09-19-2003, 09:02 AM
i was taught some assembly language about 3 years ago-jeez! - if theres anyone here who wrote large programs in that - S! to you my friend /i/smilies/16x16_smiley-happy.gif you probably have more skill than anyone who programs in java or c++ !/i/smilies/16x16_smiley-happy.gif


<center><img src=http://mysite.freeserve.com/Angel_one_five/flukelogo.jpg>

Zayets
09-19-2003, 09:32 AM
never tried Python although I used some scripts made by someone. As long as they were working didn't bothered too much for a check. But maybe you're right , but Perl,PHP and others man , they are ugly with those & # ~ !#$ prefixes. I preffer the code written without these characters , that I was saying. And yes , could be crap wht I'm saying but was just an oppinion you #$%^#@er!

3 years go you must been working on top notch cpu's.Now here I admit never worked. My assembly adventure stopped at 8088 model. Still happy I don't do it anymore. Those guys who bought that nummeric control machine forgot to ask for what software it uses. Turned on that the manufcturer had indeed a languge for the machine but the boss wandered around to see if is good for the money they've paid. Since as a highschool kid I was doing Z80 assembly to entertain my friends with crappy games, well , they assigned me to the task to make the machine move! I was scared to death ( I was hired for only bout 2 months) , turned out that was not as hard assembling on 8088.Now it looks so far /i/smilies/16x16_smiley-very-happy.gif

Zayets out

http://www.arr.go.ro/iar81c.JPG

XyZspineZyX
09-19-2003, 09:46 AM
It is interesting, is it not. I remember when, more than 15 years ago now, I broke the 1000 line limit for a whole program. At that time, for me that program was huge.

I'm not very impressed with long functions as they usually are better broken down into a few functions instead. My largest func. right now is just over 200 lines, and I'm going to divide it into 3 funcs I think.

The program as a whole is a few hundred K lines.
_
/Bjorn.

XyZspineZyX
09-19-2003, 09:59 AM
If it doesnt fit to one A4-page it is getting uglyhttp://ubbxforums.ubi.com/infopop/emoticons/icon_smile.gif

But use commonsense in braking the code to subroutines..
Depends on the task the subroutine is doinghttp://ubbxforums.ubi.com/infopop/emoticons/icon_smile.gif

fluke39
09-19-2003, 11:06 AM
yes i think there more subroutines the better (standard practice)

it not only makes the code more solid - it should save you alot more coding too - through reuse of existing functions/subroutines and also by making the code "tidier" thus easier to work through for error finding etc..

you probably know all this anyway..but i just thought i'd say /i/smilies/16x16_smiley-happy.gif

<center><img src=http://mysite.freeserve.com/Angel_one_five/flukelogo.jpg>

XyZspineZyX
09-19-2003, 11:14 AM
Werre_ wrote:
-
- Zayets wrote:
-- The most ugliest code
-- stays in scripting langauages
-
- Utter crap. Try Python.

For being a scripting language, Python is a wonder of cleanliness. Try Perl, or Bash/Csh, or how about a little awk?
_
/Bjorn.

fluke39
09-19-2003, 11:24 AM
RAF74_Wall-dog

(or any programming-type person)

please look at this will you

http://forums.ubi.com/messages/message_view-topic.asp?name=us_il2sturmovik_gd&id=yyrrc

thank you /i/smilies/16x16_smiley-happy.gif



<center><img src=http://mysite.freeserve.com/Angel_one_five/flukelogo.jpg>

XyZspineZyX
09-19-2003, 11:36 AM
- I preffer the code
- written without these characters , that I was
- saying. And yes , could be crap wht I'm saying but
- was just an oppinion you #$%^#@er!


Okay, pick your take http://ubbxforums.ubi.com/infopop/emoticons/icon_wink.gif

http://99-bottles-of-beer.ls-la.net/a.html

Best!
/Chris Z.

XyZspineZyX
09-19-2003, 12:08 PM
S! All!

You guys are bringing back some memories.

I wrote my first program on a TI 4KB machine back when I was 12. I wanted to call it "Star Trek" but there was a limit to seven characters in the name, so I ended up with "StarTre." It was a mindless zork-like text game, and it was only a few hundred lines of code. That was back in the "10 Print 'whatever' / 20 GOTO 10..." days. And of course it was in Basic.

Up until the RAF74 IL2/FB Dynamic Campaign System, the longest program I'd ever written might have been 2000 lines of code. For the most part, I use Visual Basic - not because it's a particularly 'great' language, but because it is a very FAST language. You can do in just a few hours in VB what would take a week in C++.

I like C++ better than VB. C++ is a much more robust programming language, but it just isn't as fast to write in.

The DCS is written with Microsoft Visual Studio .NET, and it's in VB. Though .NET is language-independant. Microsoft has relegated hardware control to the OS only and has made the compiler language-independant. C++ under .NET isn't any more powerful than VB. Back in the pre .NET days, I used C++ to write .dll's or whatever when I needed to do something that I couldn't do cleanly in VB. Now I can do everything in VB... Anyway, the program now has over 10,000 lines of code and while it is a functional program, there is still a ton of functionality that needs to be added. This is by FAR the biggest program I've ever written.

At this point someone is going to say something negative about VB. All I can tell you is that VB is fast, and while a lot of VB programmers learn to take a lot of stupid shortcuts, there is nothing in VB that forces you to use them. I don't have any real 'bad habits.' I'm perfectly adept at C++, albeit a bit slow from lack of practice... When I release the code for the DCS you'll be able to take a look at it - it's a pretty nice piece of work for VB. The only thing I really dislike about VB is that it really does encourage a lot of bad habits - whomever thought up 'data-bound objects' should be shot (I do all my database accessibility in freehand). On the other hand, what a breeze it is to be able to just call a 'msgbox' instead of having to write one...

A little about me - I'm the Information Systems Manager for a medium-sized manufacturing company. I'm in charge of everything from EDI to ERP. And I'm the only IT person in the company. I'm not just the manager of the IT department - I AM the IT department! I do a fair amount of programming, but I do a lot of a lot of things... When I program, time is at a premium. VB may be frowned upon by lots of people, but it makes real good business sense to use it - and particularly so in .NET where C++ isn't any faster or more powerful. In today's MS world, EVERYTHING needs a translator to run.

Anyway though - back to the story.

When I started I made the mistake of thinking that I only needed a few things from the .mis files I was parsing (most of the data is in the log files, right?). And I'd done all of the preliminary work outlining what I was doing and what I was going to need, etc. But then 'mission creep' happens and you start needing more data... Well, it turns out that in order to use the data from the log files to turn-around a sequential (and dynamic) online mission that does the things we wanted to do (including lots of user requests), you need a TON of data from the .mis file. To make a long story short, a 100 line subroutine that did the bulk of the .mis file parsing just kept growing and growing until it turned into a real monster.

Now, there are three good reasons to break up a subroutine. One is that it makes the code easier to read. Second is that it makes it easier to re-use code. The third is that it makes error-handling easier. 749 lines was too much. Not the longest is about 400 lines, and that's going to get cut-back as well.

RAF74_Wall-dog

<a href="http://www.doghousecomputing.com/scorchedearth">http://www.doghousecomputing.com/scorchedearth/images/parsersmall.jpg

XyZspineZyX
09-19-2003, 12:11 PM
&Zayets wrote:

- with those # ~ !#$ prefixes. I preffer the code
- written without these characters , that I was
- saying. And yes , could be crap wht I'm saying but
- was just an oppinion you #$%^#@er!

Hehe, that ending does look a lot like Perl /i/smilies/16x16_smiley-wink.gif

How about the traditional endian war? Big endian, little endian or (shudder!) Vax byte-order? /i/smilies/16x16_smiley-wink.gif
_
/Bjorn

XyZspineZyX
09-19-2003, 12:20 PM
swingman wrote:

- For being a scripting language, Python is a wonder
- of cleanliness. Try Perl, or Bash/Csh, or how about
- a little awk?
- /Bjorn.

Hey! Let's not get into awk-bashing, it's a great tool. I'd say that most ugly little Perl-scripts could be replaced by just as fast awk versions (and the footprint of awk is MUCH smaller).

http://sivusto.servepics.com/~lahnat/werre2s.jpg

prkl