Saturday, July 23, 2011

How to Improve your Skills as a Programmer

Steps
  1. Gather complete requirements.
    1. Take the time to write down what the end product needs to achieve. Clarity of thought at this stage will save a lot of time down the line.
  2. Write an implementation plan (or model).
    • For something small and self-contained, this might just be a basic flowchart or an equation.
    • For larger projects, it helps to break it into modules and consider what job each module must do, how data gets passed between modules, and within each module how it will function.
    • Although it is fun to dive straight into code, it is equally tedious to spend hours debugging. By taking the time to design the structure on paper, you will drastically reduce your debugging time (and you may also spot more efficient ways of doing things even before you write the first line of code).
  3. Add Comments to your code.
    Whenever you feel your code needs some explanation, drop some comments in. Each function should be preceded by 1-2 lines describing the arguments and what it returns. (Comments should tell you why more often than what. Remember to update the comments when you update your code!)
  4. Use naming conventions for variables.
    It will help you keep track of what type the variable is and also what it's purpose is. Although this means more typing than x = a + b * c, it will make your code easier to debug and maintain. One popular convention is Hungarian notation where the variable name is prefixed with its type. e.g. for integer variables, intRowCounter; strings: strUserName. It doesn't matter what your naming convention is, but be sure that it is consistent and that your variable names are descriptive. (See Warnings below)
  5. Organize your code.
    Use visual structure to indicate code structure. i.e. indent a code block that sits within a conditional (if,else,...) or a loop (for,while,...) Also try putting spaces between a variable name and an operator such as addition, subtraction, multiplication, division, and even the equal sign (myVariable = 2 + 2). As well as making the code more visually elegant, it makes it much easier to see the program flow at a glance. (See tips on indentation below)
  6. Test.
    Start by testing it with inputs that you would typically expect. Then try inputs that are possible but less common. This will flush out any hidden bugs. There is an art to testing and you will gradually build up your skills with practice.
    Write your tests to always include the following:
    • Extremes: zero and max for positive values, empty string, null for every parameter.
    • Meaningless values, Gibberish. Even if you don't think someone with half a brain might input that, test your software against it.
    • Wrong values. Zero in a parameter that will be used in a division, negative when positive is expected or a square root will be calculated. Something that is not a number when the input type is a string, and it will be parsed for numeric value.
  7. Practice. Practice. Practice.
  8. Be prepared for change.
    In a realistic working environment, requirements change. However, the clearer you are at the start about the requirements and the clearer your implementation plan, the less likely those changes will be down to misunderstanding or "Ah, I hadn't thought of that" scenarios.
    • You can take an active role in improving clarity of thinking by presenting your requirements document or your implementation plans before coding to ensure that what you are planning to create is actually what's been asked for.
    • Structure the project as a series of milestones with a demo for each block. Approach the programming one milestone at a time - the less you need to think about at any moment, the more likely you will think clearly.
  9. Start simple and work towards complexity.
    When programming something complex, it helps to get the simpler building blocks in place and working properly first.
    For example, let's say you want to create an evolving shape on screen that follows the mouse and where the degree of shape change depends on mouse speed.
    • Start by displaying a square and get it to follow the mouse, i.e. solve movement tracking on its own.
    • Then make the size of the square relate to mouse speed, i.e. solve speed-to-evolution tracking on its own.
    • Finally create the actual shapes you want to work with and put the three components together.
    • This approach naturally lends itself to modular code writing, where each component is in its own self-contained block. This is very useful for code reuse (e.g. you want to just use the mouse tracking in a new project), and makes for easier debugging and maintenance.                    
                                                      

Configuring IIS6 to host your Intranet, Extranet or Web Site.



1. Installing IIS6 on Windows Server 2003:
If using the new style Start menu: Click on “Start”, “Control Panel”, “Add or Remove Programs” and select the “Add/Remove Windows Components” tab on the left-hand side.


If using the "Classic" style Start menu: Click on “Start”, “Settings”, “Control Panel”, “Add or Remove Programs” and select the “Add/Remove Windows Components” tab on the left-hand side.


In the “Windows Components Wizard”, highlight the “Application Server” and press the “Details” button. The screen below will be displayed (Fig 1).
<><> <><> <><>
Figure 1 - The Application Server Screen


