Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Structured Programming
Quote:
na_th_an Wrote:
Moneo Wrote:
axipher Wrote:I do fine with fb, and really, what’s wrong with using GOTO/GOSUB's, if the program works that should be enough. Shouldn't it???
Axipher, you made a very good point here that perhaps the structured programming purists missed.

Your point is: "if the program works...". I think that's the important issue. You wrote it, you tested it, you modify it, and you're happy with it. Almost all the QB/FB programs that all of us here write are for our own use, and are not going to have to pass inspection by some programming standards committee. If some of us chose to use gotos or GOSUB’s because we fell comfortable coding that way and have success doing so, then "to each his own."

If the programs we write were to be maintained by programmers other than ourselves, then yes, it would be a good convention to write these programs in a structured manner.
*****

The problem is, if we have to train and try to educate future programmers, we better teach them good practice.

I don't know how programming businesses are over there, but here, that you know how to do properly structured and indented programming is a must. Many businesses even have a "book of style" with directions on how to format your code. Because time is money, if some other employee has to keep with your project, it's better that he understands the sources in no time.

It's like when you are taking scripts. You get any pen on any kind of paper, and squibble fastly. Most of the time, you will understand it. I mean, it does the job. But what if you want the script to be read and understood, used and expanded by some other person? You have to use a proper paper, nice indentation and good calligraphy.

Structured programming is just that. It favourishes encapsulation, something that is a really important concept when you are going to work in a big project with more people. It favourishes readability. It also favourishes scalability. It's easier to debug and, most of all, easier to expand and to maintain.

I'm just talking of experience. I sometimes review my old programs and I have to spend some time figuring out where all those branches go and what do those unindented nested loops begin and end, which are inside which and stuff like that.

Plus, using GOSUB’s instead of SUBs is horrid for scalability. Imagine that you code something and place all your subroutines behind labels and you use GOSUB to call them. Now leave that program for two years. After that time, your boss asks you to add a new capability that someone else coded. You cut and paste the code and... ARGH! Same variable names! Everything goes haywire and you have to review the source line by line to replace the variable names. That takes time.

If you had used SUBs, you just stick the new SUB in your code and forget about it. You don't even have to read the new code. That's encapsulation.

That's why all my claims grow stronger and stronger. Meg has her own crusade as well. I think that in places like this we are "forming" future programmers. And no matter how we prefer to code, we should try to show them the "easiest way to success".

I do use GOTOs, but I strongly speak against newbies using them. Avoid GOTO like the plague until you know exactly what you are doing.





Instead of posting a meaningless post like usual, this post will actually contain information worth reading. Now Moneo, I did make a good point and na_th_an countered quite nicely, I think that most programs people make never do get released and are for personal use or for experiments. Some people though, like me, won't abandon a project until it is done and have a separate document explaining exactly what each part of the code does. Now if the program works and it is not being added to or being reviewed by someone who has never seen the code before, then using GOTO's and GOSUB's is ok, if I were making a program where there was more then just me working on it, then I would use SUB's instead. Now on the fact of indenting and UPPERCASE KEYWORDS, I am a great example, I actually do most of my coding on a Palm Pilot, I use the graffiti area to just write away, then when I transfer it to FreeBasic, it is all lower case and everything is aligned left, FreeBasic has 2 great features, 1 that turns all keywords into UPPERCASE and a feature that auto-indents. So I say don't worry about messy code when it comes to indents and keywords, they can easily be solved. Now na_th_an, I understand your stance on how we must teach future programmers proper techniques, but I found multiple teaching resource such as programs and books that teach BASIC and the first lessons use GOTO's and GOSUB's, also sometimes when examples are included with the source, you can't understand the code because of this. But if the source was not included or the program had no GOTO's or SUB's, the program would have worked just as well.

One last thing that deserves a paragraph of it's own, you all me posting off-topic or meaniningless posts, but then you go and flame them and create more useless posts, why not PM me and tell me to delete the post before anyone else posts???

P.S.: That's a lot of text, hope there's no mistakes :o I even corrected some of the quote's errors Big Grin
Even if you are the only one using the program. Can you explain why you prefer gosub over sub?
Reply
axipher... GOSUB is the old version of the "modern" subs/functions...

Are you still using a fireplace for heating? No? It could warm you just as well as whatever else you're using now, yknow.
url=http://www.copy-pasta.com]CopyPasta[/url] - FilePasta
Reply
Using GOSUB-style subroutines has the following consequences:

1. Any and all variables used within the subroutine must be global. There is no way to pass variables to GOSUB-style subroutines, so the only option for referencing variables outside the subroutine is to pollute the global namespace. This one fact alone should be enough of a deterrant.

