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

Microsoft will allow you to upgrade your pirated Windows 7 or 8 to full Windows 10 for little or no amount.

Microsoft's latest Operating System has been trending for a while now, while some are still contemplating on who gets a free upgrade from the two previous Microsoft's Operating Systems just like the company promised and who wouldn't, Microsoft has announced that the upgrade to Windows 10 would be free for genuine Windows 7 or 8 users and there is a good news for pirated Windows 7 or 8 users, they too might go to the Promised Land for free or pay a little fee.Hmmm..., with this in place, you can see how determined Microsoft is to pull everyone into its Windows 10 ecosystem. You know, when Windows 10 Technical Preview was first announced along with how it's going to operate (Windows as a Service), I thought Microsoft is finally kicking Windows Pirates out of business, but it seems Microsoft is giving them one more chance to own a genuine Windows, maybe Windows 10  is going to be "un-piratable" (let's just hope MS gets it right).Hahaha!...., before I forget,

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

This article is for hackers who want to develop a c++ program on Kali Linux without having to install any additional software. While some would tell you that you need to install an additional software or a compiler in order to develop a simple program in c++ on Kali Linux, I am going to show you how to develop a c++ program on this distro right out of box. Your Linux Kali comes pre-installed with a c++ compiler called g++ so we are going to write a C++ hello word code and compile it with this compiler. Before we get started, first open up terminal and run to verify if this compiler is installed on you machine: g++ -v if the compiler is pre-installed, you should get the version information of the compiler, otherwise, you should get an error. Now let's jump right in. In your terminal window, type in: nano MyCpp.cpp to create a c++ file and lunch it in nano editor for editing. When nano opens, type in the following c++ code and press Ctrl+x then y and then R

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. Usually, these companies sends ou