First, check “Application Server Console” and “ASP.NET”, then highlight the “Internet Information Services (IIS)” option and press “Details” (as shown in Fig 1 above).
<><> <><> <>     <> <><>
Figure 2 - The Internet Information Services (IIS) Screen


On the next screen (Fig 2 above) we highlight “World Wide Web Services” and press the “Details” button to bring us onto the last screen.
<><><>    <> <><> <><>
Figure 3 - The World Wide Web Service Screen
 make extensive use of ASP files in my Intranet, so I need to select the “Active Server Pages” option (shown in Fig 3 above). At this point, you may wish to check through the other options available in the list and select them if they apply to your site. Don't worry if you are unsure, you can always change these options later.

Click “OK” to close each window and “Next” to install the newly-selected components. You will be asked to insert your Windows Server 2003 disk. Click “Finish” once the installation is complete.


You have now installed Internet Information Services 6.


<><> <><> <><>
Figure 4 - Where to find the IIS Manager
2. Where to find the IIS Manager:
If you are using the new style Start menu, you can reach the Internet Information Services console by clicking “Start”, “Administrative Tools” and selecting “Internet Information Services (IIS) Manager” from the list in figure 4 above.
If you are using the “Classic” style Start Menu, you can reach the console by clicking “Start”, “Programs”, “Administrative Tools” and select “Internet Information Services (IIS) Manager”
from the list in figure 4 above.
<><><>      <> <><> <><>
Figure 5 - Internet Information Services (IIS) Manager
3. Internet Information Services (IIS) Manager:
IIS5 users will notice two new additions to the IIS Manager. The First is the “Web Service Extensions” screen which provides control over which file extensions should be allowed or blocked. The second is the “Application Pools” screen which allows applications to run in isolated memory “Pools” meaning that if one application has problems, others are not affected.
<><><>    <> <><> <><>
Figure 6 - Creating a New Web Site in IIS 6
4. Creating a New Site in IIS 6:
The first task is to disable the “Default Web Site”. To do this, select the “Web Sites” item in the left-hand pane then right-click on the “Default Web Site” item in the right-hand pane and select “Stop” from the list. The Default Web Site is now stopped.
Next, we need to create a new site to host our web content. To do this, right-click on “Web Sites” in the left-hand pane and select “New” and “Web Site…” as shown above in figure 6.
When the “Welcome to the Web Site creation Wizard” screen appears, click next.
<><><>     <> <><> <><>
Figure 7 - Name your Site
5. Name your Site:
Simply enter in a name for your site in the box and press "Next" as shown in Figure 7 above. This name is for reference only and should help you locate your site in IIS Manager.
<><> <><> <><>
Figure 8 - Changing the IP Address

6. Method of Access (using an IP address - Recommended):
This method simply uses the IP address (or one of the IP addresses if more than one are configured) of the web site to allow users to reach the site.
To use this method, all you need to do is select an/the available IP number from the list and leave the port settings on 80.
To reach the site, visitors will have to type the IP address http://192.168.200.4 into their web browser. They can also type the machine name in eg. http://suk-sys4
<><> <><> <><>
Figure 9 - Changing the Port number
7. Method of Access (using Port numbers):
In this case, no additional IP numbers have been assigned to the Server, so all we have to work with is 192.168.200.4. Unfortunately, this is already being used by another site so we are going to host this particular Web Service from Port 81 instead of 80. To view the site, users will have to enter http://192.168.200.4:81 (the colon denotes the port number).
<><> <><> <><>
Figure 10 - Changing the Host Header

