Login Register






Tutorial The do's and don'ts of programming filter_list
Author
Message
RE: The do's and don'ts of programming #10
(08-28-2016, 03:05 AM)phyrrus9 Wrote:
(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_variable2

vs 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

Thank you for clarifying. I'm pretty sure I'm guilty of this as I started with Python and I for one thought not needing to initialize variables was part of the convenience of the language.
"If you look for the light, you can often find it. But if you look for the dark, that is all you will ever see.”


Reply





Messages In This Thread
The do's and don'ts of programming - by phyrrus9 - 08-23-2016, 11:35 AM
RE: The do's and don'ts of programming - by 0M4R - 08-27-2016, 03:39 PM
RE: The do's and don'ts of programming - by Nil - 08-27-2016, 06:12 PM
RE: The do's and don'ts of programming - by Nil - 08-28-2016, 04:03 AM



Users browsing this thread: 1 Guest(s)