Dec-01-2017, 03:34 AM
I just came across the book Code Complete, 2nd Ed on amazon. It comes with a checklist to ensure you write maintainable code.
Under Chapter 10: General Issues In Using Variables
Other General Issues in Using Data (p. 17)
One of the points says:
When referring to variable scope, do they mean large scale applications and using classes to achieve encapsulation? Suppose I had a script that doesn't use classes, but a
How would you properly declare it to limit the variable scope as suggested by the checklist/book?
When should you worry about variable scope?
Under Chapter 10: General Issues In Using Variables
Other General Issues in Using Data (p. 17)
One of the points says:
Quote:Do all variables have the smallest scope possible?
When referring to variable scope, do they mean large scale applications and using classes to achieve encapsulation? Suppose I had a script that doesn't use classes, but a
List
and functions to do certain tasks, example, a backup script:def read_data_from_file(file_to_read): #code to read from file and build list return list_of_files def copy_files_to_backup_folder(file_to_copy): #code to copy files file_name="some/path/to/file" files_to_backup = read_data_from_file(file_name) for f in files_to_backup: copy_files_to_backup_folder(f)Do you have to worry about variable scope when writing scripts for common admin tasks? In this case
file_name
? How would you properly declare it to limit the variable scope as suggested by the checklist/book?
When should you worry about variable scope?