Sign in to follow this  
  • entries
    191
  • comments
    861
  • views
    116948

Good Programming Practices, Issue #1

Sign in to follow this  
Mithrandir

96 views

I am currently refactoring what was once a simple and small project, and has since turned into something else. When I first started on the project, I said to myself "Well, it's small and simple, it won't be doing anything really important, I can loosen up my coding standards to make it easier to program!".



Well, I ignored my own advice, and it has come back to bite me in the arse. The advice? In my C# book, I wrote that you should ALWAYS use properties, ALWAYS. Never ever ever use a public variable for anything, even in a small struct. Sure it's a little extra coding now, but you'll thank yourself later on.

And boy, I was right.


It turns out that if I had used properties, I would have saved myself a hell of a lot of time when the project took an unexpected turn. Oh and it gets even better; the very fact that I used public variables actually influenced the way I designed the project, and the sheer amount of sanity-checking code ballooned out of control.



So anyways, properties are amazing, yadda yadda, use them for everything. Oh and if you didn't already know, if you have a simple property:


public int Property
{
get { return property; }
set { property = value; }
}



The .net JIT optimizes that like if it were a direct access, and not a function call. So you can't use performance as an excuse to not use them.

Good day.
Sign in to follow this  


3 Comments


Recommended Comments

Quote:
Two responses :)


1) Aren't we always learning?

Hopefully, some people make me wonder though.
Quote:
2) I was just being lazy. This is something I already knew but didn't take too seriously.

That being the bigger problem, as you found yourself. Being lazy usually ends up costing more than it is worth.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now