Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions

2017 ACTUARIAL SALARY SURVEYS
Contact DW Simpson for a Personalized Salary Survey

Reply
 
Thread Tools Search this Thread Display Modes
  #51  
Old 06-27-2008, 02:08 PM
Ron Weasley's Avatar
Ron Weasley Ron Weasley is offline
Member
CAS AAA
 
Join Date: Oct 2001
Studying for naught.
Favorite beer: Butterbeer
Posts: 8,623
Default

Quote:
Originally Posted by carryme View Post
Let's put it this way:

With your argument you could also claim that ELSE is there just because of the demand not the need. Would you call using ELSE just a difference in style?!
ELSE doesn't do anything by itself, its just an optional part of the IF construct. It's included for the same reason that case statements are often included in languages, it helps make the IF construct easier to manage and follow.

So, no, I would not call using ELSE just a difference in style, I call it an optional part of the IF construct.
Reply With Quote
  #52  
Old 06-27-2008, 02:10 PM
GadgetGeek's Avatar
GadgetGeek GadgetGeek is offline
Note Contributor
SOA AAA
 
Join Date: Sep 2001
Location: I'm here, where are you?
Studying for a way to escape
College: Illinois College ('87)
Favorite beer: Stag
Posts: 11,154
Default

Quote:
Originally Posted by Steve Grondin View Post
Did you do this just to see if we are asleep?
__________________
Some people are like slinkies. Mostly useless, but provide amusement when pushed down the stairs.
Reply With Quote
  #53  
Old 06-27-2008, 02:14 PM
carryme's Avatar
carryme carryme is offline
...:illllllli:..
 
Join Date: Feb 2005
Posts: 1,955
Default

Quote:
Originally Posted by Ron Weasley View Post
it helps make ... easier to manage and follow.
So does "continue".
Reply With Quote
  #54  
Old 06-27-2008, 03:20 PM
Ron Weasley's Avatar
Ron Weasley Ron Weasley is offline
Member
CAS AAA
 
Join Date: Oct 2001
Studying for naught.
Favorite beer: Butterbeer
Posts: 8,623
Default

. . . in your style.
Reply With Quote
  #55  
Old 06-27-2008, 05:52 PM
Gates Is Antichrist's Avatar
Gates Is Antichrist Gates Is Antichrist is offline
Member
SOA AAA
 
Join Date: Feb 2002
Location: Hell
Favorite beer: Pacifico
Posts: 161
Default

Quote:
Originally Posted by notreallyme View Post
How do you make variables hold their values when going from one sub to the next?
I don't see that this (from thread entry #14) was ever answered. (You also expressed problems with undefined variables. I would urge study/google of "option implicit" and stick with it until you die. If you're unconvinced, I'll hunt down an article that is persuasive.)

The general rule for scope is to define a variable at the least common level - i.e. lowest scoped. In extreme case, declare a variable in the middle of a routine, and it stays static (isn't cleared out) until leaving the routine. *

But declaring a variable in the middle of a routine, or the frivolous C example footnoted, are generally discouraged. Unless you have a reason not to, declare variables at the top of routines. That means that the scope of the variable is the routine it's declared in.

If you want to live beyond the routine, expand scope accordingly. If all the code where the variable is used is in one module (i.e., you can page up and down across all code where it needs to retain its value), declare it atop all the subs and functions (control-home or page up to the top of the code).

If the variable needs to live even further than that, such as in the code of multiple worksheets, the next level of scope is to declare it in a general module. This is essentially making it global.

Of course, you could always keep the contents alive by making the variable of concern a calling parameter.

Once all this is second nature, I suspect that the concept of GOSUB will fade away from you. (Personally, I only view it as a backward compatibility convenience.) If your variables are scoped (or parameterized) such that they retain their contents, then there's no [that I am aware of] reason not to call other subs or functions outright, rather than gosub.

That sounds like a lot of hooey unless I give an example. People don't give enough examples. Examples are what make the world go around. [/rant off].

Sub foo
dim i as long
i=8
gosub ILoveGates
i=6
gosub IWantHisBabies
exit sub
ILoveGates:
msgbox i
return
IWantHisBabies:
msgbox i
return
end sub

Instead, make that

dim i as long 'atop the module

Sub foo
i=8
call ILoveGates
i=6
call IWantHisBabies
end sub

sub ILoveGates
msgbox i
end sub

sub IWantHisBabies:
msgbox i
end sub

This is poor code, but hope that it cleanly demonstrates the point of avoiding gosub via scope.

* (BTW, check me out, but I believe in the C language, you could go
{int foo}
in any old place, and its scope is virtually zero code lines!)

P.S. I miss the hell out of continue (in VBA).
__________________
______
ON STRIKE. Thomas Jefferson and I are on strike against AO over a little matter called the First Amendment and censorship against dissenting views. I hereby choose to toss my voluntary contributions into the Harbor rather than accede in slavery to the oppressor.
Reply With Quote
  #56  
Old 06-28-2008, 02:05 AM
Phil's Avatar
Phil Phil is offline
Site Supporter
Site Supporter
 
Join Date: Sep 2001
Location: not on board the Enterprise
Posts: 5,667
Default

Agreed. The only annoyance with the above is that, due to the poor VBA interface, the subs "IloveGates" and "IwantHisBabies" will now appear in the subroutine dropdown menu in VBA Project Explorer, and also in the macro dropdown menu in Excel (when you press Alt-F8 to run a macro). You may not wish to see these subs listed there, as they are truly only meant to be called from within Sub Foo.

It would be very very nice if you didn't have to see the auxiliary subs in those lists, as they clutter up the lists. Using GoSub avoids this.

If a sub requires an argument, then it won't appear in the Alt-F8 menu but it will still appear in the VBA sub dropdown menu. One solution might be to give them names that start with Z, like Z_ILoveGates and Z_IwantHisBabies, so at least they'll all be listed at the end. Or put them in a separate module but that's a bit of an annoyance too.
Reply With Quote
  #57  
Old 06-28-2008, 12:51 PM
jraven's Avatar
jraven jraven is offline
Member
 
Join Date: Aug 2007
Location: New Hampshire
Studying for nothing!
College: Penn State
Posts: 1,305
Default

Quote:
Originally Posted by Phil View Post
Agreed. The only annoyance with the above is that, due to the poor VBA interface, the subs "IloveGates" and "IwantHisBabies" will now appear in the subroutine dropdown menu in VBA Project Explorer, and also in the macro dropdown menu in Excel (when you press Alt-F8 to run a macro). You may not wish to see these subs listed there, as they are truly only meant to be called from within Sub Foo.
You could just mark the subroutines Private -- that will keep them out of Excel's macro list.
__________________
The Poisson distribution wasn't named after a fish -- it was named after a man ... who was named after a fish.
Reply With Quote
  #58  
Old 06-30-2008, 09:49 AM
carryme's Avatar
carryme carryme is offline
...:illllllli:..
 
Join Date: Feb 2005
Posts: 1,955
Default

Quote:
Originally Posted by Gates Is Antichrist View Post
I would urge study/google of "option implicit" and stick with it until you die. If you're unconvinced, I'll hunt down an article that is persuasive.)
I like to see why one would recommed using Option Implicit. I always use Option Explicit and have never seen anybody recommending Option Implicit before.

Quote:
If the variable needs to live even further than that, such as in the code of multiple worksheets, the next level of scope is to declare it in a general module. This is essentially making it global.
You don't need a general module to make it global. It already is considered global if you have not declared it inside a sub or function. If you need to be able to reference the variable in other modules, all you need to do is declaring it as Public variable. A Private module level variable is still global but can be accessed only through that module. That being said, the idea of having all your global variables in a separate sheet is a very good practice to make them manageable.

In general, using global variables is discouraged because it reduces the modularity and reusability of your code. Try to pass variables as arguments as much as possible. Of course, using a global variable makes your code concise and that is a plus that you cannot easily overlook.

Using global constants on the other hand is a very good practice.

Quote:
P.S. I miss the hell out of continue (in VBA).
Reply With Quote
  #59  
Old 06-30-2008, 10:12 AM
campbell's Avatar
campbell campbell is offline
Mary Pat Campbell
SOA AAA
 
Join Date: Nov 2003
Location: NY
Studying for duolingo and coursera
Favorite beer: Murphy's Irish Stout
Posts: 83,773
Blog Entries: 6
Default

__________________
It's STUMP

LinkedIn Profile
Reply With Quote
  #60  
Old 06-30-2008, 05:42 PM
Gates Is Antichrist's Avatar
Gates Is Antichrist Gates Is Antichrist is offline
Member
SOA AAA
 
Join Date: Feb 2002
Location: Hell
Favorite beer: Pacifico
Posts: 161
Default

and
. . . . . . . . . . . .

And EXplicit, si` si`. Doooooooooo
__________________
______
ON STRIKE. Thomas Jefferson and I are on strike against AO over a little matter called the First Amendment and censorship against dissenting views. I hereby choose to toss my voluntary contributions into the Harbor rather than accede in slavery to the oppressor.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT -4. The time now is 03:17 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.27088 seconds with 9 queries