RE: The do's and don'ts of programming 08-28-2016, 03:05 AM
#9
(08-27-2016, 06:12 PM)God Wrote:Quote:Some languages (PHP, javascript, python, etc) allow you to declare a variable at runtime, and whenever you want. This is a neat little tool, but it isn't used as it should be. In essence, you should know [b]EXACTLY what variables you need from the start. Dynamic definitions is a scoping feature, NOT a convenience. If you have a function that does something, and you need three variables, don't define them the first time you use them, define them at the top of the function! This goes for languages like C as well (older versions actually forced this). I have been caught a few times forgetting a variable name and digging through it, it sucks, avoid it.
Could you give a python example with this. I haven't used python in a while and I just want to make sure I'm understanding what you mean. Thanks.
Code:
#!/usr/bin/python
integer_variable = 5
integer_variable2 = 3
integer_result = 0 #assigned during addition routine
######################
#addition routine
######################
integer_result = integer_variable + integer_variable2vs defining variables on the fly
Code:
#!/usr/bin/python
integer_variable = 5
integer_variable2 = 3
######################
#addition routine
######################
integer_result = integer_variable + integer_variable2 #integer_result isn't defined until the addition routine, considered bad practice by many, and will leave you open to debugging issues

























![[+]](https://sinister.li/images/modern/collapse_collapsed.png)