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
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:
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:
we will call this one SpeechANDtext and it will look like this:
We can now call the methods of those class we created from our "Program" class which will look like this:
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.
.
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
- 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.
- 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.
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
Post a Comment