Making code easier…

Sometimes you want to have your code more compact. Have a look at these code lines:

As you can see I have quite a few lines of code for a simple application, that throws out a message in a TextBlock, depending on my input in a TextBox. But I want to have that a little more compact, using less lines of code. So let me go ahead, comment those lines out, and write only two lines of code that allows my application to work the same way.

The green lines are commented out. That means though they still are there, they don’t do anything. They are just a comment now. You can see that something is commented out when the line starts with “//”. I just left it there, that I can compare.  I started with 10 lines of code, and replaced them with just two lines.

Now this is a very simple application. You type something in a TextBox, and after clicking a button you get a result in your TextBlock. If your code gets more complex you might want to use yet another way, the switch statement:

This code simply says, if case 1 is true, then you get this result, if case 2 is true, you get another result. You are switching cases. And if no defined case is true, then you get the default message.

Whatever way want to use is totally up to you. Just keep in mind that you want to keep your code as easy as possible.

To be continued…


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: