Jump to content

Is "this" Important?


brandon3055

Recommended Posts

I have noticed in a lot of the mods i have looked at they seem to use this.field/method a lot e.g.

public void markDirty ()
    {
        buildTool(toolName);
        if (this.worldObj != null)
        {
            this.blockMetadata = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);
            this.worldObj.markTileEntityChunkModified(this.xCoord, this.yCoord, this.zCoord, this);
        }
    }

(Example is from tconstruct)

I understand how it works but is it actually necessary? I would like to know because i dont use it almost at all in my mod except in constructors.

I am the author of Draconic Evolution

Link to comment
Share on other sites

good practice in case a parameter name conflicts with an attribute (in which case it'd only set the variable in the scope of the function itself). if the variable names are different then it's not technically necessary but it still helps with remembering which scope you're dealing with (class or function)

Link to comment
Share on other sites

Hi

 

Largely a style choice, I don't think there's any consensus.  Personally I think it adds unnecessary clutter, and scope hiding is asking for trouble, so I only add it if my intentions are unusual and I want to make them obvious.  Others find it more clear to use this all the time.  Still others give their member variables a prefix eg m_blockMetadata.  If you're consistent I reckon it doesn't matter.

 

-TGG

 

 

Link to comment
Share on other sites

Yeah I don't like it either style-wise. 

 

There are three cases I know of where you DO need the keyword this:

 

1. To be explicit that you're referring to an object field in the case where there is some other local or super variable with same name.  Like if you had an int field in your object called counter and then you had a for loop with its own index also called counter and you needed to access the object's counter field from within that loop.

 

2. To refer to the current object as a parameter to a method you're calling.  For example, if you're coding within a custom entity and adding ai tasks you would have:

tasks.addTask(0, new EntityAISwimming(this));

 

3. If you're calling a constructor of your object from within it.

    public ItemGoldenEgg() 
    {
	this("Golden Goose", 0xF5E16F, 0xF5F56F);
}
    
    public ItemGoldenEgg(String parEntityToSpawnName, int parPrimaryColor, int parSecondaryColor)
    {
        this.maxStackSize = 16; // same as regular egg
        this.setCreativeTab(CreativeTabs.tabMisc);
        colorBase = parPrimaryColor;
        colorSpots = parSecondaryColor;
    }

 

The general use of "this" all over the place is, as TheGreyGhost notes, arguable.  Some people think it makes it more readable, some like me and TGG think it makes it less readable (due to clutter).  But shouldn't create any issue with your code either way.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I don't like it either, but sometimes if I have trouble remembering the name of a variable or method "this." brings up the options and helps me out.  I go back and delete the 'this' at that point.  However, its not really hurting anything.  I just think it looks ugly.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

In reality, 99%of the time when you see this in mods, its copy/pasta from vanilla code and people not knowing better.

But people are right there are cases where its NEEDED (Local variables shading class variables)

It's largely a style choice, however most 'modders' learn from decompiled code {horrible btw} which uses excessive 'this' to guarantee there are no shading issues.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.