2. There's no guarantee (read: mandate) of a GOSUB-style subroutine ever returning. If you don't manually return from the subroutine, your call stack will never get unwound, which can be quite a pain when we get to #3.

3. From #2, the chance of fall-through increases. Since GOSUB-style subroutines lack a definitive structure, missing a return statement or two has disastrous results.

Note that using functions (or subs) eliminates all three of the above bug-inducing situations. It is generally preferred, then, not to use GOSUB-style subroutines at all. It is a deprecated programming construct. Using them will only make your programming life harder.
stylin:
Reply
Quote:.....
The problem is, if we have to train and try to educate future programmers, we better teach them good practice.

I don't know how programming businesses are over there, but here, that you know how to do properly structured and indented programming is a must.....

Structured programming is just that. It favourishes encapsulation, something that is a really important concept when you are going to work in a big project with more people.....
.....
I think that in places like this we are "forming" future programmers. And no matter how we prefer to code, we should try to show them the "easiest way to success".
.....
All your points are true and very valid. However, this issue started with one programmer asking what's wrong with using goto/gosub on programs for his own personal use. That situation was what my post was addressing.

After seeing your point about educating future programmers, I must admit that my post should not have encouraged the programmer to continue using goto/gosub, no matter if it works, since he would be getting into bad habits that don't conform with the recognized standard of structured programming.

I appreciate your sharing your insight.
*****
Reply
Quote:axipher... GOSUB is the old version of the "modern" subs/functions...

Are you still using a fireplace for heating? No? It could warm you just as well as whatever else you're using now, yknow.

1. I tried converting my paint program into SUB's and it did not work, it would give me "duplicate definition errors".

2. Yes, I live out in the bush, 30 min from high-speed internet access, we have no natural gas line and our house is too big for electric heaters which we have anyway, so we have two fireplaces going constantly.


Now I also agree with teaching future programmer's the oproper ways of structured programming, but it's hard when the easiest ways to do something involve what structured programming is against.
Reply
Quote:.....
Even if you are the only one using the program. Can you explain why you prefer gosub over sub?
For me, who's been coding Basic since 1969, it's a matter of habit. Didn't have Do Loops, Functions and Subs in the old days, so I got used to using what was available, gotos and gosubs.

While working on my masters in 1975, they taught us structured programming. At the time, at work I was coding strictly in assembler, so could find no value to structured programming then. Like they say: "You can't teach an old dog new tricks."

But for most of the newer guys at programming since QuickBasic and QBasic in the 1980's, there is really no excuse.
*****
Reply
@ na_th_an : All of the companies I've worked for have set coding standards for each individual project. Meaning, you usually don't have a choice as to

1. how to name your variables/functions
2. how to indent code
3. how to comment your code

#3 tends to be pretty lax, but on the whole, you either follow their standards - which vary widely - or you don't have a job, regardless of your personal coding style.
stylin:
Reply
Quote:@ na_th_an : All of the companies I've worked for have set coding standards for each individual project. Meaning, you usually don't have a choice as to

1. how to name your variables/functions
2. how to indent code
3. how to comment your code

#3 tends to be pretty lax, but on the whole, you either follow their standards - which vary widely - or you don't have a job, regardless of your personal coding style.

That's why I code for fun. It's like when I make WarCraft and StarCraft maps, I don't reply to requests, I make what I want then submit that and people pay, they don't care how it's made, as long as it works, I make sure it works without bugs and then there's no reason to come back to it.
Reply
Quote:......

Now I also agree with teaching future programmer's the oproper ways of structured programming, but it's hard when the easiest ways to do something involve what structured programming is against.

I sympathize with you. I have the same problem myself. It's like when my dentist showed me how I should brush my teeth. Damn, I've been doing it wrong all my life! It was tough, but I had to learn for the sake of not loosing my teeth.

Don't go back and modify an existing program. When you start a new one, decide that this one is going to be structured. Do some pencil and paper design before you start.
*****
Reply
Quote:
axipher Wrote:......

Now I also agree with teaching future programmer's the oproper ways of structured programming, but it's hard when the easiest ways to do something involve what structured programming is against.

I sympathize with you. I have the same problem myself. It's like when my dentist showed me how I should brush my teeth. Damn, I've been doing it wrong all my life! It was tough, but I had to learn for the sake of not loosing my teeth.

Don't go back and modify an existing program. When you start a new one, decide that this one is going to be structured. Do some pencil and paper design before you start.
*****

If you were to read one of my posts possibly in another topic about how I use the graffiti part on the Palm Pilot and use the Stylus like a pen and that's my pen and paper. Now I guess once you make a program one way, it's hard to change it, I had a hard time porting programs to FB, now from GOSUB's to SUB's/FUNCITON's... Anyway, I now get what a SUB is, but how does a FUNCTION work?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)