View Full Version : Duplication of code problem


Booswig
03-09-2008, 03:28 AM
With some help of the internet, as starter module from gemma_the_husky, as well as input from this forum, I managed to do a small noise propagation model in access, using a module (visual basic).

I have just added a second noise source, and I realised I effectively just doubled the code, including formulas, variables etc. I need some way to make this easier, as I have to add at least another few noise source points.

I define various functions and variables for the first two noise source point, eg.

' Source Noise Characteristics
Const Source_noise_63 = 115
Const Source_noise_125 = 105
Const Source_noise_250 = 103
Const Source_noise_500 = 97
Const Source_noise_1000 = 92
Const Source_noise_2000 = 85
Const Source_noise_4000 = 80
Const SP_Source_noise_63 = 105
Const SP_Source_noise_125 = 108
Const SP_Source_noise_250 = 110
Const SP_Source_noise_500 = 115
Const SP_Source_noise_1000 = 122
Const SP_Source_noise_2000 = 117
Const SP_Source_noise_4000 = 112
' Source Co-ordinates
Const Source_Xcoord = -12.5 '70852
Const Source_Ycoord = 6.501 '-2843675
Const SP_Source_Xcoord = 4.01 '70852
Const SP_Source_Ycoord = -5.01 '-2843675

etc

I also have various variables

' Some Global Variables used in the functions
Dim Angle_quadrant As Long
Dim yvec As Long
Dim ME_Cor63 As Double
Dim ME_Cor125 As Double
Dim ME_Cor250 As Double
Dim ME_Cor500 As Double
Dim ME_Cor1000 As Double
Dim ME_Cor2000 As Double
Dim ME_Cor4000 As Double
Dim SP_ME_Cor63 As Double
Dim SP_ME_Cor125 As Double
Dim SP_ME_Cor250 As Double
Dim SP_ME_Cor500 As Double
Dim SP_ME_Cor1000 As Double
Dim SP_ME_Cor2000 As Double
Dim SP_ME_Cor4000 As Double


Then I have also a massive

Select Case Pasquill_SC ' pasq_cat

Case 1:
If Wind_vector(x, y) <= -3 Then
ME_Cor63 = 8
ME_Cor125 = 5
ME_Cor250 = 6
ME_Cor500 = 8
ME_Cor1000 = 10
ME_Cor2000 = 6
ME_Cor4000 = 8

ElseIf Wind_vector(x, y) > -3 And Wind_vector(x, y) <= -0.5 Then
.
.
.


and


Select Case Pasquill_SC ' pasq_cat

Case 1:
If SP_Wind_vector(x, y) <= -3 Then
SP_ME_Cor63 = 8
SP_ME_Cor125 = 5
SP_ME_Cor250 = 6
SP_ME_Cor500 = 8
SP_ME_Cor1000 = 10
SP_ME_Cor2000 = 6
SP_ME_Cor4000 = 8

ElseIf SP_Wind_vector(x, y) > -3 And SP_Wind_vector(x, y) <= -0.5 Then
.
.
.


With about 20 functions similar to:


' Correction for Geometric Divergence due to distance
Function Geometric_divergence(x As Long, y As Long) As Double
Geometric_divergence = 10 * (Log(4 * Pie * (Dist_calc(x, y) ^ 2)) / Log(10))
End Function
' Correction for Geometric Divergence due to distance between second point source and receptor
Function SP_Geometric_divergence(x As Long, y As Long) As Double
SP_Geometric_divergence = 10 * (Log(4 * Pie * (SP_Dist_calc(x, y) ^ 2)) / Log(10))
End Function


The first set of code is basically for the first noise source, with the second being for the second noise source point. As can be seen, the only difference is that I added SP_ before everything, and doubled it. For the third point source I will just again double the original code, and add TP_ before it, using FP_ and FSP_ and so forth.

Now, that is messy. Is there a way that I can change the variables and function names with something like {point_name_holder} = 1SP_, SP_, TP_, FP_ and FSP_, and then use something like {point_name_holder}Geometric_divergence(x,y) and {point_name_holder}my_variables so that I do not need to double the code and just change the first few letters in the variable/functions/statements.

It will simplify my code, and make it far less messy than what it is now.

I hope you guys can understand me.

gemma-the-husky
03-09-2008, 04:24 AM
it depends really on what you are trying to do

if the results of your functions are based on a particular formula (as they probably are) then instead of having all the constants explicitily quantified, you can calculate the values you need based on somer sort of seed

so rather than having explicitly

If SP_Wind_vector(x, y) <= -3 Then
SP_ME_Cor63 = 8
SP_ME_Cor125 = 5
SP_ME_Cor250 = 6
SP_ME_Cor500 = 8
SP_ME_Cor1000 = 10
SP_ME_Cor2000 = 6
SP_ME_Cor4000 = 8

you have a function calculate these based on the value of sp_wind_vector - in fact it seems a bit artificial to have discrete values for a whole range of input parameters (eg EVERY VALUE less than 3)

The point is, that if you are able to make a function that uses parameters, perhaps you can use this same function to calculate the effect of the second source.

Having said that, you know what you are trying to do, and therefore how the calculations must flow.

If the two wave calcs need to reinforce/destroy depending on their origins, then I imagine it may well be extremely complex

in some ways it depends on how easy it is for you to code. It might seem long winded to copy and paste everything and explicitly duplicate/triplicate your code, but you only need to do this once, and if thats the quickest way to get it working, I would do it that way.

The wave calculator (if you like) can be treated as a black box - ie you put the parameters in, and get the results out, and it doesnt really matter how the calculation takes place.

In processing terms however the complexity of the calculation is not likely to have any siginficant affect on the speeed of the program - all the data is stored in memory, and it is only a few (relatively) extra clock cycles to undertake your calculations, even if they appear very complex - the only factor really likely to have an affect is the number of points you try to analyse

ie if you have 100*100 grid you have 10000 points to caclulate
ie if you have 1000*1000 grid you have 1000000 points to caclulate, which will take 100 times longer

so if the first calculates in 1 minute, the second takes 2 hours.