天天看点

编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础Written In The FontDon't code by confusing letters in the constants and variablesDon't change the constants into the variableMake the the types of ternary operators the same.

    the reasonable man adapts himself to the world;the unreasonable one persists in trying to adapt the world to himself. —萧伯纳

    相信自己看得懂就看得懂了,相信自己能写下去,我就开始写了.其实也简单—泥沙砖瓦浆木匠

today , i am writing my java notes about <编写高质量代码改善java程序的151个建议> -秦小波.

three pieces[1-3]:

         1.don't code by confusing letters in the constants and variables  [不要在常量和变量中出现易混淆的字母]

         2.don't change the constants into the variable.                           [莫让常量蜕变成变量]

         3.make the the types of ternary operators the same.                 [三元操作符的类型务必一致]

from the book:

outputs:

1

<code>2</code><code>  </code>

(unbelieved?just ctrl c + ctrl v run the code !)

in my words:

    ‘1l’is not ‘11’. but we always code by the confusing things .

then bebug for a long time , finally we will laught at ourselves with

the computer and codes face to face.”what fucking are the

coder?”lol,smile ! carm down , because u r so lucky to read this . i

will tell some excemples to keep them away. away ? really away? 

step 1: somthing about coding standards

     constants should always be all-uppercase, with underscores to separatewords. [常量都要大写,用下划线分开]

 see a case from my project itp:

   camel case:[变量命名驼峰原则,自然你也可以选择其他的法则等]

        if u wanna do it , right now ! it can make your codes more beautiful and clean! amazing ! u learned it , keep on!!!

#please remeber the camel , then u can write a nice code.

编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础Written In The FontDon't code by confusing letters in the constants and variablesDon't change the constants into the variableMake the the types of ternary operators the same.

step 2: somthing can make your program easier to understand

    some letters should not be used with the numbers,like  l o … they

are the brother of the numbers.but we can do some to avoid. like use

‘l’ , and write some notes about them.    

编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础Written In The FontDon't code by confusing letters in the constants and variablesDon't change the constants into the variableMake the the types of ternary operators the same.

a magical demo:

#i think the demo is bad. rand_cosnt is not a constant and we never do that.

what is constants ?

    constants are immutable values which are known at compile time and do not change for the life of the program.but if the project is too large to manage.there will be a problem.let me show u!

编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础Written In The FontDon't code by confusing letters in the constants and variablesDon't change the constants into the variableMake the the types of ternary operators the same.

example:

class a's goodnumber = 0.618

now we  change a.java –&gt; goodnumber to “0.6180339887”

“javac a.java”then “java b” , we will find the outputs is the same:

why??????????????????

       just see the class of b, use “ javap –c b”:

#3: ldc #3 // string class a's goodnumber = 0.618

ok , we see! the last interface a was already in the class b. so we can “javac b.java”to deal.

all in all ,

a best way store constants : static fianl  xxx    static object getxxx()

it shows the java dynamic advantage and a constant principle.

<code>false</code>

  

see the compiled code ,use “javap –c client03”,we will see:

the transformation rules about ternary operators.

三元操作符类型的转换规则:

        1.若两个操作数不可转换,则不做转换,返回值为object 类型。

        2.若两个操作数是明确类型的表达式(比如变量),则按照正常的二进制数字来转换,int 类型转换为long 类型,long 类型转换为float 类型等。

        3.若两个操作数中有一个是数字s,另外一个是表达式,且其类型标示为t,那么,若数字s 在t 的范围内,则转换为t 类型;若s 超出了t 类型的范围,则t 转换为s类型(可以参考“建议22”,会对该问题进行展开描述)。

        4.若两个操作数都是直接量数字(literal) ,则返回值类型为范围较大者。