Hey! I am back – Your Koffee Cup. Let me tell you about my current owner. He is a boy of around 18-19. He has a great zeal to learn things. Every morning, he gets a cup of coffee, in other words, fills me with coffee, and opens his laptop and then “Disappears”. Yeah! I mean it. He just vanishes leaving me in front of his Personal Computer. Glancing at it for almost a year now, every day different topics, I learned one very important thing – Programming!
I thought, why don’t I discuss it with you guys, Here we go –
This is my first article on this series, which I call “How to Code”. Hope you start here with this Introduction and learn to code – In fact learn to code anything with ease.
So, Let us start our discussion with the importance of coding or ‘Writing a code’ in a Project, what are the several steps or issues we need to be concerned of before we start to code. Let me make my definition of word Project precise here – “A Project is performing a task or group of Tasks, by an individual or a group in order to attain an end result that benefits its stake holders directly or indirectly” [1].
Let us consider a project as an example – Building or developing an application, Say Mobile Application. We can divide the steps involved in building the application roughly into the following steps.
Developing, Idea for the Application
Deciding, Basic and Additional Features to be included in the Application
Knowing, Platform and Architectural Concerns
Gathering, Other resources or Resource Knowledge Required.
Design, Application Interface [Visually]
Planning and Deciding, Interface and Interaction between various pages or features of Application and Mobile Operating System.
Developing, User Interface Components
Developing, Work Flow of Various other Components
Coding, Writing or turning the design into a working model.
Testing, the Application for any flaws or errors
Deploying, delivering the product to the end user or to another project[If it is a part of another large project]
[Say, this is project developed by a pair of students. So Time Consuming steps like “Requirement Gathering”, “Unit Testing”, “Planning the Tests and Developing Test Cases” and most importantly, “Documentation” are omitted from the list, which are not generally performed in Small Projects [2].]
This clearly shows that, coding is just one of several other major tasks of developing a project and clearly, when other steps are followed with discipline, coding comes out to be of very little or no effort[3]
Researches and Analysis show that a disciplined software development project involves at most 3% of its efforts devoted to coding or programming [4].
Let me support my point by quoting a real time example, generally faced by Programmers or coders!
Problem: Write a program to save the text files created by users in a folder named Local Folder.
Assumptions or Requirements:
1. Two or more files can have same name.
2. Users can save a file without a name.
3. User can create, read, edit and delete a file.
4. Users don’t see Local Folder directly, instead they see the list of text files on a Display UI [User Interface – Say, a mobile Display Screen, in case of mobile Application].
1. Two or more files can have same name.
2. Users can save a file without a name.
3. User can create, read, edit and delete a file.
4. Users don’t see Local Folder directly, instead they see the list of text files on a Display UI [User Interface – Say, a mobile Display Screen, in case of mobile Application].
Solution:
Before we jump into code the solution for this problem, lets us take some time analyzing the given Problem, possible corner cases and possible fixes for them and finally the best possible solution applicable to the current situation.
Let us take on one assumption and formulate a solution for them step by step:
v Since user should be able to save two or more files with same name, we need to append or concatenate [Mix] the name given by the user to file with a unique element that varies continuously and help us defining a unique name to our file. Once such option could be appending the name given by user with the current time taken to a scale of milliseconds. This clearly solves the problem of saving two or files with same name in same directory.
So, we can save a file with a name, something like this:
<date>_<hour>_<minute>_<seconds>_<millisecond>_<Name-Given-By-User>
v Second condition or assumption is that user can save a file without a name, so when a user doesn’t enter a name, we need to save or give a default name say, “Untitled”.
v Third Condition: File should be able to be accessed for Read, Write, Edit and delete. These can be simply achieved by the opening the file in the interface in their respective modes, that is, read mode, write mode, edit mode.
We also need a delete option, which erases the file and its contents. This can be done by opening the file at only one instance and run a proper code to erase the contents and then finally delete the file name from the list.
v Final Condition is that user should be able to see the files in a list-type Interface. This can be simply achieved by calling the list function or “List Box” type of feature in our programming language and recursively add all the files to the list and erase any name from the list as desired.
Done with what to do, now we will dig into “How to Do” more specifically! At each step, we will proceed further, assuming that we have found a solution!!
I. To create name, we need a function to retrieve current date and time.
II.Convert the Date and time retrieved by the use of function (I) into required format.
III.Append or Join the result of (II) and user-entered-name-of-the-file. If No Name is given by user, we already assumed that we will be naming the file as “Untitled”.
IV. We can call a function (may be recursively) to take notes from user and save it with the same file name derived as a result of (III).
V. We will need a function to open the file for read, write or edit as desired.
VI. Final task is displaying all the files to the user by a list kind of interface. For achieving this, we can call a list box function or alike function and add all the files to the list box and display it to the user. If user clicks on a file name, we need to then open the corresponding file, initially in read mode. We may need to switch modes as desired by calling the required function at right time.
Example: Let us check out an example, by use of all those above defined mechanisms and analysis to prove my point, how easy it is to code... The below code is in C#. It is fine, if you don’t know C# or don’t understand it. Just go through the Comments and the code below it to get a rough idea. If you feel difficult in any way, just skip it. We will learn all these things in a while.
I bet, by doing such thorough analysis of requirements, the code of any language seems familiar!!
-----------------------------------------------------------------------------------------------------------
//Getting Name for the File from User by use of titleTextBox and saving
//that name in title variable.
title = titleTextBox.Text;
//If title not defined by user, then as discussed we will use “Untitled” as //default name
if (title.Trim().Length == 0 || titleTextBox.Text == "Enter the Title Here")
{
title = "UnTitled";
}
//Constructing file Name
StringBuilder sb = new StringBuilder();
//Getting Current Date and Time and adding it to file name as discussed.
sb.Append(DateTime.Now.Year);
sb.Append("_");
//Format is used to format the string as desired by us.
sb.Append(String.Format("{0:00}", DateTime.Now.Month));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Day));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Hour));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Minute));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Second));
sb.Append("_");
title = title.Replace(" ", "-");
title = title.Replace(", ", "_");
sb.Append(title);
//To save the file as text file, we are adding the desired Extension.
sb.Append(".txt");
//try – catch block is something like, It tries to execute the code in try
//block first and if something goes wrong, then the code of catch block is
//executed.
//The code just calls required functions to create a new file with the name
//defined by us above and write user contents to it.
try
{
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (var filestream = appStorage.OpenFile(sb.ToString(),
System.IO.FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(filestream))
{
sw.Write(editTextBox.Text);
}
}
}
catch
{
MessageBox.Show("Internal Error");
}
navigateBack();
------------------------------------------------------------------------------------------------------------
Hope you have started to find insights on “How to Code”. The two major requisites to code are: Logic and A Programming Language. The Logic is generally based on requirement, environment etc. To apply an appropriate logic, we need to learn certain Data Structures and Algorithms!! Don’t Panic!!
We will do it, step by step. We will also learn a programming language and implement the logics learned!
We will learn easy things at the earliest, Mix up two or more of them, make them complex and proceed to learn “How to code”, easily. Very easily.
Happy Coding!
[1]: Definition developed in a view to Explain its Importance and use, should not be used for academic purposes although the definition is authentic and legitimate in its terms.
[2]: The Term is generally used to describe projects that requires comparatively less Human Power, Time and Other Resources. Generally Performed by Individuals.
[3]: Many poll results support this fact but should not exactly be taken by its literal meaning.
[4]: Disciplined Software Project(s) can be described as a software project that follows a development models like Water Fall Model, Spiral Model, Unified Model or a mixture of two or more of these standard development models.
0 comments:
Post a Comment