Skip to main content

WRITING A MAINTAINABLE CODE (C#)

Writing a maintainable code is very important in your application making, no matter how small you may imagine the program to be, always consider maintainability, even the largest programs start from small.You may have small idea at start up, but believe me, that idea may grow over time as the need for more functionality arises in your program, so its always very important to plan ahead.
.
So what plan are we talking about?
We are talking about writing a code that is maintainable, a code that can be extended easily.
The experts always write and talk about it, but have you considered what good it can do?
Ok, you know the benefits, have you tried putting it into practice?
You can write what ever you want (not recommended), but when you are getting into something you called a program, you need a lot of thinking.

Below is the list of things you need to consider when making your program
  1. Plan your naming convention: naming convention is very important, ensure your variable naming relates to what ever you are trying to model or something that makes sense to you and your code. I also recommend you follow the normal C# naming convention.
  2. Split your codes: try to split your code into chunk of codes that does specific job. e.g separating the code that encrypts from the code that decrypts. This is called separation of concern. Also get into the habit of compiling your code into DLL files and referencing it in your project.
To Demonstrate what I said, we are going to write a simple HELLO WORLD console program with three classes.

Lets start by creating a project named lesson.
Create a console program project in visual studio and name it Lesson.
We are just creating a simple hello world program with three classes, one class implements a functionality that prints the hello world in the console screen while the other implements a functionality that speaks the word using Text To Speech (TTS) technology and the last implements the speech and text function
Note: Am not teaching you how to program in c#, I also assume that you are not a total beginner as such, I expect you to compile those three classes into DLL files and reference them on our lesson project.

Add a new class and make a code that looks like this:


using System; using System.Collections.Generic;
 using System.Linq; 
using System.Text;
 using System.Threading.Tasks;
 using System.Speech.Synthesis;
 namespace Lesson 


//this is the class that handles the talking functionality

class SpeechClass 
 { 
 private string whatToSay = "Hello World!";

SpeechSynthesizer ss = new SpeechSynthesizer(); 

 public void SayIt() 
 { 
 ss.SpeakAsync(this.whatToSay); 

 Console.WriteLine("Robot Speaking...."); 

 Console.ReadLine();
 }



}
In the above code, notice how we declared the whatToSay variable private and accessed it in the public method "SayIt()" using the "this" keyword. I did this because I do not want a direct access to that variable. This is what is called encapsulation. I recommend you do that where necessary.

Our second class will look like this:


using System; using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 namespace Lesson
 { 

 class WrittenWord 
 { 

 private string whatToPrint = "Hello World"; 

 public void PrintIt() 
 {

 Console.WriteLine(this.whatToPrint);
 } 
 } 
}

notice "this" keyword again.

To be creative, lets add the third class, a class that does both the writing and speaking all at once.

we will call this one SpeechANDtext and it will look like this:

using System; 
using System.Collections.Generic; 
using System.Linq; using System.Text; 
using System.Threading.Tasks; 
using System.Speech.Synthesis; 
namespace Lesson 


SpeechANDtext 

 {
 SpeechSynthesizer ss1=new SpeechSynthesizer(); 

 private string what2Say = "Hello World"; 
 private string what2Print = "Hello world!"; 


 public void Say_What2SayANDwhatToPrint() 
 { 
 ss1.Speak(this.what2Say); 
 Console.WriteLine(this.what2Print); 

 }
 } 
}

Note: To be able to use the speech technology, you need to reference the System.Speech assembly.


We can now call the methods of those class we created from our "Program" class which will look like this:


using System;

 using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.Threading.Tasks; 

using System.Speech.Synthesis; 