8. Method of Access (using Headers):
Headers allow multiple sites to run from one IP address and through one port (80). The advantage of using "Host Headers" is that a large number of sites can be hosted through just 1 external IP address.
The request is interpreted by the server and the visitor is directed to the correct site based upon the URL they entered and NOT just the IP address it was translated to by the DNS server.
This means that you need to add the URL (excluding http://) in the "Host Header" box as shown above. In the case of my Intranet, I am entering "intranet.simongibson.com" so visitors will type http://intranet.simongibson.com to access the site.
However, this will mean that a DNS entry must exist for "intranet.simongibson.com" to be translated to the correct IP address and route to our web server. This will therefore mean that you must also make this change to your DNS server if you plan to use this method.
Before you begin working with Host Headers it is recommended that you correctly configure a DNS server. To go to the DNS Configuration tutorial on this site, click here
<><> <><> <><>
Figure 11 - The Web Site Home Directory
9. Defining your site's home directory and who can access it:
 Once you have defined which method of access you wish to use, you need to point IIS at your default home directory. This is pretty self-explanatory and in this case is c:\Inetpub\intranet_live which is the sub-directory 'intranet_live' located in the 'Inetpub' directory with the C: drive on the web server (SUK-SYS4).
Notice that I have unchecked 'Allow anonymous access to this Web Site'. This will ensure that any visitor will have to enter their username, password and Domain to access the site from outside the building (ideal for an Extranet).
If you do not plan to make your Intranet available through the Internet then it is probably best to leave this box checked.
<><> <><> <><>
Figure 12 - Web Site Access Permissions
10. Defining Access Permissions:
The options shown in Fig.6 above are the default settings for IIS. These are fine for our Intranet. You can set up seperate access permissions for sub-directories later, such as a cgi-bin. See 'Configuring Active Perl' on the 'Intranet/Extranet' main page for more information.
<><> <><> <><>
Figure 13 - The completed IIS6 Manager with new web site
11. The completed configuration of IIS:
If you have followed these instructions correctly, your IIS dialogue box should look something like Fig 13 (above). You can add as many extra web sites as you like, but bear in mind the access method you plan to use (check section 6, 7and 8).

Building Your First Web Application Project

The below tutorial walks-through how to create, build and run your first web app using C# and the ASP.NET Web Application Project support in VS 2005.

Creating a New Project

Select File->New Project within the Visual Studio 2005 IDE. This will bring up the New Project dialog. Click on the “Visual C#” node in the tree-view on the left hand side of the dialog box and choose the "ASP.NET Web Application" icon:

Choose where you want the project to be created on disk (note that there is no longer a requirement for web projects to be created underneath the inetpub\wwwroot directory -- so you can store the project anywhere on your filesystem). Then name it and hit ok.
Visual Studio will then create and open a new web project within the solution explorer. By default it will have a single page (Default.aspx), an AssemblyInfo.cs file, as well as a web.config file. All project file-meta-data is stored within a MSBuild based project file.

Opening and Editing the Page

Double click on the Default.aspx page in the solution explorer to open and edit the page. You can do this using either the HTML source editor or the design-view. Add a "Hello world" header to the page, along with a calendar server control and a label control (we'll use these in a later tutorial):

Build and Run the Project

Hit F5 to build and run the project in debug mode. By default, ASP.NET Web Application projects are configured to use the built-in VS web-server (aka Cassini) when run. The default project templates will run on a random port as a root site (for example: http://localhost:12345/):

You can end the debug session by closing the browser window, or by choosing the Debug->Stop Debugging (Shift-F5) menu item.

Looking under the covers

When you compile/build ASP.NET Web Application projects, all code-behind code, embedded resources, and standalone class files are compiled into a single assembly that is built in the \bin sub-directory underneath the project root (note: you can optionally change the location if you want to - for example, to build it into a parent application directory).
If you choose the "Show All Files" button in the solution explorer, you can see what the result of our compilation output looks like:

This works exactly the same as with Visual Studio 2003 ASP.NET Web Application Projects

Customizing Project Properties

ASP.NET Web Application Projects share the same configuration settings and behaviors as standard VS 2005 class library projects. You access these configuration settings by right-clicking on the project node within the Solution Explorer in VS 2005 and selecting the "Properties" context-menu item. This will then bring up the project properties configuration editor. You can use this to change the name of the generated assembly, the build compilation settings of the project, its references, its resource string values, code-signing settings, etc:

ASP.NET Web Application Projects also add a new tab called "Web" to the project properties list. Developers use this tab to configure how a web project is run and debugged. By default, ASP.NET Web Application Projects are configured to launch and run using the built-in VS Web Server (aka Cassini) on a random HTTP port on the machine.
This port number can be changed if this port is already in use, or if you want to specifically test and run using a different number:

Alternatively, Visual Studio can connect and debug IIS when running the web application. To use IIS instead, select the "Use IIS Web Server" option and enter the url of the application to launch, connect-to, and use when F5 or Control-F5 is selected:

Then configure the url to this application in the above property page for the web project. When you hit F5 in the project, Visual Studio will then launch a browser to that web application and automatically attach a debugger to the web-server process to enable you to debug it.
Note that ASP.NET Web Application Projects can also create the IIS vroot and configure the application for you. To do this click the "Create Virtual Directory" button.