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

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. Usua...

How to execute eject command on an ATM Machine using CoreXfs lib on .NET

 In this tutorial, I'm going to demonstrate how to run an eject command on an ATM using CoreXfs, a library I developed.  You can download the library from  here  and follow up with tutorial. First we need to add reference to the downloaded dll. Screen One Once the reference to the dll has been added, you can proceed to coding the application. What I did in the screen below is very simple, I opened the device on the atm using it's logical name so that I can pass in commands. Screen Two So, that's it. thanks for dropping by. You can watch the video below to see how I coded it. Note that this code can only run on an ATM machine. NCR, WINCOR, DIEBOLD, HYOSUNG