鍨冨溇浠g爜涔﹀啓鍑嗗垯锛堟湁鎰忔€濓級

Posted Java鏀诲煄甯?,

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍨冨溇浠g爜涔﹀啓鍑嗗垯锛堟湁鎰忔€濓級相关的知识,希望对你有一定的参考价值。

馃挬 浠ヤ竴绉嶅鏄撻€犳垚浠g爜娣锋穯鐨勬柟寮忓懡鍚嶅彉閲?/h3>

鍛藉悕瓒婄煭锛屽氨闇€瑕佽秺澶氱殑鏃堕棿鍘绘€濊€冧唬鐮侀€昏緫绛夐棶棰樸€?/p>

Good 馃憤馃徎

int a = 42;
澶嶅埗浠g爜

Bad 馃憥馃徎

int age = 42;
澶嶅埗浠g爜

馃挬 鍙橀噺/鏂规硶鍛藉悕椋庢牸涓嶇粺涓€

涓洪鏍间笉缁熶竴骞叉澂銆?/p>

Good 馃憤馃徎

int wWidth = 640;
int w_height = 480;
澶嶅埗浠g爜

Bad 馃憥馃徎

int windowWidth = 640;
int windowHeight = 480;
澶嶅埗浠g爜

馃挬 涓嶅啓娉ㄩ噴

鍙嶆娌′汉鑳借鎳備綘鐨勪唬鐮併€?/p>

Good 馃憤馃徎

int cdr = 700;
澶嶅埗浠g爜

Bad 馃憥馃徎

娉ㄩ噴搴旇鍖呭惈涓€浜涒€滀负浠€涔堚€濓紝鑰屼笉鏄竴浜涒€滄槸浠€涔堚€濄€傚鏋滀唬鐮佽繛鏄€滀粈涔堚€濋兘琛ㄨ揪涓嶆竻妤氾紝閭d唬鐮佷篃澶儌浜嗐€?/p>

// 700ms 鐨勬暟閲忔槸浠?UX A/B 娴嬭瘯缁撴灉涓緱鍒扮殑涓€涓粡楠屽€笺€?// @鏌ョ湅: <璇︾粏瑙i噴 700 鐨勪竴涓摼鎺?gt;
int callbackDebounceRate = 700;
澶嶅埗浠g爜

馃挬 浣跨敤姣嶈鍐欐敞閲?/h3>

濡傛灉浣犵殑姣嶈鏄嫳璇紝閭d箞璇峰拷鐣ヨ繖鏉″噯鍒欍€?/p>

Good 馃憤馃徎

// 袟邪泻褉懈胁邪褦屑芯 屑芯写邪谢褜薪械 胁褨泻芯薪械褔泻芯 锌褉懈 胁懈薪懈泻薪械薪薪褨 锌芯屑懈谢泻懈.
toggleModal(false);
澶嶅埗浠g爜

Bad 馃憥馃徎

// 闅愯棌閿欒寮圭獥
toggleModal(false);
澶嶅埗浠g爜

PS锛氬鏋滆嫳璇功鍐欒兘鍔涗笉鏄緢寮虹殑璇濓紝寤鸿杩樻槸鐢ㄦ瘝璇惂銆傛瘯绔熻娓呮鎬绘瘮璇翠笉娓呮瑕佸己銆?/p>

馃挬 澹版槑鍙橀噺鐨勯鏍间笉缁熶竴

鍐嶆涓洪鏍间笉缁熶竴骞叉澂銆?/p>

Good 馃憤馃徎

String [] i1 = {"娌?, "榛?, "鐜?, "浜?};
String i2 [] = {"娌?, "榛?, "鐜?, "涓?};
澶嶅埗浠g爜

Bad 馃憥馃徎

String [] wanger = {"娌?, "榛?, "鐜?, "浜?};
String wangsan [] = {"娌?, "榛?, "鐜?, "涓?};
澶嶅埗浠g爜

