5.) Get rid of unnecessary blank lines
6.) Use a three char prefix for the constants
7.) Get rid of unnecessary type declarations (.l, .b, etc.). These are completely useless and just slow down the program.
You don't need to assign a type at all because the native type will
always be LONG (4 Byte) at x86 and QUAD (8 Byte) on x64.
Only use explicit type declarations for structures to save memory.
8.) Get rid of unnecessary calculations. No need for this
Code:
newentry = newentry + zip$
just do it like this
Code:
newentry + zip$
9.) No need to set variables to NULL or #False, they are empty by default
10.) I know programmers are lazy but constantly writing
everything in lower case is a pain in the ass since it deceases readability
11.) Use the
: character to convert a bunch of short single commands to a single line
So this
Code:
Ares$ = ""
License$ = ""
becomes this
Code:
Ares$ = "" : License$ = ""
My final take:
Code:
EnableExplicit
Enumeration
#WIN_Main
EndEnumeration
Enumeration
#TXT_Name
#TXT_Mail
#TXT_City
#TXT_State
#TXT_Zip
#TXT_Email
#TXT_Call
#STR_Name
#STR_Address
#STR_City
#STR_State
#STR_ZIP
#STR_Email
#STR_CallSign
#CheckBox_ARESInfo
#CheckBox_LicInfo
#BTN_Add
#BTN_Exit
#Text_Counter
#Text_Confirm
EndEnumeration
Global Quit, FileLength = 0
Define Event
Procedure countlines()
FileLength = 0
If OpenFile(0, "c:\purebasicwork\safetyfair\efair2.txt")
While Eof(0) = 0
FileLength + 1
ReadString(0)
Wend
CloseFile(0)
EndIf
EndProcedure
Procedure clear_fields()
SetGadgetText(#STR_Name, "")
SetGadgetText(#STR_Address, "")
SetGadgetText(#STR_City, "")
SetGadgetText(#STR_State, "WA")
SetGadgetText(#STR_ZIP, "")
SetGadgetText(#STR_Email, "")
SetGadgetText(#STR_CallSign, "")
SetGadgetState(#CheckBox_ARESInfo, #PB_Checkbox_Unchecked)
SetGadgetState(#CheckBox_LicInfo, #PB_Checkbox_Unchecked)
EndProcedure
Procedure MainProc()
Protected Event, Ares$, License$, Name$, Address$, City$, State$, Zip$, Email$, CSign$, Newentry.s
If OpenWindow(#WIN_Main, 389, 176, 759, 397, "TC ARES - Emergency Preparedness Expo 2010", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
TextGadget(#TXT_Name, 120, 60, 100, 20, "Name ", #PB_Text_Right)
TextGadget(#TXT_Mail, 110, 90, 110, 20, "Mailing Address ", #PB_Text_Right)
TextGadget(#TXT_City, 110, 120, 110, 20, "City ", #PB_Text_Right)
TextGadget(#TXT_State, 360, 120, 50, 20, "State", #PB_Text_Right)
TextGadget(#TXT_Zip, 500, 120, 70, 20, "Zip Code", #PB_Text_Right)
TextGadget(#TXT_Email, 100, 150, 120, 20, "Email ", #PB_Text_Right)
TextGadget(#TXT_Call, 100, 180, 120, 20, "Ham Call sign (if any) ", #PB_Text_Right)
StringGadget(#STR_Name, 220, 60, 160, 20, "")
StringGadget(#STR_Address, 220, 90, 230, 20, "")
StringGadget(#STR_City, 220, 120, 120, 20, "")
StringGadget(#STR_State, 420, 120, 30, 20, "WA")
StringGadget(#STR_ZIP, 580, 120, 90, 20, "")
StringGadget(#STR_Email, 220, 150, 190, 20, "")
StringGadget(#STR_CallSign, 220, 180, 120, 20, "")
CheckBoxGadget(#CheckBox_ARESInfo, 220, 220, 260, 20, "Please send me ARES membership information")
CheckBoxGadget(#CheckBox_LicInfo, 220, 250, 290, 20, "Please send me information on obtaining a ham license")
ButtonGadget(#BTN_Add, 350, 310, 80, 40, "Add Me!")
ButtonGadget(#BTN_Exit, 670, 360, 50, 20, "Exit")
TextGadget(#Text_Counter, 20, 360, 30, 20, "", #PB_Text_Center)
TextGadget(#Text_Confirm, 660, 330, 70, 20, "")
SetActiveGadget(#STR_Name)
countlines()
SetGadgetText(#Text_Counter, Str(FileLength))
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #BTN_Add
Ares$ = "" : License$ = ""
Name$ = GetGadgetText(#STR_Name)
Address$ = GetGadgetText(#STR_Address)
City$ = GetGadgetText(#STR_City)
State$ = GetGadgetText(#STR_State)
Zip$ = GetGadgetText(#STR_ZIP)
Email$ = GetGadgetText(#STR_Email)
CSign$ = GetGadgetText(#STR_CallSign)
If GetGadgetState(#CheckBox_ARESInfo) = #PB_Checkbox_Checked
Ares$ = "ARES YES"
EndIf
If GetGadgetState(#CheckBox_LicInfo) = #PB_Checkbox_Checked
License$ = "LICENSE YES"
EndIf
NewEntry = Name$ + "|" + Address$ + "|" + City$ + "|" + State$ + "|";
NewEntry + Zip$ + "|" + Email$ + "|" + CSign$ + "|"
NewEntry + Ares$ + "|" + License$
If OpenFile(0, "c:\purebasicwork\safetyfair\efair2.txt" )
FileSeek(0, Lof(0))
WriteStringN(0, newentry)
CloseFile(0)
Else
MessageRequester("FATAL ERROR!", "Unable to oopen or create output file!!!", #PB_MessageRequester_Ok)
Quit = #True
EndIf
countlines()
SetGadgetText(#Text_Counter, Str(FileLength))
clear_fields()
SetActiveGadget(#STR_Name)
Case #BTN_Exit
Quit = #True
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow Or Quit = #True
EndIf
EndProcedure