Your Ad Here

Archive for November, 2008

Programming tips


These tips are generalized and not to any specific language that is the reason why I included code from many different languages. The techniques are almost the same though variations exist in the syntax of things.

By referring to variations, I mean the different methodologies of coding, e.g

So let the tips come.

When beginning a method, class, loop, function, condition statements (such as if, switch, etc), open and close it before starting to code.

e.g.

if(condition)

{ //place this and the closing of the curly bracket first, then code

//this reduces a lot of problems faced when writing complicated problems

/*don’t start typing here first*/

}

By this a typical error is often removed that is just because of the { , } that is often encountered by new programmers.

Code indentation

Indentation is normally done using the tab key typically 5 or 6 spaces are left.

e.g. the following is a C/C++ code.

Indentation helps when catching errors in larger programs such as databases. In a database related program where a database such as SQL, Oracle. Etc run in the backend the code is very complicated and there are lots of things we have to do. So why not indent the code so that errors can be caught easily. Not only are database programs long. Full calendar software in C exceeds 500 lines with a lot of nested loops, and conditional statements. So if the code is poorly indented, there is very little probability that the code will run properly without any abnormalities. Where as in indentation we can figure out the loop of conditional changes and make necessary changes to it to fix the problem.

Comments inclusion

One of the very crucial points, often the point where many of the new programmers have difficulty in. when we start making larger more sophisticated programs (i.e. that involve more lines of cod, we can’t figure out every time that this variable was for this purpose and this was for this purpose). This often is helpful when code has to be updated. That is miscellaneous bugs have to be removed.

Ways of commenting.

One is the single lined comment that is started with // here is a comment

Another is the multi lined comment which has syntax as follows

/*

Here is comment line 1

Here is comment line 2

Here is comment line 3

*/

Here is a sample java code with and without codes.

Include author(s) name, created on, version, and purpose of program in the beginning of your source code. Source code is the code that we wrote. This way user knows who made the code, no on can claim that he/she wrote the code without seeing the source code.

How to write it?

The following template can be used in most of the languages such as C, C++, Java, etc.

Name Variables, pointers, functions, classes properly. Do not name the variables randomly as this can create a lot of complexities in larger programs.

e.g.

we are naming both the variables for the use of date.

int d; // d will be hard to remember

int date; // but date can be easily remembered

What about the case for first name. Many things can be done;

This is not allowed in most of the languages

char first name [100];

Correctly we can name variables such as

char first_name[100];

char f_name[100];

Use of data structures

The purpose of data structures is to help us to use fewer computer resources and do a specific purpose without the hesitation of buying a new computing machine.

E.g. we have a record of cars for a whole city. We have 10 million cars in the city. We want to store them in a database, if our program is badly written it will take up hours, days or even weeks to execute. Where as if our program is written properly with the use of data structures we can minimize this to even 20 minutes. The process we are doing on the records can be sorting, searching, or any other thing. We are using the same computer for the manipulation of records in the system.

Always make an algorithm, flowchart, what is the point of getting into more trouble? Actually this is even a little time consuming when writing small programs.

Algorithms are not meant for any programming languages; instead they can be used to create the same program in different languages.

A sample Algorithm is as follows. This algorithm shows the process of registering a user.

Registering a User

This is done because when writing complex programs is not that easy, one can not just open up the compiler and start coding. When working with larger systems, we need to design it properly so that bugs are not there as well. We often use a model called ADITE model (Analysis Design Implementation Testing & Evaluation) to create full systems. The following is a flow chart




Posted on November 25th, 2008 by Mutedart  |  4 Comments »

Prolog introduction



There are many programming languages that exist in the market. There are many paradigms in programming. Prolog is a logical and declarative language. Program is PROgramming in LOGic. French clause ‘programmation en logique’. This means programming in logic. Prolog differs from other programming languages. It was developed by a French scientist.

Prolog is particularly used by programs that use non numeric objects. Programmer has to specify how to solve a problem. The general purpose of the language is associated with AI and computational linguistics. Program logic is expressed in terms of relations and execution is triggered by running queries over these relations.

What is a relation? It consists of facts and rules. Basically how things are related;

For example: I love programming. This is describing a relationship between me and programming.

Imran loves basketball à relation

?- Imran loves basketball /*The query question mark at beginning indicates a query*/

Yes /*this is the output we get from the query we ran*/

Ejaz_lost_his_room_keys /*this means Ejaz lost his room keys*/

?-Imran_lost_his_room_keys /*we are asking the computer to check if imran lost his room keys*/

No /*we get a no to be the answer*/

What if we have two people doing the same thing like reading a book?

?-reads(people,’book’). /*people The group reads a book*/

people=Imran; /*people include Ayesha and Imran*/

people=Ayesha;

Prolog is a very case sensitive language. What do we mean by saying case sensitive? An example will be HELLO WORLD, and hello world.

HELLO WORLD is in upper case as it is all capitalized where as hello world is in lower case as it is in small characters.

Prolog can be divided into two parts

· One is the program, the program has an extension of pl (i.e. *.pl) this is the file that contains all the facts and rules.

· The other part is the query which is actually running the program. We ask about the relationships we described in the program.

So where is it used? It is used in databases, Backend of websites, Artificial Intelligence, Expert systems, Programs using non numeric objects, computational linguistics.

Where can I get the compiler from? SWI-prolog is the compiler that I know there might be others in the market. But this one works fine. Link is http://www.swi-prolog.org/




Posted on November 19th, 2008 by Mutedart  |  2 Comments »