馃挬 灏藉彲鑳芥妸浠g爜鍐欐垚涓€琛?/h3>

Good 馃憤馃徎

IntStream.range(1, 5).boxed().map(i -> { System.out.print("Happy Birthday "); if (i == 3) return "dear NAME"; else return "to You"; }).forEach(System.out::println);
澶嶅埗浠g爜

Bad 馃憥馃徎

for (int i = 1; i < 5; i++) {
    System.out.println("Happy Birthday " + (i == 3 ? "dear NAME" : "to you"));
}
澶嶅埗浠g爜

馃挬 瀵归敊璇俊鎭笉绠′笉椤?/h3>

鏃犺浠€涔堟椂鍊欏彂鐜伴敊璇紝閮芥病鏈夊繀瑕佽鍏朵粬浜虹煡閬撱€?/p>

Good 馃憤馃徎

try {
  // 鎰忔枡涔嬪鐨勬儏鍐点€?} catch (error) {
  // tss... 馃か
}
澶嶅埗浠g爜

Bad 馃憥馃徎

try {
  // 鎰忔枡涔嬪鐨勬儏鍐点€?} catch (error) {
  // and/or
  logError(error);
}
澶嶅埗浠g爜

馃挬 浣跨敤澶ч噺鐨勫叏灞€鍙橀噺

鍏ㄧ悆鍖栫殑鍘熷垯銆?/p>

Good 馃憤馃徎

int x = 5;

void multi() {
  x = x * 2;
}

multi(); // 鐜板湪 x 鏄?10
澶嶅埗浠g爜

Bad 馃憥馃徎

int x = 5;

int multi(int num) {
  return num * 2;
}

x = multi(x); // 鐜板湪 x 鏄?10
澶嶅埗浠g爜

馃挬 澹版槑鏍规湰涓嶄細浣跨敤鐨勫彉閲?/h3>

涓囦竴浠ュ悗鐢ㄤ簡鍛紵浠ュ涓嶆椂涔嬮渶銆?/p>

Good 馃憤馃徎

int sum(int a, int b, int c) {
  int timeout = 1300;
  int result = a + b;
  return a + b;
}
澶嶅埗浠g爜

Bad 馃憥馃徎

int sum(int a, int b) {
  return a + b;
}
澶嶅埗浠g爜

馃挬 濡傛灉鏉′欢鍏佽鐨勮瘽锛屼粠涓嶆寚瀹氱被鍨嬨€?/h3>

Good 馃憤馃徎

// 浜彈渚挎嵎鐨勫揩涔?List list = new ArrayList();
list.add("娌夐粯鐜嬩簩");
list.add(18);
澶嶅埗浠g爜

Bad 馃憥馃徎

List<String> nameList = new ArrayList<String>();

// 缂栬瘧鍑洪敊
nameList.add(18);
澶嶅埗浠g爜

馃挬 娌¢笩鐢ㄧ殑浠g爜

鐪嬭捣鏉ユ洿涓ヨ皑锛屽叾瀹炲緢澶氫綑銆?/p>

Good 馃憤馃徎

Integer multi(Object num) {
    if (!(num instanceof Integer)) {
        return null;
    } else if (num != null) {
        return (Integer) num * 2;
    }
    return null;
}
澶嶅埗浠g爜

Bad 馃憥馃徎

Integer multi(Object num) {
    if (num instanceof Integer) {
        return (Integer) num * 2;
    }
    return null;
}
澶嶅埗浠g爜

馃挬 澶ч噺鐨?if-else 宓屽

Good 馃憤馃徎

void someMethod(int a, int b, int c) {
    if (a > 0) {
        if (b > 0) {
            if (c > 0) {
               int result = a / b / c;
            }
        }
    }
}
澶嶅埗浠g爜

Bad 馃憥馃徎

void someMethod1(int a, int b, int c) {
    if (a < 0 || b < 0 || c < 0) {
        return;
    }
    int result = a / b / c;
}
澶嶅埗浠g爜

馃挬 鍙傚樊涓嶉綈鍦扮缉杩?/h3>

鍙傚樊涓嶉綈涔冨垢绂忔湰婧愩€?/p>

Good 馃憤馃徎

String [] wanger = {"娌?, 
        "榛?, "鐜?, "浜?};
String [] wangsan = {"娌?, "榛?, "鐜?, "涓?};
Arrays.asList(wanger).stream().
        forEach(System.out::println);
Arrays.asList(wangsan).
        stream().
                forEach(System.out::println);
澶嶅埗浠g爜

Bad 馃憥馃徎

String [] wanger = {"娌?, "榛?, "鐜?, "浜?};
String [] wangsan = {"娌?, "榛?, "鐜?, "涓?};
Arrays.asList(wanger)
        .stream()
        .forEach(System.out::println);
Arrays.asList(wangsan)
        .stream()
        .forEach(System.out::println);
澶嶅埗浠g爜

馃挬 浠g爜琛屾暟澶氱殑鏂规硶鐨勬瘮灏戠殑濂?/h3>

涓嶈鎶婁唬鐮侀€昏緫鍒嗘垚鍙鐨勯儴鍒嗐€?/p>

  • 涓€涓被涓殑浠g爜琛屾暟瓒呰繃 10000 琛屻€?/li>
  • 涓€涓柟娉曚腑鐨勪唬鐮佽鏁拌秴杩?1000 琛屻€?/li>
  • 涓€涓柟娉曢噷鏃㈠仛鍑忔硶澶勭悊鍙堝仛鍔犳硶澶勭悊锛岃繕鍋氫箻闄ょ殑澶勭悊銆?/li>

馃挬 涓嶈娴嬭瘯浣犵殑浠g爜

浠g爜娴嬭瘯鏄祴璇曞伐绋嬪笀鐨勪簨锛屽叧鎴戝眮浜嬨€?/p>

馃挬 閬垮厤浠g爜椋庢牸缁熶竴

闅忓績鎵€娆插湴缂栧啓浠g爜锛岀壒鍒槸鍦ㄤ竴涓洟闃熶腑鏈夊涓紑鍙戜汉鍛樼殑鎯呭喌涓嬶紝鎴戝磭灏氣€滆嚜鐢扁€濄€?/p>

馃挬 涓嶈鍐欐枃妗?/h3>

浠庝竴寮€濮嬪氨涓嶈銆?/p>

馃挬 涓嶈鍒犻櫎搴熷純鎺夌殑浠g爜

浠g爜灏界宸茬粡搴熷純浜嗭紝娉ㄩ噴鎺夊氨琛屼簡锛屾病蹇呰鍒犳帀銆?/p>

鍙傝€冿細銆?020鏈€鏂癑ava鍩虹绮捐瑙嗛鏁欑▼鍜屽涔犺矾绾匡紒銆?/a>

閾炬帴锛?a href="https://link.segmentfault.com/?enc=5vb0QyYy9TNyZh%2BWIz9Jjw%3D%3D.G8TOhDovL6ZTagS5zYm0yd5FIgb%2Bj2FvSTVhFB%2FZTvCNvpGl4e3OHN%2FnjVYIhq46" rel="nofollow">https://juejin.cn/post/692002...

以上是关于鍨冨溇浠g爜涔﹀啓鍑嗗垯锛堟湁鎰忔€濓級的主要内容,如果未能解决你的问题,请参考以下文章

銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫?/a>

鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?/h1>

鏈哄櫒瀛︿範锛?3-鍨冨溇閭欢鍒嗙被2

Docker 鍨冨溇鍥炴敹鏈哄埗琛ュ厖

銆怭OJ銆?480 Longge's problem锛堟鎷夊嚱鏁帮級