Wednesday, August 27, 2008

如何寫 Beautiful code

剛剛讀了一篇文章,發現跟我的想法不謀而合,因此抓下來供自己或參觀我的 blog 參考
所謂 beautiful code 並不是要證明寫的 code 很厲害確難懂,而是清楚易懂又有效率
不易懂的 code 讓自己或別人無法輕易理解及重複使用

Class.forName for Methos.invoke 等是我最不建議使用,因為用了 System level 的呼叫方式
這些方式通常屬於較底層的寫法,用在平常開發 Web Application 則顯得格格不入

Beautiful Coding - The way to life.

I often have to fix other people’s code. You can tell coders who have been coding for years simply by the feel of their code.

One of the problems with java is the simple fact that you can end up with a spaghetti-junction of objects all calling, calling back, implementing, sub classing, overriding, running ejb’s etc etc and it can be a bit of a nightmare to find your way through.

The key here (although you can reverse engineer into a UML Model with Netbeans) is really in the way that the code is written.

Things I do to make code more readable

- Initials of variables

All my strings for instance start with s - sName, sUsername
integers I , iCount, iGold
doubles d

and so on for different types of variables or objects.

Components on forms I use a 3 letter start up.

txtTEXTBOX
lblLABEL
stcSTATICTEXT
cmbCOMBOBOX

Also in the code using the java doc function, within Netbeans if you type /** enter it will auto document the next function and you just “fill in the blanks”, javaDoc is fantastic and well worth looking into. If you have “java doced” your code, not only does Netbeans pick up the documentation when you are typing or using your own functions but also you can generate a website that documents your classes!!! How Call

ALT-SHIFT-F - auto format your code, very useful.

Comment long bracketed loops. I always put // from next to a bracketed loop that is too long to see in one screen. e.g.

for (int iCount=0;iCount<10;++icount)> 

Also comment all calls to EJB’s to say what they do -

// Get this job’s History
JobManagerBean.getHistory(sJobNumber);

Beautiful Code!

Lisp programmers write beautiuful code, why? Because you have to, it becomes unreadable otherwise. In a way I wish all programmers would learn Lisp - because it makes you think in a different way and see software development from an almost etherial height. When I get some further research time I want to work on a lisp implementation for netbeans.

Remember that you will not be around forever, don’t punish the next poor programmer who has to work on your code. I like my code to be really easy to read and understand.

Code Rewriting

You know that sometimes I will rewrite a whole subroutine if I’m not happy with it. Programmers are artists, the difference is that the result of our art is a system for people to use or some form of output. Well written, neat, fast, efficient code means happy users and that is the caling of a programmer, to make the lives of the users easier.

If your software makes it harder. It needs rewriting, or maybe replacing with a pen and paper.

Be passionate about your coding and your users will be passionate about your software.



出處 : Beautiful Coding - The way to life.

No comments: