Is there a better way to generic some similar task

my_tans.sv


   rand bit[47:0] dmac;
   rand bit[47:0] smac;
   rand bit[15:0] ether_type;
   rand byte      pload[];
   rand bit[31:0] crc;

In a base seq there is a task;


virtual task cfg1(bit[47:0] dmac=0);
      my_transaction tr;
      start_item(tr);
      tr = new("tr");
      assert(tr.randomize());
      tr.dmac=   dmac;
      finish_item(tr);
      end
   endtask


virtual task cfg2(bit[47:0] dmac=0,
 bit[15:0] ether_type=0
);
      my_transaction tr;
      start_item(tr);
      tr = new("tr");
      assert(tr.randomize());
      tr.dmac=   dmac;
      tr.ether_type=   ether_type;
      finish_item(tr);
      end
   endtask

I find similar way to write 2 similar task,
I think it is not a good way to do so. but can not find a better solution yet.

In reply to designer007:

Could you please explain what your objective is, because you are doing the same in principle in both tasks.

In reply to chr_sue:

I give an example simplified


virtual task cfg1(bit[47:0] dmac=0);
      my_transaction tr;   //call serial function /task block1
      start_item(tr);
      tr = new("tr");
      assert(tr.randomize());
      tr.dmac=   dmac;  //assign one field to specific value 
      finish_item(tr);  //call serial function /task block2
      
   endtask

virtual task cfg2(bit[47:0] dmac=0,
 bit[15:0] ether_type=0
);
      my_transaction tr;   //call serial function /task block1
      start_item(tr);
      tr = new("tr");
      assert(tr.randomize());
      tr.dmac=   dmac;
      tr.ether_type=   ether_type;  //this one can assign 2 field to specific value 
      finish_item(tr);   //call serial function /task block2
      
   endtask

and I read something of design pattern.
do not copy your code.
the two task is the same except task cfg2 add another parameter assign
(tr.ether_type= ether_type;)
or it is the same for the 2 parts call serial function /task block1 and
call serial function /task block2

so I try to find a better solution.

In reply to designer007:

take the some part to function
if there is some bug in f1 , we only have to modify the code in one place(f1), not everywhere


function f1();
my_transaction tr;   //call serial function /task block1
      start_item(tr);
      tr = new("tr");
      assert(tr.randomize());
endfunction
function f2();
finish_item(tr);
endfunction


virtual task cfg1(bit[47:0] dmac=0);
      f1();
      tr.dmac=   dmac;  //assign one field to specific value 
      f2();
   endtask
virtual task cfg2(bit[47:0] dmac=0,
 bit[15:0] ether_type=0
);
      f1();
      tr.dmac=   dmac;
      tr.ether_type=   ether_type;  //this one can assign 2 field to specific value 
     f2();
     
   endtask


this is a better way , but i do not know if there is some other solution