宏与子程序

Mto*_*tok 2 macros sap abap

我试图了解在哪种情况下宏或子程序是一个更好的主意使用.

例如,我正在创建一个程序来解析一个巨大的xml,它有数百个字段和属性,我正在定义子程序和宏来获取那些节点,属性等.所以这些子程序(或宏)被调用数千次.

这是我可以使用的示例子例程和宏;

MACRO

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
Run Code Online (Sandbox Code Playgroud)

子程序

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.
Run Code Online (Sandbox Code Playgroud)

那么哪一个更好用?

vwe*_*ert 10

理解差异很重要:宏在编译时处理,而表单(您可能更好地使用方法)在运行时处理.

至于建议你问:你不使用宏来构造代码.宏是一个痛苦的... ...自然调试.宏最好用于缩短单个指令或短代码片段而无需分支或循环.对于其他一切,使用方法(或表格,如果必须).

另外,要解析和处理XML,您可能需要检查现有的框架和技术......

  • 当宏包含巨大的代码分支时,我无法忍受它 - 突然停止正常工作......对于他们来说唯一的时间就是当你做*非常*重复的操作时,它们不包含任何超出基本任务的东西,或者可能是内部表更新.即使这样,我也会认真考虑使用方法或子方法. (2认同)