kriasoft / aspnet-starter-kit

Cross-platform web development with Visual Studio Code, C#, F#, JavaScript, ASP.NET Core, EF Core, React (ReactJS), Redux, Babel. Single-page application boilerplate.
https://twitter.com/dotnetreact
MIT License
1.16k stars 203 forks source link

Do you prefer having `this.` prefixes in code? #3

Closed koistya closed 8 years ago

koistya commented 10 years ago

What is your preferred code style from these two: (prefix private fields with _, then reference them without using this.)

public class UserStore
{
    private readonly ApplicationDbContext _db;

    public UserStore(ApplicationDbContext db)
    {
         _db = db;
    }

    public IQueryable<User> Users
    {
        get { return _db.Users; }
    }

    public Task CreateAsync(User user)
    {
        _db.Users.Add(user);
        return SaveChanges();
    }
}

or

public class UserStore
{
    private readonly ApplicationDbContext db;

    public UserStore(ApplicationDbContext db)
    {
         this.db = db;
    }

    public IQueryable<User> Users
    {
        get { return this.db.Users; }
    }

    public Task CreateAsync(User user)
    {
        this.db.Users.Add(user);
        return this.SaveChanges();
    }
}
MadhureshKS commented 10 years ago

I was quite interested in using 'this' instead of '_' to prefix the private members of the classes that I write.

However, Resharper and other modern code-refactoring tools, suggest prefixing '_' (underscore) to make sure that it is a private member of the class.

So, going forward, I think it is better to use ' ' (underscore) than using 'this'. Interestingly, we also type less code if we use a ' ' (underscore).

optiklab commented 10 years ago

Hi guys, agreed about using '_'. This is pretty known standard which came from Microsoft if I'm correctly remember. And it is shorter and better noticeable if you're using editor w/o syntax highlight (but probably there are not so many guys use it).