• entries
191
861
• views
117867

# Good Programming Practices, Issue #1

125 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.

One would almost think you were still learning C#. [grin]

Two responses :)

1) Aren't we always learning?
2) I was just being lazy. This is something I already knew but didn't take too seriously.

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.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.