Programming is an art. The following is an aesthetic perspective. The following are seven methods to write beautiful code:
1. End the if statement as soon as possible
For example, the following JavaScript statement looks terrible:
1 function findshape (flags, point, attribute, list ){
2 If (! Findshapepoints (flags, point, attribute )){
3 if (! Dofindshapepoints (flags, point, attribute )){
4 If (! Findinshape (flags, point, attribute )){
5 If (! Findfromguide (flags, point ){
6 if (list. Count ()> 0 & flags = 1 ){
7 dosomething ();
8}
9}
10}
11}
12}
13}
But it looks much better if you write it like this:
1 function findshape (flags, point, attribute, list ){
2 If (findshapepoints (flags, point, attribute )){
3 return;
4}
5
6 if (dofindshapepoints (flags, point, attribute )){
7 return;
8}
9
10 if (findinshape (flags, point, attribute )){
11 return;
12}
13
14 if (findfromguide (flags, point ){
15 return;
16}
17
18 if (! (List. Count ()> 0 & flags = 1 )){
19 return;
20}
21
22 dosomething ();
23
24}
You may dislike the second expression, but it reflects the idea of returning the if value quickly. It can also be understood as: Avoid unnecessary else statements.
2. If it is only a simple boolean operation (logical operation), do not use the if statement.
For example:
1 function isstringempty (STR ){
2 If (STR = ""){
3 return true;
4}
5 else {
6 RETURN false;
7}
8}
It can be written as follows:
1 function isstringempty (STR ){
2 Return (STR = "");
3}
3. Use blank space. This is free of charge.
For example:
1 function getsomeangle (){
2 // some code here then
3 radangle1 = math. atan (slope (center, point1 ));
4 radangle2 = math. atan (slope (center, point2 ));
5 firstangle = getstartangle (radangle1, point1, center );
6 secondangle = getstartangle (radangle2, point2, center );
7 radangle1 = degreestoradians (firstangle );
8 radangle2 = degreestoradians (secondangle );
9 baseradius = distance (point, center );
10 radius = baseradius + (lines * y );
11 P1 ["X"] = roundvalue (radius * Math. Cos (radangle1) + center ["X"]);
12 P1 ["Y"] = roundvalue (radius * Math. Sin (radangle1) + center ["Y"]);
13 pt2 ["X"] = roundvalue (radius * Math. Cos (radangle2) + center ["Y"]);
14 pt2 ["Y"] = roundvalue (radius * Math. Sin (radangle2) + center ["Y ");
15 // now some more code
16}
Many developers do not want to use blank spaces, as if they are charged. I am not deliberately adding blank spaces here, and rudely interrupt the code consistency. In the process of writing the code, it is easy to add a blank space to the position, which is not only beautiful but also easy to understand, as shown below:
1 function getsomeangle (){
2 // some code here then
3 radangle1 = math. atan (slope (center, point1 ));
4 radangle2 = math. atan (slope (center, point2 ));
5
6 firstangle = getstartangle (radangle1, point1, center );
7 secondangle = getstartangle (radangle2, point2, center );
8
9 radangle1 = degreestoradians (firstangle );
10 radangle2 = degreestoradians (secondangle );
11
12 baseradius = distance (point, center );
13 radius = baseradius + (lines * y );
14
15 P1 ["X"] = roundvalue (radius * Math. Cos (radangle1) + center ["X"]);
16 P1 ["Y"] = roundvalue (radius * Math. Sin (radangle1) + center ["Y"]);
17
18 pt2 ["X"] = roundvalue (radius * Math. Cos (radangle2) + center ["Y"]);
19 pt2 ["Y"] = roundvalue (radius * Math. Sin (radangle2) + center ["Y ");
20 // now some more code
21}
4. Do not use unnecessary comments.
Meaningless comments are annoying. Do not Mark obvious comments. In the following example, everyone knows that the Code expresses "Students id", so it is unnecessary to mark it.
1 function existsstudent (ID, list ){
2 For (I = 0; I <list. length; I ++ ){
3 student = list [I];
4
5 // get the student's ID
6 thisid = student. GETID ();
7
8 If (thisid = ID ){
9 return true;
10}
11}
12 Return false;
13}
5. Do not leave the deleted code in the source file, even if you mark it
If you use version control, you can easily retrieve the code of the previous version. If you read your code at full cost and find that you want to delete the code, it is so annoying.
// Function thisreallyhandyfunction (){
// Somemagic ();
// Somemoremagic ();
// Magicnumber = evenmoremagic ();
// Return magicnumber;
//}
6. Do not have code that is too long.
It is really hard to look at code that is too long, especially the Code itself has very little functionality. As follows:
1 public static enummap <category, intpair> getgroupcategorydistribution (enummap <category, integer> sizes, int groups ){
2 enummap <category, intpair> categorygroupcounts = new enummap <category, intpair> (category. Class );
3
4 For (Category Cat: category. Values ()){
5 categorygroupcounts. Put (CAT, getcategorydistribution (sizes. Get (CAT), groups ));
6}
#
I am not saying that I have to stick to the length of 70 characters, but an ideal length is controlled within 120 characters. If you publish code on the Internet, it is very difficult for users to read it.
7. Do not have too many lines of code in a function (or function ).
One of my colleagues once said that visual C ++ is stinking because it does not allow you to have more than 10,000 lines of code in a function. I can't remember the upper limit of the number of lines in the code. I don't know if he is correct, but I disagree with him. If there are more than 50 rows of a letter, how hard it looks, you know, there are endless if loops, and you can scroll the mouse to check the code. For me, more than 35 lines of code are difficult to understand. I suggest dividing a function code into two if the number is exceeded.