Some Sugar with your Syntax?

August 11, 2010 by Dave
Filed under: C#, Crappy Code 

In my recently posted tutorial, I created the render state objects using some old-school syntax.

StencilAlways = new DepthStencilState();
StencilAlways.StencilEnable = true;
StencilAlways.StencilFunction = CompareFunction.Always;
StencilAlways.StencilPass = StencilOperation.Replace;
StencilAlways.ReferenceStencil = 1;
StencilAlways.DepthBufferEnable = false;

You can do this in a much prettier way nowadays.

StencilAlways = new DepthStencilState()
{
  StencilEnable = true,
  StencilFunction = CompareFunction.Always,
  StencilPass = StencilOperation.Replace,
  ReferenceStencil = 1,
  DepthBufferEnable = false
};

That is so much nicer to read. I’ve seen this syntax before, but 20 years of habits die hard and I rarely remember to use it. Hopefully it will stick now, and make my crappy code that much less crappy.

Comments

2 Comments on Some Sugar with your Syntax?

  1. ZMan on Wed, 11th Aug 2010 9:45 am
  2. You should get yourself a copy of resharper – it will notice the 1st one and suggest you change it to the second one and then fix it for you with one click.

  3. Dave on Wed, 11th Aug 2010 10:39 am
  4. Thanks for the tip, I’ll have to give that a try.

Tell me what you're thinking...