Niuke news publishing system-Learning Web tips and summary, and news publishing system web

Source: Internet
Author: User
Tags coding standards

Niuke news publishing system-Learning Web tips and summary, and news publishing system web
July November 10, 2014 is an unforgettable day. On this day, the BS learning of xiaobian started, starting with BS. Starting from niuyun's news publishing system, all the content I learned earlier was about CS, after the soft test, I began to learn BS. I had been in contact with BS for several days. I felt great when I followed Niu Yu's news and publishing system. I watched the first 20 episodes, I found that Niu Yu really learned the full implementation of the Party's ideas and followed Niu Yu's learning. I found a lot of small knowledge that made me feel lost, and every time I made my small universe burst, now I will share these tips with my little friends. This blog post mainly describes three aspects of knowledge. First, how to make the code neat and beautiful, second: code comments, and third: Tips for connecting to the database.

First, how to make the code neat and beautiful

Why write beautiful code? This is very important, because when writing code, in addition to running it, there is also a need to look at it. You should look at it yourself, and others should also look at it. If the writing is messy, it is not clear, looking back later, I thought that I could use the Spring Festival Gala sentence to summarize it. It was disgusting that her mother had opened the door and finally got home. So what is beautiful code? First of all, nice looking, at least neat, there won't be too long rows, there won't be too long variable names, batch value assignment statements, initialization statements are aligned, there are correct and obvious indentation, there won't be too short variable names. Secondly, it is easy to understand. This feature is very abstract, and some code can be understood at a glance, but it is very troublesome to write code. Therefore, beautiful code should be read smoothly, understand what your intention is, and there is nature. Everything goes well. Finally, there is no resistance. This may be more abstract, rather than rules everywhere. There are limits everywhere and restricted relationships everywhere, just like the feeling given by the slack.

How to write beautiful code is very important first. Let the code tell you how important it is to name it. It is almost the culprit of the success or failure of many software projects, the reason is that the Code not only supports the business logic of 0 and 1 running in the computer system, but also the standard language for developers to communicate and study, with no significance or ambiguity in naming, just like two people waiting for communication, when faced with a pile of Mars, communication becomes a disaster, which will lead to many problems. At the same time, good names are self-explanatory, let the code tell the developer who I am, what I do, and how I do it. Of course, in addition to the necessary comments and descriptions of the static type, the dynamic type of code can also contain the role of passing information, let the code tell you, because the code is "Live code ". In the NIU news publishing system, Niu Yu teaches Xiao a skill that ctrl + k and d can be used after each writing. We can quickly tidy up our code, and it is not a horizontal alignment, the entire module is neat at once. If you are interested, you can give it a try.

Second: code comments

Note: When writing a program, the writer gives explanations or prompts for statements, program segments, and functions to improve the readability of the program code and facilitate future reference and modification. Annotation is an explanatory text. When running a program, it will be skipped by the program and not processed. In the small Editor's understanding, the annotation is similar to when the small Editor learns the classical Chinese text when he is a child, the lower part of the textbook is the process of translating obscure text from ancient texts into modern discourse. For a programmer, programming norms are the top priority. I still remember that when I reconstructed the charging system for the IDC room, the comment was called a few. The small editor was changed and changed. Of course, do not just think of the programming specification as a code specification in a narrow sense, the entire software development process requires specification, and software engineering itself is a specification. Next, let's give you some comments.
In the world of coding standards, annotations play a leading role. So how is the comment important? First, consider it from the encoding's own perspective. Generally, a project is completed in a day or two. It usually takes weeks, months, or even years. Imagine how much can we understand if we didn't write the corresponding code comment? How long do we need to think back to our own ideas? This is not a waste of time! These can be compensated by writing comments. Second, from the perspective of others. We write code not only for ourselves, but also for others to understand. Maintenance accounts for most of the software development process. The maintainer may not be the person who originally wrote the code. You may be a master and have a high ability to write code. It should have been a day's work. As a result, you did not write comments and it was half a day. On the surface, it seems that you are working very efficiently. But we should think about it for others. It may take two days or even longer for maintenance personnel to understand your code. This is why many people are unwilling to do maintenance work. We always say that as a qualified programmer, We need to always think about our customers. Don't we have to think about it for our fellow citizens who are fighting with us? Finally, from the company's perspective. If we do not write comments, the work is completed for half a day, saving the company half a day. However, it may take two days for the maintenance personnel to understand your code. You can imagine how much "benefit" you bring to the company "! Q: If you are a company boss, Will you hire such a programmer? Certainly not! In short, the comment is very important, and it is a bridge between programmers.

There is a saying: "A person's life is not just for himself ". Therefore, every line of code is not enough to implement this function. The key is maintenance. Therefore, it is quite necessary to mention this annotation. As we have just said, when our program is transferred to someone else, if our code is not annotated, no one is willing to write your code, it also adds unnecessary trouble to future maintenance. Some people say that it is better to change the green color with a // annotation. In fact, this is much better than not commenting, but if you use //, I think you will find another world. Let's take a look at this effect:

Let's take a look at the code we wrote in SQLHelper:

/// <Summary> /// execute the query SQL statement or stored procedure /// </summary> /// <param name = "plain text"> query the SQL statement or stored procedure </param> /// <param name = "ct"> command type </param> /// <returns> </returns> public DataTable ExecuteQuery (string plain text, commandType ct) {DataTable dt = new DataTable (); cmd = new SqlCommand (plain text, GetConn (); cmd. commandType = ct; using (sdr = cmd. executeReader (CommandBehavior. closeConnection) {dt. load (sdr) ;}return dt ;}
Then, when we call this method, what kind of magic screen will appear:

Third: How to Securely connect to the database

From the charging system of the VB data center to the present, we can see that many of our friends are connected to the database. If I use SQL Server for identity authentication, the user name is sa and the password is: 123456. It feels good to remember, however, it was found that this was widely used by many people, and most people used it for testing. Therefore, other people in their own databases could go in with their eyes closed, and the security was not guaranteed. Some friends may say that the database is connected once for one operation, so it will not stop using:

String connStr = @ "server = (local); database = newssystem; uid = sa; pwd = 123456"

In fact, according to the principle of high cohesion and low coupling, this is not in line with the requirements. Then we need to constantly look for this sentence, and then change it in one place. In case of a trembling hand, one less change or one more space, then, the results are returned. We connect to the database in web. config, which makes it much easier for our computer to change the password for connecting to the database.

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- For more information about how to configure ASP. NET applications, visit the http://go.microsoft.com/fwlink? LinkId = 169433 --> <configuration> <system. web> <compilation debug = "true" targetFramework = "4.5"/> If we change the password, we can simply change the pass value and do not need to modify it in the code because it is in an external file, we only need to open this file in text: We found the web in the program that saves our web. the config file can be directly modified after it is opened. This is an introduction. Next, let's take a look at our key part:

In our DAL, SQLHelper writes the following:
Database connection:

public sqlHelper()       {           string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;           conn = new SqlConnection(connStr);       }
This benefit is self-evident. But don't forget to add a. net reference "System. Configuration"

Otherwise, your ConfigurationManager cannot proceed.

Editor's note: this blog post is the first time that I have written BS knowledge. I am very excited. This blog post has three tips: first, how to make the code neat and beautiful. Second, code comments; third, how to Securely connect to the database, this little knowledge is like a small wave of waves on the BS Road, looking forward to these small waves stir up waves, let's go further and further on the IT Road ......




Related Article

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.