Verification Martial Arts: A Verification Methodology Blog

Do they diss this “this”?

Posted by Janick Bergeron on June 9th, 2008

One of the issues with open-sourcing, is that everyone gets to see the code you write.

So now that you have had the chance to look at the VMM source code, you probably have noticed what a former co-worker once described as “a compulsive use of “this” in method implementations“. Why is it that I indeed compulsively use “this” whenever I refer to a data member in a class? As with every coding guideline, there are pros and cons. This one is no exception.

First of all, a bit of background, for those of you who may not be familiar with “this”… Those of you who are, can skip forward…

When referring to a variable or function/task that is declared as a member of a class from within that same class, you can prefix the reference with “this.”. For example:

class packet;
bit [47:0] da;
function new(bit [47:0] da);
this.da = da;
endfunction
function void display();
$write("DA = %h\n", this.da);
endfunction
endclass

“this” is an implicitly defined handle to the object instance that is executing the procedural code. In the constructor of the above example, “this.da” is an explicit reference to the “packet::da” variable contained in the class. Using the “this.” prefix makes it clear that I am not referring to the “da” argument. In this case, the use of “this.” is not optional as it is needed to differentiate between the constructor argument and the data member. It also avoids having to come up with different names for the constructor argument and data member when in fact they both represent the same thing.

I choose to use the “this.” prefix even in cases where it is clearly optional. For example, in the “packet::display()” method, because there are no other variables named “da” in the scope of the function, it would be clear that a simple reference of “da” would refer to the packet::da variable.

I always use the “this.” prefix to document that the variable or method referred to is a member of the class and not some variable/task/function inherited from some larger scope. In the simple example above, it is easy to see that the “da” variable is a class data member. But what if the procedural code making use of the “packet::da” variable and the declaration of that variable where separated by several dozens — or hundreds — of lines? What if they were in different files? Consider the code below. Using “this.”, despite being option, clearly document the nature of the “stream_id” and “fcs” variables.

class bad_frames_in_stream_0 extends eth_frame;
rand bit is_bad;

constraint some_bad_frames {
if (this.stream_id == 0 && this.is_bad) this.fcs != 0;
}

endclass

Share and Enjoy:
  • Google Bookmarks
  • Facebook
  • Digg
  • Print
  • email
  • Live
  • LinkedIn
  • MySpace
  • Reddit
  • Technorati
  • NewsVine
  • Twitter

4 Responses to “Do they diss this “this”?”

  1. JL Gray Says:

    Janick,

    I also frequently use “this” when programming (where it doesn’t conflict with the coding style of the surrounding code). Next time someone asks me why I can now say, “Well, Janick does it, so I can too!” ;-) .

    JL

  2. Martin Says:

    “this.da” is an explicit reference to the “packet::da” variable contained in the class

    Is it correct to say that “this.da” represent the da value of the instance being dereferenced, while “packet::da” represents the da value of the class without an instantiation? Can you reference da and obtain its value without a constructed instance?

  3. janick Says:

    You can refer to the “da” in the packet class as “packet::da” without a constructed instance only if it were a static property (i.e. shared by all instances). Static data members are like global variables. Non-static data members can only be referenced through an instance, i.e. “this.da” or “pkt.da”.

  4. David Robinson Says:

    Hi Janick,
    good article. Use of “this.” is a pet hate of mine, so I decided to rise to your challenge and diss this. http://www.oh-onemorething.com/2008/06/dissing-this.html

    Cheers
    David

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>