Bác có thể cho một ví dụ về code trước khi được "làm đẹp" và code sau khi được "làm đẹp" ko?
Tui tham gia diển đàn cũng đã lâu, nhưng tới giờ vẫn không hết cảm giác khó chịu và càng khó chịu hơn khi đọc source code của các bạn post lên trong các topic trao đỗi, thảo luận, giải thuật. Nhưng nhất là các topic nhờ tìm giúp, fix bug giúp. Bản thân người đọc như tôi và các bạn khác có muốn giúp thì không khỏi thấy khó chịu, lười và không giúp khi nhìn cái đống code ẩu tã, không có coding style gì hết, khai báo, viết code tùm lum, không một quy tắc nào hết (ghét nhất là cứ thích viết code cho ngắn, 2-3 lệnh trên 1 dòng). Thông thường code nào cần tìm bug thì tui copy về, paste vào 1 new file, format lại code cho dể nhìn, rõ ràng, rồi mới build, debug.
Bây giờ tui xin giới thiệu 1 tool tui thường dùng và dùng rất lâu, mấy năm rồi, dùng để format lại source code C/C++, C#, Java theo coding style mà mình chọn và thay đổi được. Tui đã thử rất nhiều tool, shareware, freeware, GPL có, và cuối cùng chọn thằng này: AStyle. Trang chủ: http://astyle.sourceforge.net/.
Các bạn xem thêm thông tin ở trang này, chịu khó đọc một chút.
Tool này khi down về sẽ có source code, document HTML, và file .exe. Nếu chỉ cần chạy, các bạn chỉ cần file .exe và tự tạo 1 file text .cfg.
Tui attach lên đây file .exe mới nhất và file .cfg của tui. Các bạn extract ra thư mục nào đó trong %PATH%, rồi muốn format file nào thì chỉ cần gõ command sau: astyle file.c/.cpp/.cs/.java trong cửa sổ Command Prompt (hay add và tool menu section của Editor tool mà bạn đang dùng). UltraEdit mà tui đang dùng tích có hợp sẵn AStyle này.
Hy vọng các bạn ưng ý và dùng tool này lâu dài. Khi post code mình lên, chịu khó dùng AStyle format dùm 1 cái![]()
Attach "quài" không được, thôi chỉ post file .cfg của tui lên thôi. Các bạn copy và save lại thành file astyle.cfg, copy file này vào chung thư mục với astyle.exe.
Code:# Default parsing is of C/C++/C# files mode=c # ANSI style formatting/indenting style=ansi # Indent switch blocks indent-switches # Add extra indentation to labels indent-labels # Set 4 spaces per indent indent=spaces=4 # Break brackets from their pre-block statements brackets=break # Suffix of original files should be .pre suffix=.bak # Converts tabs into single spaces convert-tabs # Insert space padding around operators pad=oper # Print errors and help information to standard-output rather than to standard-error errors-to-standard-output # Add extra indentation to namespaces indent-namespaces
Bác có thể cho một ví dụ về code trước khi được "làm đẹp" và code sau khi được "làm đẹp" ko?
pEnGwINUS.
Cậu post 1 đoạn code mà cậu thấy khó đọc nhất lên đi !
Em có máu hay quên, post bài ở đây rồi lại quên mất, đến hôm nay ghé qua mới nhớ. ^_^.
Em vớ một đoạn code và làm bầy bừa nó ra như thế này. Anh dùng chuơng trình chỉnh hộ em. ^_^.
Code:#include <stdio.h> #include <conio.h> #include <stdlib.h> int ReadFile(FILE *f);void Input(int ***aInput, int& iV); void ShowGraph(int **aInput, int iV); bool IsAdjacent(int **aInput, int *a, int iMax, int b); int RemoveItem(int *a, int iMax, int x);void Colouring(int** aInput, int* aColor, int iV);void Output(int* aColor, int iV); void main() { int **aInput, *aColor; int iV; Input(&aInput, iV); aColor = (int*) malloc(iV * sizeof(int)); ShowGraph(aInput, iV); Colouring(aInput, aColor, iV); Output(aColor, iV); } int ReadFile(FILE *f) { int i = 0, c; c = fgetc(f); while(c != EOF && c != ' ' && c != '\n') { i = (i * 10) + (c - '0'); c = fgetc(f); } return i; } void Input(int ***aInput, int& iV){ int i, j; FILE *f; if((f = fopen(" input.txt","r")) == NULL) { printf(" Can not open input.txt"); exit(0); } iV = ReadFile( f); *aInput = (int**) calloc(iV, sizeof(int*)); for(i = 0; i < iV; i++) ( *aInput)[i] = (int*) calloc(iV, sizeof(int)); for(i = 0; i < iV; i++) for(j = 0; j < iV; j++) (*aInput)[i][j] = ReadFile(f); fclose(f); } // Display data of graph which has been loaded from text file void ShowGraph(int **aInput, int iV){ int i, j; printf("Graph represented by adjacent matrix: \n"); for(i = 0; i < iV; i++) { printf("\n"); for(j = 0; j < iV; j++) { printf("%3d",aInput[i][j]); } } } // Do all of array a elements adjacent b ? bool IsAdjacent(int **aInput, int *a, int iMax, int b) { for(int i = 0; i < iMax; i++) if(aInput[a[i]][b]) { return 1; } return 0; } // Get an item from a and remove it from a int RemoveItem(int *a, int iMax, int x) { int iReturn = a[x]; for(int i = x; i < iMax; i++) a[i] = a[i+1]; return iReturn; } // Use greedy algorithm to colouring all vertexs void Colouring(int** aInput, int* aColor, int iV) { int *v, *s; int iS; int x, y, i, m = 0; s = (int*) malloc(iV * sizeof(int)); v = (int*) malloc(iV * sizeof(int)); for(i = 0; i < iV; i++) v[iV - i -1] = i; while(iV > 0) { iS = 0; // Get x from v x = v[iV - 1]; iV--; // Colouring x m++; aColor[x] = m; // Put x to s s[iS] = x; iS++; for(y = 0; y < iV; y++) { if(!IsAdjacent(aInput, s, iS, v[y])) { // Colouring y aColor[v[y]] = m; // Put y to s s[iS] = v[y]; iS++; // Remove y from v RemoveItem(v,iV,y); iV--; y--; } } } } // Output result to monitor void Output(int* aColor, int iV) { printf("\ n\nOutput:\n"); printf("\n%8s","Vertex:"); for(int i = 0; i < iV; i++) printf("%5d",i + 1); printf("\n%8s", "Color:"); for(int i = 0; i < iV; i++) printf("%5d",aColor[i]); getch(); }
pEnGwINUS.
Giống đánh đố tui quá !
Code đã format theo Ansi_C/C++ style, 2 blank line để phân cách hàm, include, prototype. 1 blank line cho các block trong hàm.
Code:#include <stdio.h> #include <conio.h> #include <stdlib.h> int ReadFile(FILE *f); void Input(int ***aInput, int& iV); void ShowGraph(int **aInput, int iV); bool IsAdjacent(int **aInput, int *a, int iMax, int b); int RemoveItem(int *a, int iMax, int x); void Colouring(int** aInput, int* aColor, int iV); void Output(int* aColor, int iV); void main() { int **aInput, *aColor; int iV; Input(&aInput, iV); aColor = (int*) malloc(iV * sizeof(int)); ShowGraph(aInput, iV); Colouring(aInput, aColor, iV); Output(aColor, iV); } int ReadFile(FILE *f) { int i = 0, c; c = fgetc(f); while (c != EOF && c != ' ' && c != '\n') { i = (i * 10) + (c - '0'); c = fgetc(f); } return i; } void Input(int ***aInput, int& iV) { int i, j; FILE *f; if ((f = fopen("input.txt", "r")) == NULL) { printf(" Can not open input.txt"); exit(0); } iV = ReadFile( f); *aInput = (int**) calloc(iV, sizeof(int*)); for (i = 0; i < iV; i++) { (*aInput)[i] = (int*) calloc(iV, sizeof(int)); } for (i = 0; i < iV; i++) { for (j = 0; j < iV; j++) { (*aInput)[i][j] = ReadFile(f); } } fclose(f); } // Display data of graph which has been loaded from text file void ShowGraph(int **aInput, int iV) { int i, j; printf("Graph represented by adjacent matrix: \n"); for (i = 0; i < iV; i++) { printf("\n"); for (j = 0; j < iV; j++) { printf("%3d", aInput[i][j]); } } } // Do all of array a elements adjacent b ? bool IsAdjacent(int **aInput, int *a, int iMax, int b) { for (int i = 0; i < iMax; i++) if (aInput[a[i]][b]) { return 1; } return 0; } // Get an item from a and remove it from a int RemoveItem(int *a, int iMax, int x) { int iReturn = a[x]; for (int i = x; i < iMax; i++) a[i] = a[i+1]; return iReturn; } // Use greedy algorithm to colouring all vertexs void Colouring(int** aInput, int* aColor, int iV) { int *v, *s; int iS; int x, y, i, m = 0; s = (int*) malloc(iV * sizeof(int)); v = (int*) malloc(iV * sizeof(int)); for (i = 0; i < iV; i++) v[iV - i -1] = i; while (iV > 0) { iS = 0; // Get x from v x = v[iV - 1]; iV--; // Colouring x m++; aColor[x] = m; // Put x to s s[iS] = x; iS++; for (y = 0; y < iV; y++) { if (!IsAdjacent(aInput, s, iS, v[y])) { // Colouring y aColor[v[y]] = m; // Put y to s s[iS] = v[y]; iS++; // Remove y from v RemoveItem(v, iV, y); iV--; y--; } } } } // Output result to monitor void Output(int* aColor, int iV) { printf("\ n\nOutput:\n"); printf("\n%8s", "Vertex:"); for (int i = 0; i < iV; i++) printf("%5d", i + 1); printf("\n%8s", "Color:"); for (int i = 0; i < iV; i++) printf("%5d", aColor[i]); getch(); }
@hieubm : sao không tải về thử đi.Mà tui thấy chữ ký của bạn rùi đó
Chử ký này của hieubm "hơi bị" sốc quá, khiêm tốn 1 chút đi em !~~~~~~ Không biết có ai phát hiện ra bí mật này không nhỉ ? ~~~~~~
CH-C-C
~~~~~~ Chắc chẳng có ai biết đâu ~~~~~~
TA LÀ VÔ ĐỊCH THIÊN HẠ ! HA HA HA ! HÊ HÊ HÊ !
uhm, Tool của bác cũng hay đó. ^_^. Nhưng em viết code cũng khá theo quy tắc chuẩn rồi. Ko biết có cần cái này ko. Thôi thì cứ dùng thử xem ^_^.
Mà tui thấy chữ ký của bạn rùi đóHi hi, nghịch chút cho vui thôi mà. Mà em khiêm tốn nên mới để nó trùng với màu nền đấy, tại em ko thích bày tỏ cho mọi người biết sự thật. Hi hi hi hi.Chử ký này của hieubm "hơi bị" sốc quá, khiêm tốn 1 chút đi em !
Đùa chút thôi, em đổi chữ kí đây
pEnGwINUS.
Mọi người nên dùng tool này, vì ngoài format code của mình ra, đôi khi mình cần tham khảo, đọc, fix bug code của người khác (từ Internet, trong team, bạn bè... ) Lúc đó nếu có cảm giác khó chịu về coding style của coder đó thì cứ AStyle là xong.
Lúc trước, khi còn làm coder, tui đề ra coding style thống nhất cho mọi người trong team (5 - 10 người). Lúc đầu code review rất cực vì mỗi người code một kiểu, chưa sữa được. Từ từ rồi quen dần, vào khuôn khổ. Nhìn code 10 người như 1, member mới vào team thì phải theo coding style đó, code từ library # đưa qua thì AStyle lại.
Không hiểu sao có vài đoạn nó ko format được anh TQN ơi
Với mấy chỗ dấu { sau hàm nữa. Em muốn nó phải xuống dòng mới T_TCode:void Input(int ***aInput, int& iV){ int i, j; FILE *f; if ((f = fopen(" input.txt","r")) == NULL) { printf(" Can not open input.txt"); exit(0); } iV = ReadFile( f); *aInput = (int**) calloc(iV, sizeof(int*)); for (i = 0; i < iV; i++) ( *aInput)[i] = (int*) calloc(iV, sizeof(int)); for (i = 0; i < iV; i++) for (j = 0; j < iV; j++) (*aInput)[i][j] = ReadFile(f); fclose(f); } ..... void ShowGraph(int **aInput, int iV){ int i, j; printf("Graph represented by adjacent matrix: \n"); for (i = 0; i < iV; i++) { printf("\n"); for (j = 0; j < iV; j++) { printf("%3d",aInput[i][j]); } } }
Keep moving forward!
... Retired ...