Code Convension for Coding (Dr. P’s Prescriptions)

May 14, 2007

There are some universal code convention for coding known as Dr. P’s Prescription for coding. Basically these are useful for C, Java, C++, C# and many other language too.

 Use parentheses to make expressions readable.

 Use one statement to a line, except very short statements that a re-conceptually related, which can be on the same line. A compound statement brace comes on the same line as its controlling condition. Its matching terminating brace is lined up under the initial letter of the keyword starting the statement. A function body is a compound statement and starts on its own line.

 Everything after the opening (left) brace is indented a standard number of spaces— for example, as in this text, three spaces. The matching, closing (right) brace causes subsequent statements to be lined up under it.

 Global statements or declarations start in column 1.

 For readability, a space is added after each token, except for the semicolon and un-ary operators.

 Declarations at the head of a block are followed by a blank line.

 Parenthesize the return expression whenever it is not a simple expression.

 The return from main() of the integer constant 0 is considered implicit. The practice of explicitly returning 0—or not—is discretionary.

 To detect errors, include a default in the switch statement, even when all the expected cases have been accounted for.

 Avoid side-effect operators, such as ++, in complex expressions, unless they are used in a known idiomatic style.

 Use prefix increment and prefix decrement in preference to post fix when either can be used.

 Avoid casting expressions.

 When possible, use the break or continue statement, rather than a goto.

Source: Ira Pohl’s C++ by Dissection


Connect MySql with C#

May 12, 2007

MySql is one of most useful and widely distributed database. Though user of C# often use MsSql but it is also possible to connect MySql with C#. Here is the procedure to connect MySql with C#.

Fisrt of we need to download the connector from dev.mysql.com as we do it for java. Go to http://dev.mysql.com/downloads/connector/net/5.1.html to download the latest version of Connector/Net. Install the connector to your pc then in a new or existing project add reference to: MySql.Data
then add “using MySql.Data.MySqlClient;”

after adding these two in any function or sapce add the following portion of code to test your connection. Here the default user name of the database is root.

string MyConString = “SERVER=localhost;” + “DATABASE=name_of_db;” + “UID=root;” + “PASSWORD=ur_passowrd;”;

MySqlConnection connection = new MySqlConnection(MyConString);

MySqlCommand command = connection.CreateCommand(); MySqlDataReader Reader;

command.CommandText = “select * from table1″;

connection.Open(); Reader = command.ExecuteReader();

while (Reader.Read()) {

string thisrow = “”;

for (int i = 0; i < Reader.FieldCount; i++)
thisrow += Reader.GetValue(i).ToString() + “,”;
System.Console.WriteLine(thisrow);}

connection.Close();

By: Md. Shahjalal


Follow

Get every new post delivered to your Inbox.