Monthly Archives: May 2015

What is debugging, How to debug… A beginners guide

We get exceptions mostly, it is best to always test your applications before submitting it to markets or other platforms for users. Debugging is one of those options to test your applications. As the name suggests, Debugging means “to debug” (What?) the application. It is a process in which framework problems and other logical errors are removed from the errors.

As a software developer you might know that there are usually three type of errors.

  1. Syntax error
  2. Run-time error
  3. Logical error

Syntax error and run-time error are two types of errors that can be shown to the developer by the framework or compiler (first one). So developer just have to check what went wrong and how to do to fix it. However the third type of error cannot be checked by a compiler. Logic is something you make up. You have to write correct algorithm to make it work. If there is some kind of problem, you have to check your algorithm once again to make sure that it is correct algorithm. Logical errors, do not generate any error while compiling the source code. They also don’t notify the user about wrong results. A common example would be,

int Remainder(int a, int b) {
    // provided b is not zero
    return a / b;
}

Indeed developer wanted to get the remainder, but used division operator. Source code would compile and would give results… Wrong results.

Debugging helps us in finding such errors and solving them to get the correct answer and solution. This option is provided in every IDE (Visual Studio, Android Studio, Eclipse… Others that you have tried) for developers to debug their applications.

How to debug

Now you might also want to know how to debug your applications. Well, that is pretty simple. I have also mentioned above that (almost) all of the  IDEs provide tools to debug your application. I have used Eclipse, Visual Studio, Android Studio… They have all provided me with (similar) tools to debug the applications and see how things are done.

The thing, “How things are done” is helpful when solving the logical error. Point to note here is that debugging is not only helpful in solving the logical errors. It is also sometimes helpful while removing the run-time errors such as the most famous “NullReferenceException” (For more on NullReferenceException you can read this another post of mine to understand What it is?… Why it is generated!… And how to solve it?). Syntax errors need to be resolved before the object-code can even be generated. So that leaves the debugging method applicable to run-time and logical errors only.

The steps to debugging processes generally depend on the type of error you are trying to debug. So let me clarify the two types of debugging process. However, the actual process is similar. You start and end up in the same way it is just your internal process that is different for both, 1. Run-time error, 2. Logical error.

Process

In every IDE, option to set up some Breakpoints is included. Breakpoints are a few steps in your code where IDE notifies you when the code execution reaches that place (or point). You are then allowed to check the application’s memory consumption, variables (including their “at that time” values). At this stage you can see what is the state of application and how it should behave. You can also check what type of variables (with value) are sent to this block and how are they being used or manipulated. This would further guide you in fixing the problem.

Run-time error debugging

For example, if you stumbled upon DivideByZeroException. Then you can add a Breakpoint to your function and execute step by step, one by one each statement and look into how does your code get a variable with zero (or is the user passing the value to be zero) and so on. This type is the Run-time error debugging. DivideByZeroExceptions are generated at run-time when the code executes. That is why you usually would face this error while running the application only.

This type of debugging is somewhat easy, because it doesn’t require a lot of searching for error. The framework that you are using would throw an exception and you (if having more than beginner level experience) would easily know that the program is telling you to fix this problem at this location. And then you can easily add the patch to fix the problem.

Logical error debugging

Debugging a logical error is somewhat tough task because they cannot be found easily. You have to run through each and every statement and check for Where actually do things mess up? Sometimes it can take 5 – 10 minutes, sometimes it can take an hour. Depending on the complexity of the logic or algorithm being designed.

Let us take an example of an “Age calculating” algorithm. Take a look at the following C# code.

// Assume dateOfBirth is coming from user having valid dateTime expression
var age = (DateTime.Now - dateOfBirth).Days / 365;

age variable would hold the correct data only if the logic is applied correctly. We know that our logic is correct. Which is not! We know 365 are the number of days in a year… But we don’t know is that we round up the overhead number of hours in the Leap year after 4 years. So if above code is run, it is not guaranteed to give the correct answer to every input. It might have at least >10% of errors chances. If processed under debugging tools, we would find that using 365.25 as divisor is the correct way to find the age of a user using DateTime object. So now the following code would run correct and would not have as much error ratio as above code had (Still might have error chances!)

var age = (DateTime.Now - dateOfBirth).Days / 365.25;

Now when you would use it, it would display correct for the provided input.

Note: The above example has a physical significance, I wrote the age calculation algorithm which gave me my age to be 20 years (while I was 19.5 or something years old).

Points of interest

Now let us wind things up. A few of things mentioned in this post are,

  1. Debugging is a process in which bugs (mainly logical or run-time) are removed from the application.
  2. (Almost) every IDE supports debugging tools.
    1. Breakpoints.
    2. Debugger (in which application runs… vshost.exe can be an example for Visual Studio geeks).
    3. Profiling tools, memory management, variable information and other tools required to alter the state of application and to check what is going on under the hood.
  3. Run-time errors are easily understandable and solvable because the underlying framework tells the developer what went wrong. So (if developer has some understanding of the framework then) he can remove the chances of problem occurring again.
  4. Logical errors take more time. Because they depend on the complexity of the logic being solved. They can be removed only if the algorithm is fully understood and how should it be written in the language.
    See the examples above for more.

That’s all for now folk! 🙂 I hope it helped some of you…

Advertisements

Novice to MVC? Read this…

This post is meant for beginners in MVC pattern (do not confuse yourself with ASP.NET MVC; if you want to learn ASP.NET MVC then please read this post of mine). If you’re novice to this framework then read this post of mine. I will cover most of the parts of MVC framework itself and how can you implement this framework in your applications.

Understanding the Model-View-Controller

MVC stands for Model-View-Controller. If you have developed a few applications in OOP model then you will understand this easily. Otherwise, you might have some tough time understand this MVC pattern. But you will synchronize as we continue.

First of all, you should get rid of any other programming model that or framework that is inside your head. Also, (as mentioned earlier) do not confuse MVC with ASP.NET MVC. This pattern is very common among other patterns of software architecture. MVC is just the term used to develop the software in a pattern such that it is divided into three sub-categories.

  1. Model — The data source for your application.
  2. View — Your application’s views, putting it simply graphical user interface.
  3. Controller — Controller the is actual background logic for your application.

Above are the three categories in which code is distributed efficiently. How application programming is made simpler, I will explain in further sections. Let us first talk about the concepts of MVC, then I will walk through implementation of MVC in different programming languages and frameworks.

Diving into MVC

Let us first consider MVC as our subject of talk. How can we use it and what it actually is. MVC is just the framework, architecture or a pattern, what so ever you want to call it. It is just the model for your software development process. Which is targeted to maintain your source code and data sources. Also, this framework is targeted to reduce ambiguity and complexity found in enterprise software. Usually when an application reaches enterprise level of complexity it is a lot harder to debug it.

MVC-Process.svg

Now let us dissect the MVC into three sections and study it thoroughly.

1. Model

Model part of this architecture puts the focus on the data of the application. You data might come through different methods,

  1. Database
  2. Reports
  3. Data sources such as files; JSON is a major example
  4. User input

These compose the model section for your application. Model is responsible for updating the user interfaces and indicating a trigger to an event (if any) in the application to demonstrate that the data has now changed. Usually these are just fancy names given to a simple model of the data. In most of the programming languages it can be a simple class, with a few members (trailing the properties or the attributes in database table for the object) and a few functions to store the data in the data source and extract it. This must be kept secure and away from user interactions. Keeping it separate would help in minimizing unauthorized access attempts.

Note: The function part can also be implemented inside the controller to save the data or extract the data. 

2. View

View is the user-interface part of the software application. All of the interface design such as buttons, input fields etc. are to be added to this category. In most of the applications (such as web applications) HTML pages are the View where as in other applications and frameworks other methods are used to create an interface such as XAML files in WPF applications.

The main purpose of having a view is to get the data from model and represent it to the user. All of the styles and other UI and UX techniques must be applied here. Views are usually called by the the controllers and returned to the users as a response after being filled up by the data from a model.

3. Controller

Coming to the most important part of this framework. It is the actual software logic running underground. It is mostly composed of, functions that your application runs, other underlying logical code for the users and such other code that has to run throughout the initial stage to the very last line when the application ends.

In web applications, a controller is responsible for handling the requests coming from a user and then returns the response to the user after merging the data from the model in to the required view. Although the process is similar to getting request and returning the response. But under the hood process is something like,

  1. Request is made.
  2. Controller handles the request.
    Url is read and then appropriate functions are triggered.
  3. Inside those functions, model is asked for data and data is filled in to the view.
  4. View is then returned as a response.

The overall framework is something like this following image.

The user must be allowed to interact with Model himself, instead a Controller must be used to connect to the Model to get the data for the user's View that would be shown to him.

The user must be allowed to interact with Model himself, instead a Controller must be used to connect to the Model to get the data for the user’s View that would be shown to him.

As I have mentioned earlier, the user must not be allowed to interact with the model. User must be provided all of the required tools and buttons on the view. View can interact with the controller to download data (if required). This is the MVC pattern of software development. Now in the next section I will give you other helpful resources that you might find helpful.

Other material…

As I had already mentioned that I would provide other tools and materials for developers in a particular framework or language.

Any framework or type

This section is meant for everyone. If you’re developing an application and want to implement MVC pattern then read this section and you will understand how to develop this pattern in your own application development process.

Default language used is C#, you can use your own particular language also. C, C++ and Java are quite similar to the language.

Creating a Model

Creating the model is a very easy task. It is just the wrapper for your data sources. Which can be converted to an object and then interacted with in the application. Think of it as a simple object.

class Model {
   public string Name { get; set; }
   public int Age { get; set; }
   public string Email { get; set; }
   public string Message { get; set; }

   // Functions
   public static List<Model> GetData() { /* code */ }
   public static void SaveData(List<Model> data) { /* code */ }
}

The above code is the model for your own application. It has a few attributes (from the database) and 2 functions. One to retrieve the data and other to store the data.

You can now connect this with database, file or other resource in the functions and fill up your data using the data sources that you have right now.

Creating a View

This is simple (another) task. It contains building up the user-interface. If you’re using web application, then you would create the HTML web pages and add the required elements in the web page. Same process has to be applied to the software applications, where you can design the user-interface for your application and show it using (back-end) code.

Creating a Controller

Now coming to the necessary part. Controller is the entire code that runs the logic for your application, its interaction with events and underlying operating system. What happens when your application is to be started, what should be done when your application has to terminate and what happens in between them. All of this code is Controller.

You can create classes which handle the start up events, and also the classes which handle other interaction event with your View. Controller also has the code to trigger the model; such as the code to trigger either one of the method in our model (See above the “Creating a Model” part).

MVC in web applications

If you are going to develop a new web application, you can develop your application in a very similar manner discussed above. Write your code in a manner that it categorizes itself into three categories.

  1. Model
  2. View
  3. Controller

It is helpful once your application reaches an enterprise level of complexity. So that at that time you won’t have to scratch your head twice to think like, “What was I thinking?”

If (however) you are going to use ASP.NET, then there is already an MVC framework built for you with all of the underlying code done and an IDE ready to serve your needs. For more on that for learning purposes, please read this another thread of mine. That article of mine has all of the required information that a beginner requires to kick start his ASP.NET MVC web application development.

Offline applications

If you are using Java… Then I have another major thread for the same purpose. You can read it to learn how to implement the same pattern in your Java application development. You can also download and try the samples included in it.

Other software languages and framework can also implement the MVC pattern, you just have to categorize the code of your software application.

Points of interest

That said, now I might give you other helpful points for secure applications and frameworks. Following might be helpful for you,

  1. MVC pattern helps you in sorting your code and categorizing same types of code under a single lable M-V-or-C.
  2. You should never allow your user to interact with model. Model contains the data or the definition for your data. If potential user is able to interact with your application’s model he might also get through the security checks. That is why, always use controller to validate the requests and then access the data from model.
  3. I have used databases, JSON files and all kind of other data sources available. So your model is capable of interacting with any kind of data source present (provided you have enough code to make it work).
  4. You can also use Ajax to run Controller partially on the views. If you are interested in learning how to use Ajax in MVC pattern then please read this another article of mine.
  5. MVC pattern is a software designing and developing architecture. It does not require you to be in web or offline context.

I hope this post must have helped you out. 🙂

Downsides of learning {programming-language} in {x} days!

This might be my first post about such topics… Which might contradict other (self-claiming) authors who say that they can teach you a programming language in a number of days. For example, “Learn ASP.NET in 7 days”, “Learn HTML5 in a matter of 3 days”. Let me point out a few of the downsides of this type of teaching and learning for new developers, who might fall for this approach.

Who are the victims?

The most victims to this type of approach are the young developers… Inexperienced, rushing to learn new things, express-to-impress type of teens and those with a deadline of them self fall to this type of approach very often. Sometimes those who cram the rules, types, structures of the language do so.

Remembering never helps you out! You have to understand how things work.

Remembering never helps you out! You have to understand how things work.

Most usually, the teens who are eager to learn something in a very short number of time are victims to this approach. 7 days? 14 days? Seriously? What is it like… A pond’s challenge! Programming is something that cannot be fed into minds of other people; you cannot move a clock in simple harmonic motion and expect the human to become a programmer once you’re done hypnotizing and brain washing him. It cannot be taught the way we learnt it. It must be taught by lowering our standards to the standard that our student can understand us. Our students come to us to learn… We should educate them, not force them to remember what we said.

Who claims such thing?

I have no personal hatred towards those who claim such. In fact people who are extraordinary in learning and experimenting claim such things. They learn things in a very fast manner. Authors who write a title to their publication in a way, “Learn {subject} in {x} days!” are them self very quick learners. That is why they expect others to learn the same subject in a very fast manner.

It won’t be false to claim that they actually are very genius of their era. They have a very resilient grip of their subject and field. But not all fingers are of same size. Are they?

