0
Assuming.. TnT
Posted by Erik Gaius,aka [ K i r E ]
on
Friday, February 13, 2009, 3:00 AM
in
Tipz n Trix
Sensya na, absent akuh khapon.. nsira ksi keyboard kuh ~_~ Anyway, here's the programming tip of the day: Never Assume! Eto guilty ako dito: palagi kong iniisip kung ano ang susunod na mangyayari. Gumugulo tuloy ang code..
Sabihin nang kailangan mo ng function na marunong mag add:
And so on and so forth.. after 10 years, nag dedebug ka parin ng code.. kahit na ang kailangan mo lang talaga ay eto:
Sabihin nang kailangan mo ng function na marunong mag add:
Function Add(X as int, Y as int) as intPano kung may decimal ung ininput? Dpat siguro double ung data type..
Return X+Y
End Function
Function Add(X as dbl, Y as dbl) as dblMas magnda siguro kung hindi lng addition ang kayang gawin ng Function ko..
Return X+Y
End Function
Function Solve(X as dbl, Y as dbl, op as str) as dblAno ba yan, bad programming practice ang gumamit ng string sa Switch Case... dpat Enumerated Type!
Switch op
Case "Add": Return X+Y
Case "Subtract": Return X-Y
Case "Multiply": Return X*Y
Case "Divide": Return X/Y
Case Else: Return Nothing
End Switch
End Function
Enum OperationWahh, pusible mag Divide by Zero! dpat may error checking..
Add
Subtract
Multiply
Divide
End Enum
Function Solve(X as dbl, Y as dbl, op as Operation) as dbl
Switch op
Case Operation.Add: Return X+Y
Case Operation.Subtract: Return X-Y
Case Operation.Multiply: Return X*Y
Case Operation.Divide: Return X/Y
Case Else: Return Nothing
End Switch
End Function
And so on and so forth.. after 10 years, nag dedebug ka parin ng code.. kahit na ang kailangan mo lang talaga ay eto:
Function Add(X as int, Y as int) as intE papano kung nangyari ung isa sa mga scenario sa taas!? Edi tska mo ayusin. Cross the bridge when you get there ika nga. Focus lang, tandaan nyo: Less Code, Less Errors, More Fun!
Return X+Y
End Function
Post a Comment