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 MAKE A SIMPLE TEXT TO SPEECH(TTS) WINDOWS PROGRAM USING C# PROGRAMMING LANGUAGE.

This post is a beginner's guide on how to get started with speech programming in visual studio using c# (c_sharp) programming language. For those who don't know what a programming language is, in a nut shell, a programming language is simply a command based computer language used for instructing a computer to do a particular job. When I say job, I mean very complex job. Write the above definition in an exam and stand a chance of losing marks. The definition isn't all there is about what programming language is, so I suggest you make a good search to learn what programming language really is. Though, this article is for beginners but I will say "BEGINNERS ARE CLASSIFIED", am a beginner. If you are a beginner who hasn't tasted code in his or her life before, I suggest you go start something. A good learning source is "tutorial point", they taught me a lot. Now for you who have tasted code, you will need the following: Computer System running wind...

HOW TO APPLY FALLING TEXT EFFECT IN A CONSOLE PROGRAM WITH THREADING IN C#

"A thread is defined as the execution part of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations then it is often helpful to set different execution paths or threads, with each thread performing a particular job.", says the smart guys behind Tutorials Point.Well, our program is not going to be complicated and we are not going to be performing any time consuming task, we are rather going be creating a camouflage application that gives us the effect similar to what you get when you ping a site or an IP address with the "-t" option in cmd or the effect you see in movies like Blacklist, 24, Person Of Interest and the likes.Now let's stop the ranting and get started.We are going be building this application upon our existing code, one we started in the previous article "How to grammatically Change the background and foreground color of a console Application".Now open up the code in...