Cleaner Code through Less Variables

by percent20 10/31/2007 6:03:00 PM
I am often times wondering how I can write cleaner better code that is effective, but not reeeeealy long. As a beginner this is something you wish you can do, but blow it off until you get better at programming feeling eventually you’ll “get it”.

Often times with beginners there might be code that looks like this.

public void Display(string s1, string s2)
{
    string sentence1 = s1;
    string sentence2 = s2;
    Console.WriteLine(sentence1 + ". " + sentence2);
}

While this may look odd there are plenty of examples of SIMILAR code in beginners. And as you have probably figured out an easy way to shorten the code would be to do this.

public void Display(string s1, string s2)
{
    Console.WriteLine(s1 + ". " + s2);
}

The code looks cleaner and slimmer and a little easier to understand. So I recommend to write cleaner looking code try to get rid of as many variables as you can, but be careful not to take away to many.

Again I just want to re-iterate my purpose for these types of posts is to help beginners in places I wanted/needed help on with I first started, and still need help on to some degree.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/7/2008 1:16:59 PM

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen


My Flare

AddThis Feed Button

National Blog Posting Month

Eagle Scout

I'm Test Driven

[Reserved for MVP status I want to earn]

View Buddy Lindsey's profile on LinkedIn

Twitter



Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in