Smart Coding, Think Modular

Today, writing code is no rocket science. Any body can write code but being efficient is the key. Read my blog here: Is Google Stealing our Intelligence?

 

The first step towards becoming efficient is think modular. Think about squeezing the size of code you write to perform an operation.

 

Here is an example.

 

I saw some code for show and hide some controls on a Form. This code simply checks if a CheckBox is checked, then make some controls visible and if the CheckBox is not checked, hide them.

 

private void RefreshButton_Click(object sender, EventArgs e)

{

    if (checkBox1.Checked)

    {

       button1.Visible = true;

       listBox1.Visible = true;

       radioButton1.Visible = true;

     }

     else

     {

        button1.Visible = false;

        listBox1.Visible = false;

        radioButton1.Visible = false;

      }

 }

I replaced the above code with below code by adding an extra private method:

private void RefreshButton_Click(object sender, EventArgs e)
{
   ShowHideControls(checkBox1.Checked);   
} 

private void ShowHideControls(bool Show)

{

     button1.Visible = Show;

     listBox1.Visible = Show;

     radioButton1.Visible = Show;

}

This is not first time I saw this type of code. I have seen similar code is being written again and again.

First key in becoming an efficient coder is, think modular. Think, think and think. Don't just copy and paste code from Google. If you do copy, think if code you copying is not only doing what it supposed to do but effectively.

Stay tuned for more tips.

Cheers!

 

Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.