Beautiful code is also a kind of art

Source: Internet
Author: User

A few months ago I had absolutely no idea to write the code beautifully and beautifully until I saw a very beautiful piece of code.


Why write the code beautifully? Because graceful code means stability, efficiency, and legibility, a piece of code that is full of unknown bugs or lengthy wordy lines is definitely not a graceful code. Beautiful code also has a very important role to play: To make the author feel happy. Every technology-loving person will definitely be happy with their technical accomplishments. The importance of stability is self-evident, in order to be stable enough to be as far as possible to pre-judge the code may appear in the exception, to "as far as possible" need to constantly accumulate experience and learn the good coding style of others. Efficiency is another very important factor that affects the user experience after stability, and efficient code requires efficient algorithms to implement. Easy to read is to make it possible for other programmers to know the functionality of the code as simply as they can by code, which requires a bit of effort from each class, method, variable name, and the simplest possible syntax.


So how do you write beautiful code? Unlike everyone's definition of beauty, different people have different opinions about the code, and I'll write a piece of code that I think is pretty beautiful from my own to analyze how to build a beautiful piece of code from the details.


The function of the following code is to implement importing data from an Excel file into a WinForm DataGridView control.

Before writing the code we have to determine the naming rules, naming rules for the readability of the code has a very important role, I adopt the naming rules are: class names are capitalized nouns, events and delegates using the first letter lowercase the initial letter of the other words capitalized word combination, The method uses the combination of the first letter capitalization, the static variable uses the full capitalization to delimit the word, the basic type variable of the global variable takes the first letter of the basic type with an underscore and a variable name such as the initial uppercase of the initial letter of the other word, for example: string S_username, The object of the global variable class is preceded by an underscore and the first letter of the class name is a lowercase plus variable name for example: User _u_login, local variable naming rules are preceded by a global variable naming convention with the letter lowercase "l" underlined on top of each variable (instances of the class are slightly different) for example: string L_s_username User _l_u_login. With this naming convention, when my entire program uses this naming convention, any piece of code can know its scope and variable type as long as I see the name of the variable or method in it.


After the naming rules are determined, the code is written. Note: Some implementation details are not shown

public bool Importdatafromexcel (string l_s_excelfilename)

{

String l_s_conn = "";


L_s_conn = string. Formart (

@ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ' {0} ';

Extended Properties = ' Excel 8.0; Hdr=no;imex=1 ' "

, l_s_excelfilename);


OleDbConnection _l_e_conn = new OleDbConnection (L_s_conn);


if (_l_e_conn = = null) {return false;}


_l_e_conn. Open ();

OleDbDataAdapter _l_e_cmd = new OleDbDataAdapter ("SELECT * from [sheet1$]", _l_e_conn);


if (_l_e_cmd = = null) {return false;}


DataSet _l_d_data = new DataSet ();

_l_e_cmd. Fill (_l_d_data, "Table1");


DataTable _l_d_childtable = new DataTable ();

foreach (DataGridViewColumn dgvc in DGV. Columns)

{

if (DGVC. Visible && DGVC. Celltype! = typeof (Datagridviewcheckboxcell))

{

DataColumn dc = new DataColumn ();

dc. ColumnName = DGVC. DataPropertyName;

_l_d_childtable.columns.add (DC);

}

}

foreach (DataRow excelrow in DS. Tables[0]. Rows)

{

int i = 0;

DataRow dr = TB. NewRow ();

foreach (DataColumn dc in TB. Columns)

{

DR[DC] = Excelrow[i];

i++;

}

Tb. Rows.Add (DR);

}

DGV. DataSource = TB;

}


This article is from the "Technology Not Home" blog, please be sure to keep this source http://chrischen.blog.51cto.com/9954795/1684297

Beautiful code is also a kind of art

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.