namespace Lesson 



 class Program 

 { 

 private static string userInput, userInput1;
 private static SpeechSynthesizer ss2 = new SpeechSynthesizer();
 static void Main(string[] args)

 {

 Console.WriteLine("Welcome to the Hello world program by Kingsley, \nthis program makes decision on how to say the greetings" + "\"Hello World\" based on how you want it say it" + "\n whether you want it to say it or write it in this console or you want it to say your own words, the choice is yours. Use the following options to make your choice."+ "\n S = Speak\n D = Display\n B= Speak and Display \n C = this is a custom mod that allows to enter what your own words.");

 userInput = Console.ReadLine();

 switch (userInput.ToUpper())

 {

 case "S":
 SpeechClass sc = new SpeechClass();
 sc.SayIt();
 break;

 case "D":
 WrittenWord wd = new WrittenWord();
 wd.PrintIt();
 break;

 case "B":
 SpeechANDtext sNt = new SpeechANDtext();
 sNt.Say_What2SayANDwhatToPrint();
 break;

 case "C":

 Console.WriteLine("Please enter what you would want the prgram to say.");
 userInput1 = Console.ReadLine();

 ss2.Speak(userInput1);
 break;

 default: SpeechSynthesizer ss3 = new SpeechSynthesizer();
 ss3.Speak("Sorry, you entered an invalid option. please try again later. Thank you"); Console.WriteLine("Sorry, you entered invalid option");

 break;
 }
 }
 }
}

Here is where our little program comes to an end, notice that we added some decision making functionality which gives us control over what we want, whether we want the text spoken or printed the choice is ours, we also added a functionality that actually allows us to enter our own text and get it back as speech. This function would have been implemented in one class  and one method, but because we wanted a maintainable code, we decided to split our functions. Go through the code and see how easy it will be if you want to add more functionality, all you need do is create a class that contains the functions you want and call those functions from our "program" class. Its just a plug and play.

CONCLUSION
Since am a learner, I believe the method I used in this walk-through is still not the best practice. So, I highly welcome your correction and criticism for thats what makes me better.

Thanks for reading.

Comments

Popular posts from this blog

HOW TO WRITE, COMPILE AND RUN C++ CODE ON LINUX KALI

Developing a C++ Program on Kali Linux Without Installing Additional Software This article is for hackers who want to develop a C++ program on Kali Linux without installing any additional software. Some might say you need to install a separate compiler or extra tools to write and run a simple C++ program on Kali Linux. However, I’ll show you how to do it right out of the box. Pre-installed C++ Compiler in Kali Linux Kali Linux comes with a pre-installed C++ compiler called g++ . We will use this to write and compile a basic "Hello, World!" program. Step 1: Check if g++ is Installed Open your terminal and run the following command: g++ -v If the compiler is installed, you should see version details. If not, you will get an error message. Step 2: Create a C++ File In your terminal, type: nano MyCpp.cpp This will create a C++ file and open it in the Nano editor. Step 3: Write the C++ Code Once Nano opens, enter the following C++ code: #include <...

HOW TO INSTALL BLOGENGINE.NET WITH VISUAL STUDIO.

Yes there are lots of article on the internet that already describes how to do this, but that wont stop me from sharing my own idea on how I got mine rocking with visual studio 2012. Now here is the story, I woke up one morning and discovered I was departing from being a beginner programmer to a programmer who is at least worthy to be called a programmer. So I felt I needed to share my programming experience and the only known way I could do that, was to start blogging, hmmm... Just like you, I didn't like blogger, I needed  to be my own boss. So, I started my own blog project, but wait....  Why was I doing that when there are open source blog projects being developed and maintained by developers who are more "programmatically" experienced than I?. BlogEngine was the one I chose and the installation was a breeze. All I did was go to the site, downloaded the first zip file which is tagged "web", extracted it to my visual studio project directory, opened vi...

LOAN MANAGEMENT SYSTEM (My Project Idea)

There are many companies that specializes on giving loan to people; they make their profits by collecting interest on any money they lend out, and before a loan can be given to any customer, the customer must provide a collateral and some details such as:  contact details and a reference/guarantor. When the loan is finally given to the customer, they charge interest based on the the amount given to the customer for a given period of time which could be daily, weekly, monthly or yearly.  For example, a company could be collecting interest based on a particular amount for a given period of time like: $200,000 loan would have an interest tag of $2,000  per month until the money is returned, and $100,000 would have an interest of $1,000 per month until the money is returned.  In  addition, the customers need to know when the time for them to pay their interest comes e.g.  at the end of the month or the timing model with which the calculation is being made. Usua...