Older Version
Newer Version
StPendl
Jan 14, 2012
Streamlining your code
Chapter 1 - Starting the mission
-This tutorial was created in response to a request in the discussion Resistor Color Code at The Official Liberty BASIC Support Group
The provided code is used with permission of the original owner.
The tutorial is aimed to show you how you can streamline your code by restructuring it and consolidating redundant code into multi-purpose blocks.
Hello
The following is a Resistor Color Code of four bands, selecting colors is done
by radiobuttons.
Is it possible to give a hint by small code to cancel radiobuttons and select
colors just by clicking on the colored squares?
Any other suggestion would be appreciated
Thanks
The code below was posted as the base for implementing the requested feature.
As you can see there are many separate controls defined and each of the four bands uses its own event handler.
At the end of this tutorial you will know how you can create multi-purpose code blocks and reduce your code to the bare minimum.
Small pieces of code are better to debug and maintain than huge piles of redundant source code.
'Resistor Calculator.bas
'Author: salaion Yahoo! Group
'Date: 27.11.10
'
' 27.11.10 00:18:50 - Initial as posted
nomainwin
WindowWidth = 850:WindowHeight = 600
UpperLeftX=int( (DisplayWidth- WindowWidth) /2)
UpperLeftY=int( (DisplayHeight- WindowHeight) /2)
BackgroundColor$ = "green"
'ForegroundColor$ = "Black"
STATICTEXT #w.Ohm,"" , 500, 200, 300, 20
STATICTEXT #w.Tole,"" , 500, 230, 300, 20
STATICTEXT #w,"Black" , 10, 42, 50, 20
STATICTEXT #w,"Brown" , 10, 82, 50, 20
STATICTEXT #w,"Red" , 10, 122, 50, 20
STATICTEXT #w,"Orange" , 10, 162, 50, 20
STATICTEXT #w,"Yellow" , 10, 202, 50, 20
STATICTEXT #w,"Green" , 10, 242, 50, 20
STATICTEXT #w,"Blue" , 10, 282, 50, 20
STATICTEXT #w,"Violet" , 10, 322, 50, 20
STATICTEXT #w,"Gray" , 10, 362, 50, 20
STATICTEXT #w,"White" , 10, 402, 50, 20
STATICTEXT #w,"Gold" , 470, 42, 50, 20
STATICTEXT #w,"Silver" , 470, 82, 50, 20
STATICTEXT #w,"None" , 470, 122, 50, 20
STATICTEXT #w,"K=Kilo " , 500, 300,100, 20
STATICTEXT #w,"M=Mega " , 500, 330,100, 20
STATICTEXT #w,"Light Blue is The Default Color of Resistance " , 500, 360,400, 20
'Band1
groupbox #w.FirstDigit, "First Digit", 70,10,80,430
radiobutton #w.Blackr1 ,"Blackr1" , [Band1],[Band1] ,75,45,20,20'Black=0
radiobutton #w.Brownr1 , "Brownr1", [Band1],[Band1] ,75,85,20,20'Brown=1
radiobutton #w.Redr1, "Redr1", [Band1],[Band1] ,75,125,20,20'Red=2
radiobutton #w.Oranger1 , "Oranger1 ", [Band1],[Band1] ,75,165,20,20'Orange=3
radiobutton #w.Yellowr1 , "Yellowr1 ", [Band1],[Band1] ,75,205,20,20'Yellow=4
radiobutton #w.Greenr1 , "Greenr1 ", [Band1],[Band1] ,75,245,20,20'Green=5
radiobutton #w.Bluer1 , "Bluer1 ", [Band1],[Band1] ,75,285,20,20'Blue=6
radiobutton #w.Violetr1 , "Violetr1 ", [Band1],[Band1] ,75,325,20,20'Violet=7
radiobutton #w.Grayr1, "Grayr1 ", [Band1],[Band1] ,75,365,20,20'Gray=8
radiobutton #w.Whiter1 , "Whiter1 ", [Band1],[Band1] ,75,405,20,20'White=9
'Band2
groupbox #w.SecondtDigit, "Second Digit", 180,10,80,430
radiobutton #w.Blackr2 , "Blackr2 ", [Band2],[Band2] ,185,45,20,20'Black=0
radiobutton #w.Brownr2 , "Brownr2 ", [Band2],[Band2] ,185,85,20,20'Brown=1
radiobutton #w.Redr2, "Redr2 ", [Band2],[Band2] ,185,125,20,20'Red=2
radiobutton #w.Oranger2 , "Oranger2 ", [Band2],[Band2] ,185,165,20,20'Orange=3
radiobutton #w.Yellowr2 , "Yellowr2 ", [Band2],[Band2] ,185,205,20,20'Yellow=4
radiobutton #w.Greenr2 , "Greenr2 ", [Band2],[Band2] ,185,245,20,20'Green=5
radiobutton #w.Bluer2 , "Blue2 ", [Band2],[Band2] ,185,285,20,20'Blue=6
radiobutton #w.Violetr2 , "Violetr2 ", [Band2],[Band2] ,185,325,20,20'Violet=7
radiobutton #w.Grayr2 , "Grayr2 ", [Band2],[Band2] ,185,365,20,20'Gray=8
radiobutton #w.Whiter2 , "Whiter2 ", [Band2],[Band2] ,185,405,20,20'White=9
'Band3
groupbox #w.Multiplier, "Multiplier", 280,10,80,430
radiobutton #w.Blackr3 , "Blackr3 ", [Band3],[Band3] ,285,45,20,20'Black=0
radiobutton #w.Brownr3 , "Brownr3 ", [Band3],[Band3] ,285,85,20,20'Brown=1
radiobutton #w.Redr3 , "Redr3 ", [Band3],[Band3] ,285,125,20,20'Red=2
radiobutton #w.Oranger3 , "Oranger3 ", [Band3],[Band3] ,285,165,20,20'Orange=3
radiobutton #w.Yellowr3 , "Yellowr3 ", [Band3],[Band3] ,285,205,20,20'Yellow=4
radiobutton #w.Greenr3 , "Greenr3 ", [Band3],[Band3] ,285,245,20,20'Green=5
radiobutton #w.Bluer3 , "Bluer3 ", [Band3],[Band3] ,285,285,20,20'Blue=6
radiobutton #w.Violetr3 , "Violetr3 ", [Band3],[Band3] ,285,325,20,20'Violet=7
radiobutton #w.Grayr3 , "Grayr3 ", [Band3],[Band3] ,285,365,20,20'Gray=8
radiobutton #w.Whiter3 , "Whiter3 ", [Band3],[Band3] ,285,405,20,20'White=9
'Band4
groupbox #w.Tolerance, "Tolerance", 380,10,80,430
radiobutton #w.Goldr , "Goldr ", [Band4],[Band4] ,385,45,20,20'Gold=5%
radiobutton #w.Silverr , "Silverr ", [Band4],[Band4] ,385,85,20,20'Silver=10%
radiobutton #w.Noner , "Noner ", [Band4],[Band4] ,385,125,20,20'None=20%
'Fram
graphicbox #w.add, 530,60,292,30'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
stylebits #w.add, 0,_WS_BORDER, 0,0
graphicbox #w.up, 523,53,306,7
stylebits #w.up, 0,_WS_BORDER, 0,0'
graphicbox #w.down, 523,90,306,7
stylebits #w.down, 0,_WS_BORDER, 0,0'
graphicbox #w.side1, 523,53,7,44
stylebits #w.side1, 0,_WS_BORDER, 0,0'
graphicbox #w.adds2, 822,53,7,44
stylebits #w.adds2, 0,_WS_BORDER, 0,0'
'Band1
graphicbox #w.Black1, 100,40,30,30
graphicbox #w.Brown1, 100,80,30,30
graphicbox #w.Red1, 100,120,30,30
graphicbox #w.Orange1, 100,160,30,30
graphicbox #w.Yellow1, 100,200,30,30
graphicbox #w.Green1, 100,240,30,30
graphicbox #w.Blue1, 100,280,30,30
graphicbox #w.Violet1, 100,320,30,30
graphicbox #w.Gray1, 100,360,30,30
graphicbox #w.White1, 100,400,30,30
'Band2
graphicbox #w.Black2, 210,40,30,30
graphicbox #w.Brown2, 210,80,30,30
graphicbox #w.Red2, 210,120,30,30
graphicbox #w.Orange2, 210,160,30,30
graphicbox #w.Yellow2, 210,200,30,30
graphicbox #w.Green2, 210,240,30,30
graphicbox #w.Blue2, 210,280,30,30
graphicbox #w.Violet2, 210,320,30,30
graphicbox #w.Gray2, 210,360,30,30
graphicbox #w.White2, 210,400,30,30
'Band3
graphicbox #w.Black3, 310,40,30,30
graphicbox #w.Brown3, 310,80,30,30
graphicbox #w.Red3, 310,120,30,30
graphicbox #w.Orange3, 310,160,30,30
graphicbox #w.Yellow3, 310,200,30,30
graphicbox #w.Green3, 310,240,30,30
graphicbox #w.Blue3, 310,280,30,30
graphicbox #w.Violet3, 310,320,30,30
graphicbox #w.Gray3, 310,360,30,30
graphicbox #w.White3, 310,400,30,30
'Band4
graphicbox #w.Gold, 410,40,30,30
graphicbox #w.Silver, 410,80,30,30
graphicbox #w.None, 410,120,30,30
'Selected Bands of Body Resistance
graphicbox #w.Band1, 155,470,15,35
graphicbox #w.Band2, 185,470,15,35
graphicbox #w.Band3, 215,470,15,35
graphicbox #w.Band4, 255,470,15,35
stylebits #w.Band1, 0,_WS_BORDER,0,0
stylebits #w.Band2, 0,_WS_BORDER,0,0
stylebits #w.Band3, 0,_WS_BORDER,0,0
stylebits #w.Band4, 0,_WS_BORDER,0,0
'General Body Resistance
graphicbox #w.37, 130,470,168,35
stylebits #w.37, 0,_WS_BORDER,0,0
'Terminal Wires
graphicbox #w.42, 80,485,50,5
graphicbox #w.43, 300,485,50,5
open "Drawing" for window as #w
#w, "trapclose [quit]"
PRINT #w, "font Times_New_Roman 13"
#w.add, "cls;fill yellow;font Times_New_Roman 16 bold;color red;backcolor yellow"
#w.add, "place 5 20;\Resistor Color Code of 4 Bands"
#w.up, "cls;fill blue"
#w.down, "cls;fill blue"
#w.side1,"cls;fill blue"
#w.adds2,"cls;fill blue"
#w.add, "flush"
#w.up , "flush"
#w.down , "flush"
#w.side1 , "flush"
#w.adds2 , "flush"
#w.Black1, "home; down; fill 0 0 0;flush"
#w.Brown1, "home; down; fill 170 100 20;flush"
#w.Red1, "home; down; fill 255 0 0;flush"
#w.Orange1, "home; down; fill 255 180 0;flush"
#w.Yellow1, "home; down; fill 255 255 55;flush"
#w.Green1, "home; down; fill 45 157 40;flush"
#w.Blue1, "home; down; fill 0 0 255;flush"
#w.Violet1, "home; down; fill 170 0 180;flush"
#w.Gray1, "home; down; fill 127 127 127;flush"
#w.White1, "home; down; fill 255 255 255;flush"
#w.Black2, "home; down; fill 0 0 0;flush"
#w.Brown2, "home; down; fill 170 100 20;flush"
#w.Red2, "home; down; fill 255 0 0;flush"
#w.Orange2, "home; down; fill 255 180 0;flush"
#w.Yellow2, "home; down; fill 255 255 55;flush"
#w.Green2, "home; down; fill 45 157 40;flush"
#w.Blue2, "home; down; fill 0 0 255;flush"
#w.Violet2, "home; down; fill 170 0 180;flush"
#w.Gray2, "home; down; fill 127 127 127;flush"
#w.White2, "home; down; fill 255 255 255;flush"
#w.Black3, "home; down; fill 0 0 0;flush"
#w.Brown3, "home; down; fill 170 100 20;flush"
#w.Red3, "home; down; fill 255 0 0;flush"
#w.Orange3, "home; down; fill 255 180 0;flush"
#w.Yellow3, "home; down; fill 255 255 55;flush"
#w.Green3, "home; down; fill 45 157 40;flush"
#w.Blue3, "home; down; fill 0 0 255;flush"
#w.Violet3, "home; down; fill 170 0 180;flush"
#w.Gray3, "home; down; fill 127 127 127;flush"
#w.White3, "home; down; fill 255 255 255;flush"
#w.Gold, "home; down; fill 255 238 51;flush" ' golden
#w.Silver, "home; down; fill 200 200 250;flush" ' silver
#w.None, "home; down; fill 21 228 255;flush" 'None
'General Body Resistance Color
#w.37, "home; down; fill 21 228 255;flush" 'light blue
'Color of Terminal Wires
#w.42,"home; down; fill 127 127 127;flush"
#w.43,"home; down; fill 127 127 127;flush"
#w.Band1, "home; down;fill 21 228 255;flush"
#w.Band2, "home; down;fill 21 228 255;flush"
#w.Band3, "home; down;fill 21 228 255;flush"
#w.Band4, "home; down;fill 21 228 255;flush"
[Band1]
#w.Blackr1 "value? Blackr1$"
#w.Brownr1 "value? Brownr1$"
#w.Redr1 "value? Redr1$"
#w.Oranger1 "value? Oranger1$"
#w.Yellowr1 "value? Yellowr1$"
#w.Greenr1 "value? Greenr1$"
#w.Bluer1 "value? Bluer1$"
#w.Violetr1 "value? Violetr1$"
#w.Grayr1 "value? Grayr1$"
#w.Whiter1 "value? Whiter1$"
select case
case Blackr1$="set"
case1=1
#w.Band1, "home; down; fill 0 0 0;flush"
Band1value=0*10'Black
case Brownr1$="set"
case1=1
#w.Band1, "home; down; fill 170 100 20;flush"
Band1value=1*10'Brown
case Redr1$="set"
case1=1
#w.Band1, "home; down; fill 255 0 0;flush"
Band1value=2*10'Red
case Oranger1$="set"
case1=1
#w.Band1, "home; down; fill 255 180 0;flush"
Band1value=3*10
case Yellowr1$="set"
case1=1
#w.Band1, "home; down; fill 255 255 55;flush"
Band1value=4*10
case Greenr1$="set"
case1=1
#w.Band1, "home; down; fill 45 157 40;flush"
Band1value=5*10
case Bluer1$="set"
case1=1
#w.Band1, "home; down; fill 0 0 255;flush"
Band1value=6*10
case Violetr1$="set"
case1=1
#w.Band1, "home; down; fill 170 0 180;flush"
Band1value=7*10
case Grayr1$="set"
case1=1
#w.Band1, "home; down; fill 127 127 127;flush"
Band1value=8*10
case Whiter1$="set"
case1=1
#w.Band1, "home; down; fill 255 255 255;flush"
Band1value=9*10
case else
wait
end select
[Band2]
#w.Blackr2 "value? Blackr2$"
#w.Brownr2 "value? Brownr2$"
#w.Redr2 "value? Redr2$"
#w.Oranger2 "value? Oranger2$"
#w.Yellowr2 "value? Yellowr2$"
#w.Greenr2 "value? Greenr2$"
#w.Bluer2 "value? Bluer2$"
#w.Violetr2 "value? Violetr2$"
#w.Grayr2 "value? Grayr2$"
#w.Whiter2 "value? Whiter2$"
select case
case Blackr2$="set"
case2=2
#w.Band2, "home; down; fill 0 0 0;flush"
Band2value=0'Black
case Brownr2$="set"
case2=2
#w.Band2, "home; down; fill 170 100 20;flush"
Band2value=1'Brown
case Redr2$="set"
case2=2
#w.Band2, "home; down; fill 255 0 0;flush"
Band2value=2'Red
case Oranger2$="set"
case2=2
#w.Band2, "home; down; fill 255 180 0;flush"
Band2value=3
case Yellowr2$="set"
case2=2
#w.Band2, "home; down; fill 255 255 55;flush"
Band2value=4
case Greenr2$="set"
case2=2
#w.Band2, "home; down; fill 45 157 40;flush"
Band2value=5
case Bluer2$="set"
case2=2
#w.Band2, "home; down; fill 0 0 255;flush"
Band2value=6
case Violetr2$="set"
case2=2
#w.Band2, "home; down; fill 170 0 180;flush"
Band2value=7
case Grayr2$="set"
case2=2
#w.Band2, "home; down; fill 127 127 127;flush"
Band2value=8
case Whiter2$="set"
case2=2
#w.Band2, "home; down; fill 255 255 255;flush"
Band2value=9
case else
wait
end select
[Band3]
#w.Blackr3 "value? Blackr3$"
#w.Brownr3 "value? Brownr3$"
#w.Redr3 "value? Redr3$"
#w.Oranger3 "value? Oranger3$"
#w.Yellowr3 "value? Yellowr3$"
#w.Greenr3 "value? Greenr3$"
#w.Bluer3 "value? Bluer3$"
#w.Violetr3 "value? Violetr3$"
#w.Grayr3 "value? Grayr3$"
#w.Whiter3 "value? Whiter3$"
select case
case Blackr3$="set"
case3=3
#w.Band3, "home; down; fill 0 0 0;flush"
Band3value=1'Black
case Brownr3$="set"
case3=3
#w.Band3, "home; down; fill 170 100 20;flush"
Band3value=10'Brown
case Redr3$="set"
case3=3
#w.Band3, "home; down; fill 255 0 0;flush"
Band3value=100'Red
case Oranger3$="set"
case3=3
#w.Band3, "home; down; fill 255 180 0;flush"
Band3value=1000
case Yellowr3$="set"
case3=3
#w.Band3, "home; down; fill 255 255 55;flush"
Band3value=10000
case Greenr3$="set"
case3=3
#w.Band3, "home; down; fill 45 157 40;flush"
Band3value=100000
case Bluer3$="set"
case3=3
#w.Band3, "home; down; fill 0 0 255;flush"
Band3value=1000000
case Violetr3$="set"
case3=3
#w.Band3, "home; down; fill 170 0 180;flush"
Band3value=10000000
case Grayr3$="set"
case3=3
#w.Band3, "home; down; fill 127 127 127;flush"
Band3value=100000000
case Whiter3$="set"
case3=3
#w.Band3, "home; down; fill 255 255 255;flush"
Band3value=1000000000
case else
if case1= 0 then notice "Please Select Color of Band 1"
wait
end select
vv=(Band1value+Band2value)*Band3value
if vv<=999 then Print #w.Ohm, "Resistance Value=";vv;" Ohm"
if vv>=1000 and vv<=999999 then Print #w.Ohm, "Resistance Value=";vv/1000;" K Ohm"
if vv>=1000000 then Print #w.Ohm, "Resistance Value=";vv/1000000;" M Ohm"
[Band4]
#w.Goldr "value? Goldr$"
#w.Silverr "value? Silverr$"
#w.Noner "value? Noner$"
select case
case Goldr$="set"
'case4=4
#w.Band4, "home; down; fill 250 235 90;flush"
Band4value=5'Black
case Silverr$="set"
'case4=4
#w.Band4, "home; down; fill 200 200 250;flush"
Band4value=10'Brown
case Noner$="set"
'case4=4
#w.Band4, "home; down; fill 21 228 255;flush"
Band4value=20'Brown
case else
'if case4= 0 then notice "Please Select Color of Band 3"
if case2= 0 then notice "Please Select Color of Band 2"
if case1= 0 then notice "Please Select Color of Band 1"
wait
'notice "Please Select Color"
end select
'if case4= 0 then notice "Please Select Color of Band 3"
if Band4value=5 then Print #w.Tole, "Tolerance Value =";" -+ ";Band4value;"%"
if Band4value=10 then Print #w.Tole, "Tolerance Value =";" -+ ";Band4value;"%"
if Band4value=20 then Print #w.Tole, "Tolerance Value =";" -+ ";Band4value;"%"
wait
[quit]
close #w
END
Chapter 1 - Starting the mission
Chapter 2 - Using arrays to reduce redundant code
Chapter 3 - Use variables for duplicate command strings
Chapter 4 - Consolidate event handlers
Chapter 5 - Apply mouse selection
Chapter 6 - Remove radio buttons
Chapter 7 - Use a single graphics box for the resistor display
Chapter 8 - Further reduce the amount of GUI controls (Using hot-spots)
Chapter 9 - Adding some eye-candy
Chapter 10 - Summary