I have 3 functions. How do I merge them into one? I have the list of table names in the function 3. The function 2 has node name that corresponds to that table and the function 1 has number of columns in each of the tables. I will prefer one function with all 3 input values while using the required ones only depending upon 开发者_StackOverflow社区the clue passed by the function input.
function_1()
{
while read tbl col_num
do
mysql test -e"call mysql.createtable('$tbl', $col_num);"
done << mytbl_list
test.authadv 45
test.fee 29
test.finadv 54
mytbl_list
}
# match tables names with the respective nodes
function_2()
{
while read tbl tag
do
php -e xmlread_new.php $filename $tbl $tag
done << heredc
test.authadv AUTHADV
test.fee FEE
test.finadv FINADV
heredc
}
# export data to excel. Change the table names as required
function_3()
{
while read tbl_name
do
mysql --table -e"select * from $tbl_name" > $report_name.txt
done << tbl_heredoc
test.authadv
test.fee
test.finadv
tbl_heredoc
}
Use arrays to store your table names and attributes. Create a fourth function to read and store your table names, etc., into those arrays. Then the other three functions can access those arrays.
read_tables ()
{
counter=0
while read -r tbl col_num tag
do
tables[counter]=$tbl
col_nums[counter]=$col_num
tags[counter]=$tag
((counter++))
done << 'TABLES'
test.authadv 45 AUTHADV
test.fee 29 FEE
test.finadv 54 FINADV
TABLES
}
create_tables ()
{
for ((i=0; i<counter; i++))
do
mysql test -e"call mysql.createtable('${tables[i]}', ${col_nums[i]});"
done
}
# match tables names with the respective nodes
match_tags ()
{
for ((i=0; i<counter; i++))
do
php -e xmlread_new.php "$filename" "${tables[i]}" "${tags[i]}"
done
}
# export data to excel. Change the table names as required
export_excel ()
{
for ((i=0; i<counter; i++))
do
mysql --table -e"select * from ${tables[i]}" > "$report_name.txt"
done
}
I can't really see how the functions depend on each other. However, it is supposed to be good style to better have more and smaller functions instead of one big one.
Keywords: Separation of concerns, one tool for one job.
If you have smaller functions, it's easier to reason about them. It is easier to test them. It is easier to find a place to reuse one of them.
精彩评论