将 3 个功能合并为一个

发布于 2024-10-23 14:12:37 字数 786 浏览 5 评论 0原文

我有3个功能。如何将它们合并为一个? 我有函数 3 中的表名称列表。函数 2 具有与该表相对应的节点名称,函数 1 具有每个表中的列数。 我更喜欢一个具有所有 3 个输入值的函数,而仅根​​据函数输入传递的线索来使用所需的值。

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
}

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 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒废 2024-10-30 14:12:37

使用数组来存储表名称和属性。创建第四个函数来读取表名称等并将其存储到这些数组中。然后其他三个函数就可以访问这些数组。

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
}

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
}
尸血腥色 2024-10-30 14:12:37

我真的看不出这些功能如何相互依赖。然而,拥有更多、更小的功能而不是一个大的功能应该是一种好的风格。

关键词:关注点分离,一种工具做一项工作。

如果函数较小,则更容易推理它们。测试它们更容易。找到一个地方来重用其中一个会更容易。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文