Macros 宏与子程序
我试图了解在哪些情况下使用宏或子例程更好 例如,我正在创建一个程序来解析一个巨大的xml,它有数百个字段和属性,我正在定义子例程和宏来获取这些节点、属性等。因此这些子例程(或宏)被称为数千次 下面是我可以使用的示例子程序和宏 宏Macros 宏与子程序,macros,abap,Macros,Abap,我试图了解在哪些情况下使用宏或子例程更好 例如,我正在创建一个程序来解析一个巨大的xml,它有数百个字段和属性,我正在定义子例程和宏来获取这些节点、属性等。因此这些子例程(或宏)被称为数千次 下面是我可以使用的示例子程序和宏 宏 DEFINE xml_get_code_att_2. node = xml_node_iterator->get_next( ). while node is not initial. if lv_lastchild is not initial
DEFINE xml_get_code_att_2.
node = xml_node_iterator->get_next( ).
while node is not initial.
if lv_lastchild is not initial and node->get_name( ) eq lv_lastchild.
xml_node_iterator = xml_node->create_iterator( ).
exit.
endif.
if node->get_name( ) = &1.
clear: list, nodee.
list = node->get_attributes( ).
nodee = list->get_named_item( 'listID' ).
if nodee is not initial.
&2 = nodee->get_value( ).
endif.
node = xml_node_iterator->get_next( ).
endwhile.
if node is initial.
xml_node_iterator = xml_node->create_iterator( ).
endif.
END-OF-DEFINITION
子例程
FORM xml_get_code_att_2 USING p_name CHANGING p_listid
node = xml_node_iterator->get_next( ).
temp = node->get_name( ).
while node is not initial.
if lv_lastchild is not initial and node->get_name( ) eq lv_lastchild.
xml_node_iterator = xml_node->create_iterator( ).
exit.
endif.
if node->get_name( ) = p_name.
clear: list, nodee.
list = node->get_attributes( ).
nodee = list->get_named_item('listID' ).
if nodee is not initial.
p_listid = nodee->get_value( ).
endif.
endif.
node = xml_node_iterator->get_next( ).
endwhile.
if node is initial.
xml_node_iterator = xml_node->create_iterator( ).
endif.
endform.
那么,使用哪种方法更好呢?了解其中的区别很重要:宏是在编译时处理的,而表单(您可能更擅长使用方法)是在运行时处理的 至于您提出的建议:不要使用宏来构造代码。宏是要调试的解剖中的一个难题。宏最适合用于缩短单个指令或短代码段,而无需分支或循环。对于其他一切,使用方法(或表单,如果必须的话)
另外,要解析和处理XML,您可能需要检查现有的框架和技术…宏无法调试-我将不再使用宏。宏是SAP以前的R2主机世界遗留下来的一些东西
有了宏,你可以看到真正丑陋的东西。有一个trmac表,您可以在其中放置宏。您可以在任何abap代码中使用这些宏,如果其他人不知道TRMAC表,则可以驱动他们执行gracy。这是真正的SAP 80编程风格。不要这样做。+1当宏包含大量代码分支时,我无法忍受-突然停止正常工作。。。imo对他们来说唯一的一次是当您在执行非常重复的操作时,这些操作不包含任何超出非常基本的赋值或可能是内部表更新之外的内容。即使这样,我也会认真考虑使用一种方法或子。谢谢你的回答,并回答我所有的问题。本文展示了一个以“理智”方式使用宏的好例子: