Monday, April 25, 2016

Why create functions?

Going back to the basics today. I personally worked on several programs where the authors have just wrapped all the code in a single function and it has worked! An example, suppose you are working on an ASP.NET website - just stuff everything into the page life cycle Page Load function!

But I am a firm believer in creating functions to segregate your code since it has the following advantages:


  1. Problem solving: They allow us to conceive of our program as a bunch of sub-steps. (Each sub-step can be its own function. When any program seems too hard, just break the overall program into sub-steps!). [1]
  2. Code reuse: They allow us to reuse code instead of rewriting it. [1] This will also reduce the length of your code file. 
  3. Garbage collection: Functions allow us to keep our variable namespace clean (local variables only "live" as long as the function does). In other words, function_1 can use a variable called i, and function_2 can also use a variable called i and there is no confusion. Each variable i only exists when the computer is executing the given function. [1]
  4. Unit testing: Functions allow us to test small parts of our program in isolation from the rest. [1] This is fantastic for Unit Testing. So is dependency inversion. 
  5. Maintainability: It makes it easy to maintain the code since the length of your file is small. And further, if you make changes to the function the new code is automatically propagated to every where the function is called. On the other hand, if you have not created functions, you would have to change the code everywhere either by searching for it or waiting till your program crashes :).

Image source: Wikipedia


When should you create functions?

  1.  Duplicated code: When the same code repeats in several places. This was and still is a thumb rule. 
  2. Long methods: All other things being equal, a shorter method is easier to read, easier to understand, and easier to troubleshoot. Refactor long methods into smaller methods if you can. [2]
  3. Single responsibility principle: If a method has more than one responsibility it can lead to a violation of SRP. [3]
References: 
1. http://www.cs.utah.edu/~germain/PPS/Topics/functions.html
2. http://blog.codinghorror.com/code-smells/
3. http://programmers.stackexchange.com/questions/275646/is-the-single-responsibility-principle-applicable-to-functions


Sunday, April 17, 2016

Two cases: Show a page only if a condition is met AND Restrict user to a certain page

1. There are multiple ways to this, but possibly the best way in ASP.NET is to have a session variable set.

Then check for value of the session variable in page_init or page_load and direct the user accordingly.




2. Another situation is when the user has to be directed to a certain page in the website. If x occurs, redirect user to a certain page. And then, don't let him out of that page if he doesn't make a selection. Java script has a solution to this:

 window.onbeforeunload = function UnLoadWindow() {
         return 'Please completed the selected.'
     };

But this still gives the user the option to leave:


But Session variable solves this too. And check for this variable in every other page of your website or on the master pages. If the variable exists and has the appropriate value, redirect user back to the page. For e.g. suppose we want to take the user to a completed page once his application for a loan is completed. Now we don't want the user to navigate to any other page in the website, until the officer process his documents. We can set a session variable:

Session["complete"]= True;

And check in all the other pages:

if(Session["complete"].ToString()=="True")
        Response.Redirect("complete.aspx");


Sunday, April 3, 2016

My binary world

I am a coder and so I view this world in binary....

Zeros and Ones. Good and Bad. Heroes and Villains. Black and White.

So how the heck am I supposed to create a green button? :)



Why does a page take a lot of time to load?

Suppose your users complain that the website takes a long time to load once they login. It's problem solving question. Let's break it down into the areas where the problem might be:

1. Server scripts

2. Database

3. Browser

4. Client scripts

5. Actual server

Lets address each area:

1. Server scripts
Server side code has some faults. What if there is an long loop of inefficient code?

2. Database
There might be a lot of concurrent users on the application and which leads to a lot of database reads and writes. There are several ways to find out how much time that it takes to execute a particular query. 

3. Browser.
Our website wasn't loading on IE either since we had not published the P3P policy.

4. Client scripts.
Errors in JavaScript.

5. Server issues.
Less RAM? Too many programs or users running concurrently may be one of the reason why the server takes a long time to respond. Why don't you check if the website loads properly at time of when there are few users accessing the system?

By the way, I came across this great post at Kissmetrics.