Discussing the downsides of this approach

Now let us discuss, why new programmers should not learn programming or any architecture or a framework using this approach. I would state a few points of mine, you can have your own feelings and opinions and might contradict to mine. But… Be logical and learn stuff slowly and by understanding what they are to you!

Not at all a true statement

First downside that I feel about such content is that they are not at all true. How can someone claim to teach you something in a matter of deadline. I teach programming to a few, some of them learn the concept very quickly. Some take time… Some take a lot of time. For those who take a lot of time, I try to make sure that they at least learn and understand the basic concept and tend to do something with it. I do not expect them to cram to reach the “x” days limit.

More appropriately such topics for publications are a term for publicity stunt. Because they are more straight-forward, targeted by those who want to make more in less time, author is challenging himself to be able to teach you more in less days.

Different psyche levels

Humans do not have similar psyche level… And you just cannot expect everyone to be similar when it comes to ability to learn and understand something. It would be responsibility of the instructor to teach his/her student in a way that they can surely learn something and not just waste their own time. I have taught people of almost every psyche level… From geniuses to average students. Those who were genius asked such questions which led me to go deep inside my mind to extract a scrap of information which I had locked in my limbo for a decade (not really!). Those who were average students had to pay some more time to learn it, they also tried not to understand the concept as to apply it. But just to learn it… To learn it!

I had never been a great instructor. So I have tried giving them a lesson in a way a friend would. Publications claiming to 5, 7 or 30 days time period don’t claim that they would teach programming to those with average mind set. They are of the same period and good use to someone who has the 100% same mental ability as the author had when he was learning the same framework or language.

A book, in any way cannot claim that the readers are expected to benefit from the content. But, claiming something that is not expected or guaranteed should not be used. When an author claims something, he must be able to prove it.

Only good side of this…

The only good side of this approach is that the subject is catchy! 😉

Personal views

There are many other points… But, I would not want to criticize such content any more. Instead I would share my views to beginners how can they benefit from less in more time.

Practice makes you better!

First of all, I should say that you need to practice what you learn. Practice is something that doesn’t make the applicable task easier, it just makes you better!

You should never think things are getting easier. It is you who have evolved.

You should never think things are getting easier. It is you who have evolved.

What Google might respond to your search as?

Deliberate_Practice

I learnt programming in a matter of 2 months. I just learnt C# at that time… Not software programming! There was nothing that I knew about creating an efficient software, writing a good API, creating a web application and so on. The more your practice the better you get. More errors you get, more you will understand. Sometimes when I go back in time 3 years ago and have a look at my questions, problems that I faced. I laugh. That doesn’t mean things are easy. That means practice has just given me enough understanding of these frameworks that now I can slice and dice them in any way that I want to. It also now has an impact on my social life.

Now, after learning and practicing. I speak less but qualitative. The quality of my speech is great now. I do not speak gibberish (or that I feel like).

Now, after learning and practicing. I speak less but qualitative. The quality of my speech is great now. I do not speak gibberish (or that I feel like).

There is no limit to study

I can build good applications in a matter of day or two. But that doesn’t mean I should give up learning more and more about such particular platform or subject. I have good grip over .NET framework, but I believe I should learn more and more.

Every new developer must only focus on learning part. I remember a lot of good threads where I have participated, ended up in a same conclusion. That you cannot learn anything in a matter of week, fortnight, month or even a year. You need at least to consume 5-10 years to learn something. Yes, if you’re going to cram through the concept for a term test then good, do it. It might even take 3 days for that.

Focus on something… Put smaller time periods in your learning

But to learn something fully, you need to focus on it in a very better way. Give more and more time to it. Let it settle down. Give your education a short time of more like 30 minutes, 45 minutes regularly. Try something, learn something. You would stumble upon more like a million errors before you can think yourself as a pro beginner! Before you can even enter intermediate level, you will fall into the errors like a million, or billion times.

Do not rush!

There is no need to rush. You can learn something in a very easy, smooth and friendly way. For new developers, it is a very good approach to read articles of other developers, read their code. Reading good content, good articles, good code helps you out to understand how they did. Remember they might be better than you (I don’t mean to tease anyone here, sorry if you take my words seriously), so if they did something in one day and you took 3 days. Do not hesitate! It is OK!

Finally. It is you, who knows where you lag. Best learning environment is when there is an instructor available for you. Books, articles, blogs do not teach you in a way they can. If you are a beginner never rely on them. If books were everything there would have been no requirement for a school, institution or university. There would have been great great libraries every where. You should ask your instructor what to do and how to do. Your instructor should help you out… He should be able to know where you lag behind others. He should polish you in a way that you can really get into something.