Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions

Π Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΈ ΠΊ ΡΡ‚ΠΈΠ»ΡŽ ΠΊΠΎΠ΄Π°

ΠŸΡ€Π°Π²ΠΈΠ»Π° языка Java

ΠŸΡ€Π°Π²ΠΈΠ»Π° Java Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊ

Π‘ΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‚ соглашСния, ΠΏΠΎ ΠΏΠΎΠ²ΠΎΠ΄Ρƒ использования Java Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊ ΠΈ инструмСнтов для Android. Π’ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… случаях соглашСния ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½Ρ‹, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Π² Ρ‚Π°ΠΊΠΈΡ… ΠΊΠ°ΠΊ использованиС старого ΠΊΠΎΠ΄Π°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ, Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ Π½Π΅ΠΎΠ΄ΠΎΠ±Ρ€Π΅Π½Π½Ρ‹ΠΉ ΠΏΠ°Ρ‚Ρ‚Π΅Ρ€Π½ ΠΈΠ»ΠΈ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΡƒ.

ΠŸΡ€Π°Π²ΠΈΠ»Π° Java стиля

ΠŸΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ Π³ΠΎΡ€Π°Π·Π΄ΠΎ ΠΏΡ€ΠΎΡ‰Π΅ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Ρ‚ΡŒ, ΠΊΠΎΠ³Π΄Π° всС Ρ„Π°ΠΉΠ»Ρ‹ ΠΈΠΌΠ΅ΡŽΡ‚ согласованный ΡΡ‚ΠΈΠ»ΡŒ. ΠœΡ‹ слСдуСм стандартному ΡΡ‚ΠΈΠ»ΡŽ программирования Π½Π° Java, ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠΌΡƒ Sun Π² ΠΈΡ… Code Conventions for the Java Programming Language, с нСсколькими ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡΠΌΠΈ ΠΈ дополнСниями. Π”Π°Π½Π½ΠΎΠ΅ руководство ΠΏΠΎ ΡΡ‚ΠΈΠ»ΡŽ являСтся ΠΏΠΎΠ΄Ρ€ΠΎΠ±Π½Ρ‹ΠΌ ΠΈ всСсторонним, Π° Ρ‚Π°ΠΊΠΆΠ΅ ΡˆΠΈΡ€ΠΎΠΊΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ Java сообщСством.

ΠŸΡ€Π°Π²ΠΈΠ»Π° языка Java

НС ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΠΉΡ‚Π΅ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ

Π’ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, Π²Π°ΠΌ захочСтся Π½Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ ΠΊΠΎΠ΄, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€:

НС ΠΏΠ΅Ρ€Π΅Ρ…Π²Π°Ρ‚Ρ‹Π²Π°ΠΉΡ‚Π΅ ΠΎΠ±ΠΎΠ±Ρ‰Π΅Π½Π½Ρ‹Π΅ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ

Иногда Π±Ρ‹Π²Π°Π΅Ρ‚ Π·Π°ΠΌΠ°Π½Ρ‡ΠΈΠ²ΠΎ ΠΏΠΎΠ»Π΅Π½ΠΈΡ‚ΡŒΡΡ с ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΎΠΉ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ ΠΈ Π½Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ Π²Ρ€ΠΎΠ΄Π΅ этого:

Π’Π°ΠΌ Π½Π΅ слСдуСт Ρ‚Π°ΠΊ Π΄Π΅Π»Π°Ρ‚ΡŒ. Π‘ΡƒΡ‚ΡŒ Π² Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ появлСниС ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π²Ρ‹ Π½Π΅ ΠΎΠΆΠΈΠ΄Π°Π»ΠΈ ΠΈ, Π² ΠΈΡ‚ΠΎΠ³Π΅, ошибка Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΡ‚Π»Π°Π²Π»ΠΈΠ²Π°Ρ‚ΡŒΡΡ Π½Π° ΡƒΡ€ΠΎΠ²Π½Π΅ прилоТСния. Π’ΠΎ Π΅ΡΡ‚ΡŒ, Ссли ΠΊΡ‚ΠΎ-Ρ‚ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ Π½ΠΎΠ²Ρ‹ΠΉ Ρ‚ΠΈΠΏ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ, Ρ‚ΠΎ компилятор Π½Π΅ смоТСт Π²Π°ΠΌ ΠΏΠΎΠΌΠΎΡ‡ΡŒ ΠΏΠΎΠ½ΡΡ‚ΡŒ, Ρ‡Ρ‚ΠΎ это другая ошибка.

Π‘ΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‚ Ρ€Π΅Π΄ΠΊΠΈΠ΅ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ ΠΈΠ· этого ΠΏΡ€Π°Π²ΠΈΠ»Π°: ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½Ρ‹ΠΉ тСстовый ΠΊΠΎΠ΄, ΠΈΠ»ΠΈ ΠΊΠΎΠ΄ Π²Π΅Ρ€Ρ…Π½Π΅Π³ΠΎ уровня, Π³Π΄Π΅ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΠ΅Ρ€Π΅Ρ…Π²Π°Ρ‚Ρ‹Π²Π°Ρ‚ΡŒ всС Ρ‚ΠΈΠΏΡ‹ ошибок (для Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΡ€Π΅Π΄ΠΎΡ‚Π²Ρ€Π°Ρ‚ΠΈΡ‚ΡŒ ΠΈΡ… ΠΎΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΎΠΌ интСрфСйсС, ΠΈΠ»ΠΈ Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠΈΡ‚ΡŒ ΠΊΠ°ΠΊΡƒΡŽ-Ρ‚ΠΎ ΠΏΠ°ΠΊΠ΅Ρ‚Π½ΡƒΡŽ Π·Π°Π΄Π°Ρ‡Ρƒ).

Π€ΠΈΠ½Π°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€Ρ‹

Π§Ρ‚ΠΎ это: Π€ΠΈΠ½Π°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€Ρ‹ β€” это способ Π·Π°ΠΏΡƒΡΠΊΠ°Ρ‚ΡŒ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½Ρ‹ΠΉ ΠΊΠΎΠ΄ ΠΏΠ΅Ρ€Π΅Π΄ Ρ‚Π΅ΠΌ ΠΊΠ°ΠΊ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ собираСтся сборщиком мусора.
Π—Π°: ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ ΠΏΠΎΠ»Π΅Π·Π½Ρ‹ ΠΏΡ€ΠΈ очисткС, Π² особСнности Π²Π½Π΅ΡˆΠ½ΠΈΡ… рСсурсов.
ΠŸΡ€ΠΎΡ‚ΠΈΠ²: Π½Π΅Ρ‚ Π½ΠΈΠΊΠ°ΠΊΠΈΡ… Π³Π°Ρ€Π°Π½Ρ‚ΠΈΠΉ Ρ‚ΠΎΠ³ΠΎ, ΠΊΠΎΠ³Π΄Π° Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½ Ρ„ΠΈΠ½Π°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€, ΠΈ, Π²ΠΎΠΎΠ±Ρ‰Π΅, Π±ΡƒΠ΄Π΅Ρ‚ Π»ΠΈ ΠΎΠ½ Π²Ρ‹Π·Π²Π°Π½.

РСшСниС: ΠœΡ‹ Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ Ρ„ΠΈΠ½Π°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€Ρ‹. Π’ Π±ΠΎΠ»ΡŒΡˆΠΈΠ½ΡΡ‚Π²Π΅ случаСв, всё Ρ‚ΠΎ, Ρ‡Ρ‚ΠΎ Π²Π°ΠΌ Π½ΡƒΠΆΠ½ΠΎ ΠΎΡ‚ Ρ„ΠΈΠ½Π°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€Π°, Π²Ρ‹ смоТСтС ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ. Если Π²Π°ΠΌ Π΄Π΅ΠΉΡΡ‚Π²ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ Π½ΡƒΠΆΠ΅Π½ Ρ„ΠΈΠ½Π°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€, Ρ‚ΠΎ ΠΎΠ±ΡŠΡΠ²ΠΈΡ‚Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄ close() ΠΈ Π·Π°Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ΠΈΡ€ΡƒΠΉΡ‚Π΅, ΠΊΠΎΠ³Π΄Π° ΠΎΠ½ Ρ‚ΠΎΡ‡Π½ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒΡΡ.

Π˜ΠΌΠΏΠΎΡ€Ρ‚Ρ‹
Π“Ρ€ΡƒΠΏΠΏΠΎΠ²ΠΎΠΉ символ Π² ΠΈΠΌΠΏΠΎΡ€Ρ‚Π°Ρ…

Π§Ρ‚ΠΎ это: Когда Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ класс Bar ΠΈΠ· ΠΏΠ°ΠΊΠ΅Ρ‚Π° foo, Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ Π΄Π²Π° способа ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ это:

Π—Π° #1: ΠŸΠΎΡ‚Π΅Π½Ρ†ΠΈΠ°Π»ΡŒΠ½ΠΎ ΡƒΠΌΠ΅Π½ΡŒΡˆΠ°Π΅Ρ‚ количСство Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Ρ… ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠ² ΠΈΠΌΠΏΠΎΡ€Ρ‚Π°.
Π—Π° #2: Π”Π΅Π»Π°Π΅Ρ‚ явным Ρ‚ΠΎ, ΠΊΠ°ΠΊΠΎΠΉ класс Π½Π° самом Π΄Π΅Π»Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ. Π”Π΅Π»Π°Π΅Ρ‚ ΠΊΠΎΠ΄ Π±ΠΎΠ»Π΅Π΅ ΡƒΠ΄ΠΎΠ±ΠΎΡ‡ΠΈΡ‚Π°Π΅ΠΌΡ‹ΠΌ для Ρ‚Π΅Ρ…, ΠΊΡ‚ΠΎ Π΅Π³ΠΎ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚.

РСшСниС: Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ ΡΡ‚ΠΈΠ»ΡŒ #2 для ΠΈΠΌΠΏΠΎΡ€Ρ‚Π° любого Android ΠΊΠΎΠ΄Π°. Π―Π²Π½ΠΎΠ΅ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ дСлаСтся для стандартных Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊ (java.util.*, java.io.*, ΠΈ Ρ‚.ΠΏ) ΠΈ для ΠΊΠΎΠ΄Π° ΠΌΠΎΠ΄ΡƒΠ»ΡŒΠ½ΠΎΠ³ΠΎ тСстирования (junit.framework.*).

ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ/Javadoc

ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ Ρ„Π°ΠΉΠ» Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΠΌΠ΅Ρ‚ΡŒ объявлСниС ΠΎΠ± авторских ΠΏΡ€Π°Π²Π°Ρ… Π² самом Π½Π°Ρ‡Π°Π»Π΅. Π”Π°Π»Π΅Π΅ ΠΈΠ΄ΡƒΡ‚ объявлСния ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠ² package ΠΈ import, ΠΏΡ€ΠΈΡ‡Π΅ΠΌ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ Π±Π»ΠΎΠΊ раздСляСтся пустой строкой. Π—Π° Π½ΠΈΠΌΠΈ ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ объявлСния класса ΠΈΠ»ΠΈ интСрфСйса. ΠžΠΏΠΈΡˆΠΈΡ‚Π΅, Ρ‡Ρ‚ΠΎ Π΄Π΅Π»Π°Π΅Ρ‚ класс Π² Javadoc-коммСнтариях.

ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ класс ΠΈ Π½Π΅Ρ‚Ρ€ΠΈΠ²ΠΈΠ°Π»ΡŒΠ½Ρ‹ΠΉ public ΠΌΠ΅Ρ‚ΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒ Javadoc, ΠΏΠΎ ΠΊΡ€Π°ΠΉΠ½Π΅ΠΉ ΠΌΠ΅Ρ€Π΅ с ΠΎΠ΄Π½ΠΎΠΉ Ρ„Ρ€Π°Π·ΠΎΠΉ, ΠΎΠΏΠΈΡΡ‹Π²Π°ΡŽΡ‰Π΅ΠΉ Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π΄Π΅Π»Π°Π΅Ρ‚. Π€Ρ€Π°Π·Π° Π΄ΠΎΠ»ΠΆΠ½Π° Π½Π°Ρ‡ΠΈΠ½Π°Ρ‚ΡŒΡΡ с ΠΎΠΏΠΈΡΠ°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ³ΠΎ Π³Π»Π°Π³ΠΎΠ»Π° 3-Π³ΠΎ Π»ΠΈΡ†Π°. ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹:

Π’Π°ΠΌ Π½Π΅ Π½ΡƒΠΆΠ½ΠΎ ΠΎΠΏΠΈΡΡ‹Π²Π°Ρ‚ΡŒ Javadoc для Ρ‚Ρ€ΠΈΠ²ΠΈΠ°Π»ΡŒΠ½Ρ‹Ρ… get ΠΈ set ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ², Ρ‚Π°ΠΊΠΈΡ… ΠΊΠ°ΠΊ setFoo(), Ссли ваш Javadoc Π³ΠΎΠ²ΠΎΡ€ΠΈΡ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Β«sets FooΒ». Если ΠΌΠ΅Ρ‚ΠΎΠ΄ Π΄Π΅Π»Π°Π΅Ρ‚ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ Π±ΠΎΠ»Π΅Π΅ слоТноС (Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, соблюдСниС Π½Π΅ΠΊΠΈΡ… ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠΉ, ΠΈΠ»ΠΈ Ссли Π΅Π³ΠΎ дСйствия ΠΈΠΌΠ΅ΡŽΡ‚ Π²Π°ΠΆΠ½Ρ‹ΠΉ эффСкт Π²Π½Π΅ Π΅Π³ΠΎ самого), Ρ‚ΠΎΠ³Π΄Π° Π΅Π³ΠΎ ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ Π½ΡƒΠΆΠ½ΠΎ Π·Π°Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ. И Ссли это Π½Π΅ просто объяснСниС Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ Foo, Ρ‚ΠΎ Π²Π°ΠΌ Ρ‚Π°ΠΊΠΆΠ΅ слСдуСт Π΅Π³ΠΎ Π·Π°Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ.

Π’ΠΎΠΎΠ±Ρ‰Π΅, любой ΠΌΠ΅Ρ‚ΠΎΠ΄, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Ρ‹ написали ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ ΠΏΠΎΠ»ΡŒΠ·Ρƒ ΠΎΡ‚ Javadoc, Π½Π΅Π²Π°ΠΆΠ½ΠΎ public ΠΎΠ½ ΠΈΠ»ΠΈ Π½Π΅Ρ‚. Public ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ ΡΠ²Π»ΡΡŽΡ‚ΡΡ Ρ‡Π°ΡΡ‚ΡŒΡŽ API, ΠΈ поэтому ΠΎΠ½ΠΈ Ρ‚Ρ€Π΅Π±ΡƒΡŽΡ‚ описания Π² Javadoc.

Для написания Javadoc’ΠΎΠ² Π²Π°ΠΌ слСдуСт ΠΏΡ€ΠΈΠ΄Π΅Ρ€ΠΆΠΈΠ²Π°Ρ‚ΡŒΡΡ Sun Javadoc conventions.

ΠšΠΎΡ€ΠΎΡ‚ΠΊΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹

ΠœΠ΅Ρ‚ΠΎΠ΄Ρ‹ Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ нСбольшими ΠΈ Ρ€Π΅ΡˆΠ°ΡŽΡ‰ΠΈΠΌΠΈ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΡƒΡŽ Π·Π°Π΄Π°Ρ‡Ρƒ Π½Π°ΡΡ‚ΠΎΠ»ΡŒΠΊΠΎ, насколько это Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. Однако, понятно, Ρ‡Ρ‚ΠΎ ΠΈΠ½ΠΎΠ³Π΄Π° большиС ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ Π±Ρ‹Π²Π°ΡŽΡ‚ цСлСсообразны, Ρ‚Π°ΠΊ Ρ‡Ρ‚ΠΎ Π½Π΅Ρ‚ строгого ограничСния Π½Π° Π΄Π»ΠΈΠ½Ρƒ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°. Если ΠΌΠ΅Ρ‚ΠΎΠ΄ ΠΏΡ€Π΅Π²Ρ‹ΡˆΠ°Π΅Ρ‚ 40 строк, Ρ‚ΠΎ Π²Π°ΠΌ, Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, стоит ΠΏΠΎΠ΄ΡƒΠΌΠ°Ρ‚ΡŒ ΠΎ Ρ‚ΠΎΠΌ, ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈ Π΅Π³ΠΎ Ρ€Π°Π·Π±ΠΈΡ‚ΡŒ Π½Π° части, Π½Π΅ Π½Π°Ρ€ΡƒΡˆΠΈΠ² структуры ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹.

Π›ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹Π΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅

ΠžΠ±Π»Π°ΡΡ‚ΡŒ видимости Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠ²ΠΎΠ΄ΠΈΡ‚ΡŒΡΡ ΠΊ ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌΡƒ. ДСлая это, Π²Ρ‹ ΡƒΠ»ΡƒΡ‡ΡˆΠ°Π΅Ρ‚Π΅ Ρ‡ΠΈΡ‚Π°Π΅ΠΌΠΎΡΡ‚ΡŒ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅ΠΌΠΎΡΡ‚ΡŒ ΠΊΠΎΠ΄Π°, Π° Ρ‚Π°ΠΊΠΆΠ΅ ΡƒΠΌΠ΅Π½ΡŒΡˆΠ°Π΅Ρ‚Π΅ Π²Π΅Ρ€ΠΎΡΡ‚Π½ΠΎΡΡ‚ΡŒ ошибок. КаТдая пСрСмСнная Π΄ΠΎΠ»ΠΆΠ½Π° ΠΎΠ±ΡŠΡΠ²Π»ΡΡ‚ΡŒΡΡ Π² самом Π³Π»ΡƒΠ±ΠΎΠΊΠΎΠΌ Π±Π»ΠΎΠΊΠ΅, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΎΠΊΡ€ΡƒΠΆΠ°Π΅Ρ‚ всС Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Π΅ мСста использования ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ.

Π›ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹Π΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΎΠ±ΡŠΡΠ²Π»ΡΡ‚ΡŒΡΡ Π² Ρ‚ΠΎΠΌ мСстС, Π³Π΄Π΅ Π²ΠΏΠ΅Ρ€Π²Ρ‹Π΅ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Π΅Ρ‘ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ. ΠŸΠΎΡ‡Ρ‚ΠΈ каТдая локальная пСрСмСнная нуТдаСтся Π² ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€Π΅. Если Π²Ρ‹ Π΅Ρ‰Π΅ Π½Π΅ Π·Π½Π°Π΅Ρ‚Π΅, ΠΊΠ°ΠΊ Ρ‚ΠΎΡ‡Π½ΠΎ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ, Ρ‚ΠΎ Π²Π°ΠΌ слСдуСт ΠΎΡ‚Π»ΠΎΠΆΠΈΡ‚ΡŒ Π΅Ρ‘ объявлСниС, ΠΏΠΎΠΊΠ° Π²Ρ‹ это Π½Π΅ ΡƒΠ·Π½Π°Π΅Ρ‚Π΅.

БущСствуСт ΠΎΠ΄Π½ΠΎ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅, ΠΊΠ°ΡΠ°Ρ‚Π΅Π»ΡŒΠ½ΠΎ Π±Π»ΠΎΠΊΠ° try-catch. Если пСрСмСнная инициализируСтся ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° return ΠΌΠ΅Ρ‚ΠΎΠ΄Π°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ выбрасываСт провСряСмоС ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅, Ρ‚ΠΎ ΠΎΠ½Π° Π΄ΠΎΠ»ΠΆΠ½Π° ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒΡΡ Π² Π±Π»ΠΎΠΊΠ΅ try. Если ΠΆΠ΅ пСрСмСнная Π΄ΠΎΠ»ΠΆΠ½Π° ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒΡΡ Π²Π½Π΅ Π±Π»ΠΎΠΊΠ° try, Ρ‚ΠΎΠ³Π΄Π° ΠΎΠ½Π° ΠΎΠ±ΡŠΡΠ²Π»ΡΠ΅Ρ‚ΡΡ ΠΏΠ΅Ρ€Π΅Π΄ Π½ΠΈΠΌ, Π½Π΅Π²Π°ΠΆΠ½ΠΎ, Π·Π½Π°Π΅Ρ‚Π΅ Π»ΠΈ Π²Ρ‹ ΠΊΠ°ΠΊ Π΅Ρ‘ Ρ‚ΠΎΡ‡Π½ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ:

Но Π΄Π°ΠΆΠ΅ этот случай ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠ±ΠΎΠΉΡ‚ΠΈ ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ инкапсуляции Π±Π»ΠΎΠΊΠ° try-catch Π² ΠΌΠ΅Ρ‚ΠΎΠ΄Π΅.

ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ Π² Ρ†ΠΈΠΊΠ»Π°Ρ… Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΎΠ±ΡŠΡΠ²Π»ΡΡ‚ΡŒΡΡ Π²Π½ΡƒΡ‚Ρ€ΠΈ самого ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π°, Ссли Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π½Π΅Ρ‚ Π½Π΅ΠΏΡ€Π΅ΠΎΠ΄ΠΎΠ»ΠΈΠΌΠΎΠΉ ΠΏΡ€ΠΈΡ‡ΠΈΠ½Ρ‹ этого Π½Π΅ Π΄Π΅Π»Π°Ρ‚ΡŒ.

Π˜ΠΌΠΏΠΎΡ€Ρ‚Ρ‹

ΠžΡ‚ΡΡ‚ΡƒΠΏΡ‹

ΠΌΡ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ 4 ΠΏΡ€ΠΎΠ±Π΅Π»Π° для Π±Π»ΠΎΠΊΠΎΠ². ΠœΡ‹ Π½ΠΈΠΊΠΎΠ³Π΄Π° Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ Ρ‚Π°Π±ΡƒΠ»ΡΡ†ΠΈΡŽ. ΠœΡ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ 8 ΠΏΡ€ΠΎΠ±Π΅Π»ΠΎΠ² для пСрСноса строк, Π²ΠΊΠ»ΡŽΡ‡Π°Ρ Π²Ρ‹Π·ΠΎΠ²Ρ‹ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ ΠΈ присваивания, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ Ρ‚Π°ΠΊ:

Названия полСй

Π€ΠΈΠ³ΡƒΡ€Π½Ρ‹Π΅ скобки

Для ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°ΡŽΡ‰ΠΈΡ… Ρ„ΠΈΠ³ΡƒΡ€Π½Ρ‹Π΅ скобок Π½Π΅ выдСляСтся ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½Π°Ρ строка, ΠΎΠ½ΠΈ находятся Π² Ρ‚ΠΎΠΉ ΠΆΠ΅ строкС, Ρ‡Ρ‚ΠΎ ΠΈ ΠΊΠΎΠ΄ ΠΏΠ΅Ρ€Π΅Π΄ Π½ΠΈΠΌΠΈ:

ΠœΡ‹ Ρ‚Ρ€Π΅Π±ΡƒΠ΅ΠΌ Ρ„ΠΈΠ³ΡƒΡ€Π½Ρ‹Π΅ скобки для ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° условия. Π˜ΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ΠΌ являСтся, ΠΊΠΎΠ³Π΄Π° ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ условия ΠΈ Π΅Π³ΠΎ Ρ‚Π΅Π»ΠΎ ΠΏΠΎΠΌΠ΅Ρ‰Π°ΡŽΡ‚ΡΡ Π² ΠΎΠ΄Π½Ρƒ строку. Π’ΠΎ Π΅ΡΡ‚ΡŒ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΈΡΠ°Ρ‚ΡŒ Ρ‚Π°ΠΊ:

Π”Π»ΠΈΠ½Π° строки

КаТдая строка тСкста Π² ΠΊΠΎΠ΄Π΅ Π΄ΠΎΠ»ΠΆΠ½Π° Π±Ρ‹Ρ‚ΡŒ Π½Π΅ Π΄Π»ΠΈΠ½Π½Π΅Π΅ 100 символов.
Π˜ΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅: Ссли ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ содСрТит ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠΌΠ°Π½Π΄, ΠΈΠ»ΠΈ URL (ΡƒΠ΄ΠΎΠ±Π½Π΅Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ copy/paste).
Π˜ΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅: строки ΠΈΠΌΠΏΠΎΡ€Ρ‚Π° ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ Π΄Π»ΠΈΠ½Π½Π΅Π΅ 100 символов, Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ люди Ρ€Π΅Π΄ΠΊΠΎ Π½Π° Π½ΠΈΡ… смотрят. Π’Π°ΠΊΠΆΠ΅ это ΡƒΠΏΡ€ΠΎΡ‰Π°Π΅Ρ‚ написаниС инструмСнтов.

БокращСния Π² ΠΈΠΌΠ΅Π½Π°Ρ…

РассматривайтС сокращСния ΠΈ Π°Π±Π±Ρ€Π΅Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρ‹ ΠΊΠ°ΠΊ слова. ИмСна Π±ΠΎΠ»Π΅Π΅ ΡƒΠ΄ΠΎΠ±ΠΎΡ‡ΠΈΡ‚Π°Π΅ΠΌΡ‹:

Π₯ΠΎΡ€ΠΎΡˆΠΎΠŸΠ»ΠΎΡ…ΠΎ
XmlHttpRequestXMLHTTPRequest
getCustomerIdgetCustomerID

Π­Ρ‚ΠΎΡ‚ ΡΡ‚ΠΈΠ»ΡŒ Ρ‚Π°ΠΊΠΆΠ΅ примСняСтся, ΠΊΠΎΠ³Π΄Π° сокращСниС ΠΈ Π°Π±Π±Ρ€Π΅Π²ΠΈΠ°Ρ‚ΡƒΡ€Π° β€” это ΠΏΠΎΠ»Π½ΠΎΠ΅ имя:

Π₯ΠΎΡ€ΠΎΡˆΠΎΠŸΠ»ΠΎΡ…ΠΎ
class Htmlclass HTML
String url;String URL;
long id;long ID;

Π‘Ρ‚ΠΈΠ»ΡŒ TODO

Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ TODO для ΠΊΠΎΠ΄Π°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ являСтся Π²Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹ΠΌ, краткосрочным, ΠΈΠ»ΠΈ Ρ…ΠΎΡ€ΠΎΡˆΠΈΠΌ, Π½ΠΎ Π½Π΅ ΠΈΠ΄Π΅Π°Π»ΡŒΠ½Ρ‹ΠΌ. ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΠΊΠ»ΡŽΡ‡Π°Ρ‚ΡŒ Π² сСбя Β«TODO:Β», Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€:

Если ваш ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ ΠΈΠΌΠ΅Π΅Ρ‚ Π²ΠΈΠ΄ Β«Π’ Π±ΡƒΠ΄ΡƒΡ‰Π΅ΠΌ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎΒ», Ρ‚ΠΎ ΡƒΠ±Π΅Π΄ΠΈΡ‚Π΅ΡΡŒ, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π²ΠΊΠ»ΡŽΡ‡Π°Π΅Ρ‚ Π² сСбя ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΡƒΡŽ Π΄Π°Ρ‚Ρƒ (1 января 2011 Π³ΠΎΠ΄Π°), ΠΈΠ»ΠΈ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΠΎΠ΅ событиС Β«Π£Π΄Π°Π»ΠΈΡ‚ΡŒ послС Π²Ρ‹Ρ…ΠΎΠ΄Π° вСрсии 2.1Β».

Π‘ΠΎΠ³Π»Π°ΡΠΎΠ²Π°Π½Π½ΠΎΡΡ‚ΡŒ

Если Π²Ρ‹ измСняСтС ΠΊΠΎΠ΄, Ρ‚ΠΎ ΠΏΠΎΡ‚Ρ€Π°Ρ‚ΡŒΡ‚Π΅ ΠΌΠΈΠ½ΡƒΡ‚Ρƒ Π½Π° Ρ‚ΠΎ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΡΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ Π½Π° ΠΊΠΎΠ΄ Π²ΠΎΠΊΡ€ΡƒΠ³ вас ΠΈ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Π΅Π³ΠΎ ΡΡ‚ΠΈΠ»ΡŒ. Если Π² Π½Π΅ΠΌ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‚ΡΡ ΠΏΡ€ΠΎΠ±Π΅Π»Ρ‹, Ρ‚ΠΎ ΠΈ Π²Π°ΠΌ слСдуСт ΠΈΡ… ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ. Если ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ содСрТат нСбольшой Π½Π°Π±ΠΎΡ€ Π·Π²Π΅Π·Π΄ΠΎΡ‡Π΅ΠΊ, Ρ‚ΠΎ ΠΈ Π²Π°ΠΌ слСдуСт ΠΈΡ… ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ.

Π’Π΅ΡΡŒ смысл Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΉ ΠΊ ΡΡ‚ΠΈΠ»ΡŽ ΠΊΠΎΠ΄Π° Π² создании ΠΎΠ±Ρ‰Π΅ΠΉ лСксики, Ρ‡Ρ‚ΠΎΠ±Ρ‹ люди ΠΊΠΎΠ½Ρ†Π΅Π½Ρ‚Ρ€ΠΈΡ€ΠΎΠ²Π°Π»ΠΈΡΡŒ Π½Π° Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ ΠΎΠ½ΠΈ говорят, вмСсто Ρ‚ΠΎΠ³ΠΎ ΠΊΠ°ΠΊ ΠΎΠ½ΠΈ говорят. ΠœΡ‹ прСдставляСм Π³Π»ΠΎΠ±Π°Π»ΡŒΠ½Ρ‹Π΅ ΠΏΡ€Π°Π²ΠΈΠ»Π° стиля, Ρ‡Ρ‚ΠΎΠ±Ρ‹ люди Π·Π½Π°Π»ΠΈ эту лСксику. Но Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ Ρ‚Π°ΠΊΠΆΠ΅ Π²Π°ΠΆΠ΅Π½. Если ΠΊΠΎΠ΄, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Ρ‹ добавляСтС Π² Ρ„Π°ΠΉΠ» выглядит Ρ€Π΅Π·ΠΊΠΎ ΠΎΡ‚Π»ΠΈΡ‡Π½Ρ‹ΠΌ ΠΎΡ‚ Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ Π±Ρ‹Π», Ρ‚ΠΎ это выбросит Π±ΡƒΠ΄ΡƒΡ‰Π΅Π³ΠΎ читатСля ΠΈΠ· Π΅Π³ΠΎ Ρ€ΠΈΡ‚ΠΌΠ° ΠΈ Π±ΡƒΠ΄Π΅Ρ‚ ΠΌΠ΅ΡˆΠ°Ρ‚ΡŒ Π΅ΠΌΡƒ ΠΏΠΎΠ½ΠΈΠΌΠ°Ρ‚ΡŒ структуру. Π‘Ρ‚Π°Ρ€Π°ΠΉΡ‚Π΅ΡΡŒ ΠΈΠ·Π±Π΅Π³Π°Ρ‚ΡŒ этого.

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Java Code Style: ΠΊΠ°ΠΊ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ ΠΎΡ„ΠΎΡ€ΠΌΠ»ΡΡ‚ΡŒ ΠΊΠΎΠ΄ Java

Π‘Ρ‚Π°Ρ‚ΡŒΡ ΠΎΠ± ΠΈΠΌΠ΅Π½Π°Ρ… ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ…, классов ΠΈ ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ², скобках, коммСнтариях, Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠΈ ΠΊΠΎΠ΄Π° ΠΈ Π΄Ρ€ΡƒΠ³ΠΈΡ… ΠΏΡ€Π°Π²ΠΈΠ»Π°Ρ… Ρ…ΠΎΡ€ΠΎΡˆΠ΅Π³ΠΎ Ρ‚ΠΎΠ½Π°, принятых Π² языкС JavΠ°.

Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π‘ΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ Ρ„ΠΎΡ‚ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π‘ΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. ΠšΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° ΠΏΡ€ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π€ΠΎΡ‚ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions

Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π‘ΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ Ρ„ΠΎΡ‚ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π‘ΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. ΠšΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° ΠΏΡ€ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π€ΠΎΡ‚ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions

Π’ языкС Java, ΠΊΠ°ΠΊ ΠΈ Π²ΠΎ ΠΌΠ½ΠΎΠ³ΠΈΡ… Π΄Ρ€ΡƒΠ³ΠΈΡ… языках программирования, Π΅ΡΡ‚ΡŒ Π½Π΅ΠΎΡ„ΠΈΡ†ΠΈΠ°Π»ΡŒΠ½Ρ‹Π΅, Π½ΠΎ принятыС сообщСством Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠ² соглашСния ΠΏΠΎ ΠΎΡ„ΠΎΡ€ΠΌΠ»Π΅Π½ΠΈΡŽ ΠΊΠΎΠ΄Π°. Π”Π°Π²Π°ΠΉΡ‚Π΅ ΠΏΠΎΠΏΡ€ΠΎΠ±ΡƒΠ΅ΠΌ Ρ€Π°Π·ΠΎΠ±Ρ€Π°Ρ‚ΡŒΡΡ, Ρ‡Ρ‚ΠΎ это Ρ‚Π°ΠΊΠΎΠ΅, ΠΊΠ°ΠΊ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ ΠΈ ΠΏΠΎΡ‡Π΅ΠΌΡƒ Π²Π°ΠΆΠ½ΠΎ.

Π—Π°Ρ‡Π΅ΠΌ Π½ΡƒΠΆΠ΅Π½ Π΅Π΄ΠΈΠ½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ ΠΊΠΎΠ΄Π°

Java Code Style β€” это Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΈ ΠΈ соглашСния ΠΎ стилС ΠΊΠΎΠ΄Π°, собранныС вмСстС. НапримСр:

РСгламСнтируСтся мноТСство Π²Π΅Ρ‰Π΅ΠΉ, связанных с ΠΎΡ„ΠΎΡ€ΠΌΠ»Π΅Π½ΠΈΠ΅ΠΌ исходного тСкста ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌ, ΠΈ приводятся трСбования, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ ΡƒΡ‡ΠΈΡ‚Ρ‹Π²Π°Ρ‚ΡŒ. ΠŸΠΎΡ‡Π΅ΠΌΡƒ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΈ эти стандарты?

ΠŸΡ€ΠΈΡ‡ΠΈΠ½Ρ‹, ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ ΠΏΡ€ΠΈΡˆΠ»ΠΈ ΠΊ Ρ‚Π°ΠΊΠΈΠΌ соглашСниям, Π»ΠΎΠ³ΠΈΡ‡Π½Ρ‹ ΠΈ просты:

ИспользованиС общСпринятых соглашСний ΠΏΠΎ ΠΎΡ„ΠΎΡ€ΠΌΠ»Π΅Π½ΠΈΡŽ позволяСт ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ ΠΊΠΎΠ΄ Π±ΠΎΠ»Π΅Π΅ Π°ΠΊΠΊΡƒΡ€Π°Ρ‚Π½Ρ‹ΠΌ, ΠΈΠ·Π±Π΅ΠΆΠ°Ρ‚ΡŒ ошибок, связанных с Ρ€Π°Π·Π½Ρ‹ΠΌΠΈ стилями написания Ρƒ людСй, Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‰ΠΈΡ… Π½Π°Π΄ ΠΎΠ΄Π½ΠΈΠΌ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚ΠΎΠΌ.

Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π‘ΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ Ρ„ΠΎΡ‚ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π‘ΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. ΠšΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° ΠΏΡ€ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions. Π€ΠΎΡ‚ΠΎ Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions

ΠŸΡ€Π΅ΠΏΠΎΠ΄Π°Π²Π°Ρ‚Π΅Π»ΡŒ Skillbox. ΠŸΠΈΡˆΠ΅Ρ‚ ΠΏΡ€ΠΎ Java, ΡƒΡ‡ΠΈΡ‚ Go. ΠŸΠΎΠΌΠ½ΠΈΡ‚ рассвСт PHP ΠΈ ΠΊΠ°ΠΊ Β«Π³Ρ€Π°Π±ΠΈΡ‚ΡŒ ΠΊΠΎΡ€ΠΎΠ²Π°Π½Ρ‹Β».

Π€ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ Π½Π° ΠΏΡ€Π°ΠΊΡ‚ΠΈΠΊΠ΅

Π‘Ρ‚ΠΈΠ»ΡŒ Π½Π΅ ΠΈΠ³Ρ€Π°Π΅Ρ‚ Ρ€ΠΎΠ»ΠΈ для ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π°, Π½ΠΎ Π²Π°ΠΆΠ΅Π½ для чтСния ΠΊΠΎΠ΄Π° Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠΎΠΌ. НашС Π·Ρ€Π΅Π½ΠΈΠ΅ устроСно Ρ‚Π°ΠΊ, Ρ‡Ρ‚ΠΎ спСрва ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ ΠΈ Π°Π½Π°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΎΠ±Ρ€Π°Π·Ρ‹, Π° Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π·Π°Ρ‚Π΅ΠΌ ΠΌΡ‹ вдаёмся Π² Π΄Π΅Ρ‚Π°Π»ΠΈ. А Ссли Π±Π»ΠΎΠΊΠΈ исходного тСкста ΡƒΠ½ΠΈΡ„ΠΈΡ†ΠΈΡ€ΠΎΠ²Π°Π½Ρ‹, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΠΎΠ½ΠΈΠΌΠ°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ ΠΏΠ΅Ρ€Π΅Π΄ Π½ΠΈΠΌ Π·Π° конструкция, Π΄Π°ΠΆΠ΅ Π½Π΅ вникая Π² сам ΠΊΠΎΠ΄.

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Π§Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ java code conventions

Java Code Conventions

Why Have Code Conventions?

Code conventions are important to programmers for a number of reasons:

80% of the lifetime cost of a piece of software goes to maintenance.

Hardly any software is maintained for its whole life by the original author.

Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

Static Code Analysis

Setting up Checkstyle

Checkstyle will help educate and enforce our coding standards. You can set up your IDE to use Checkstyle to examine code for conformance to the standards. Learn more about the checks or Google the error message to find out why it complains about certain things.

Install the CheckStyle-IDEA plugin. File | Preferences | Plugins | Browse Repositories. If nothing shows up in the list you may need to set the Http Proxy Settings. I had to set mine to manual: http://us-auto.proxy.lexmark.com:8080. Search for CheckStyle-IDEA and install it.

Set the configuration file. In File | Preferences you will now have CheckStyle settings. Click plus sign to add the configuration file. Set the configuration file to http://lexmarkweb.github.io/coding-standards/java-checks-6.1.1.xml (for IntelliJ 13)

Make this the active configuration.

Check your code. With you code open in IntelliJ right click and you will see «Check Current File».

A file consists of sections that should be separated by blank lines and an optional comment identifying each section.

Source files are encoded in UTF-8

Files longer than 2000 lines are cumbersome and should be avoided.

Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.

Java source files have the following ordering:

All source files should begin with a c-style comment that lists the programmer(s), the date, a copyright notice, and also a brief description of the purpose of the program.

The first non-comment line of most Java source files is a package statement. After that, import statements can follow.

Import statements must be grouped with associated packages together and one blank line between groups

Imported classes should always be listed explicitly

Four spaces should be used as the unit of indentation. The exact construction of the indentation is 4 spaces.

Special characters like TAB and page break should be avoided

Avoid lines longer than 120 characters, since they’re not handled well by many terminals and tools.

When an expression will not fit on a single line, break it according to these general principles:

β€’ Break after a comma.

β€’ Break before an operator.

β€’ For arithmetic expressions, avoid breaking within a set of parentheses.

β€’ Align the new line with the beginning of the expression at the same level on the previous line.

β€’ If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.

Here are some examples of breaking method calls:

Following are two examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level.

Following are two examples of indenting method declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents only 8 spaces.

Here are three acceptable ways to format ternary expressions:

Java programs can have two kinds of comments: implementation comments and documentation comments. Implementation comments are those found in C++, which are delimited by /. /, and //. Documentation comments (known as «doc comments») are

Implementation comments are meant for commenting out code or for comments about the particular implementation. Doc comments are meant to describe the specification of the code, from an implementation-free perspective to be read by developers who might not necessarily have the source code at hand.

Comments should be used to give overviews of code and provide additional information that is not readily available in the code itself. Comments should contain only information that is relevant to reading and understanding the program. For example, information about how the corresponding package is built or in what directory it resides should not be included as a comment.

Avoid duplicating information that is present in (and clear from) the code. It is too easy for redundant comments to get out of date. In general, avoid any comments that are likely to get out of date as the code evolves.

Note: The frequency of comments sometimes reflects poor quality of code. When you feel compelled to add a comment, consider rewriting the code to make it clearer.

Comments should not be enclosed in large boxes drawn with asterisks or other characters.

Comments should never include special characters such as form-feed and backspace.

Implementation Comment Formats

Programs can have four styles of implementation comments: block, single-line, trailing and end-of-line.

Block comments are used to provide descriptions of files, methods, data structures and algorithms.

Block comments should be used at the beginning of each file and before each method. They can also be used in other places, such as within methods.

Block comments inside a function or method should be indented to the same level as the code they describe.

A block comment should be preceded by a blank line to set it apart from the rest of the code.

Block comments have an asterisk » * » at the beginning of each line except the first.

Short comments can appear on a single line indented to the level of the code that follows. If a comment can’t be written in a single line, it should follow the block comment format. A single-line comment should be preceded by a blank line. Here’s an example of a single-line comment in Java code

Notice that top-level classes and interfaces are not indented, while their members are. The first line of doc comment ( /** ) for classes and interfaces is not indented; subsequent doc comment lines each have 1 space of indentation (to vertically align the asterisks). Members, including constructors, have 4 spaces for the first doc comment line and 5 spaces thereafter.

If you need to give information about a class, interface, variable, or method that isn’t appropriate for documentation, use an implementation block comment or single-line comment immediately after the declaration. For example, details about the implementation of a class should go in in such an implementation block comment following the class statement, not in the class doc comment.

Doc comments should not be positioned inside a method or constructor definition block, because Java associates documentation comments with the first declaration after the comment.

All classes are to include a comment block that describing the basic purpose of the class. (This is also a good place to put any overarching TODO statements).

All public methods need to include Java doc comments except for accessors.

Parameters are to be included, but do not require documentation unless it is something meaningful i.e. avoid * @param name The name

Return statements are to be included and documented.

Thrown exceptions may be included, but do not need to be documented.

Protected / Private methods should include java doc comments when they are not easily understood. This is up to developer / reviewer discretion.

One declaration per line. In other words,

Try to initialize local variables where they’re declared. The only reason not to initialize a variable where it’s declared is if the initial value depends on some computation occurring first.

Local variables are not habitually declared at the start of their containing block or block-like construct. Instead, local variables are declared close to the point they are first used (within reason), to minimize their scope. Local variable declarations typically have initializers, or are initialized immediately after declaration.

Avoid local declarations that hide declarations at higher levels. For example, do not declare the same variable name in an inner block:

Class and Interface Declarations

When coding Java classes and interfaces, the following formatting rules should be followed:

No space between a method name and the parenthesis «(» starting its parameter list

Closing brace «>» starts a line by itself indented to match its corresponding opening statement, except when it is a null statement the «>» should appear immediately after the «<"

Methods are separated by a blank line

Annotations applying to a class, method or constructor appear immediately after the documentation block, and each annotation is listed on a line of its own (that is, one annotation per line). These line breaks do not constitute line-wrapping so the indentation level is not increased

Each line should contain at most one statement. Example:

Compound statements are statements that contain lists of statements enclosed in braces «< statements >«. See the following sections for examples.

The enclosed statements should be indented one more level than the compound statement.

The opening brace should be at the end of the line that begins the compound statement; the closing brace should begin a line and be indented to the beginning of the compound statement.

Braces are used around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.

A return statement with a value should not use parentheses unless they make the return value more obvious in some way. Example:

if, if-else, if else-if else Statements

The if-else class of statements should have the following form:

Note: if statements always use braces, <>. Avoid the following error-prone form:

A for statement should have the following form:

An empty for statement (one in which all the work is done in the initialization, condition, and update clauses) should have the following form:

When using the comma operator in the initialization or update clause of a for statement, avoid the complexity of using more than three variables. If needed, use separate statements before the for loop (for the initialization clause) or at the end of the loop (for the update clause).

A while statement should have the following form:

An empty while statement should have the following form:

A do-while statement should have the following form:

A switch statement should have the following form:

Every time a case falls through (doesn’t include a break statement), add a comment where the break statement would normally be. This is shown in the preceding code example with the /* falls through */ comment.

Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added.

A try-catch statement should have the following format:

A try-catch statement may also be followed by finally, which executes regardless of whether or not the try block has completed successfully.

for collection loop

A for collection loop should have following format

Blank lines improve readability by setting off sections of code that are logically related.

Two blank lines should always be used in the following circumstances:

Between sections of a source file

Between class and interface definitions

One blank line should always be used in the following circumstances:

Between the local variables in a method and its first statement

Before a block or single-line comment

Between logical sections inside a method to improve readability

Blank spaces should be used in the following circumstances:

Note that a blank space should not be used between a method name and its opening parenthesis. This helps to distinguish keywords from method calls.

A blank space should appear after commas in argument lists.

Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it’s a constant, package, or class-which can be helpful in understanding the code.

Identifier TypeRules of NamingExamples
PackagesThe prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization’s own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

ClassesClass names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

Implementation class should have suffix Impl

class Raster; class ImageSprite;
InterfacesInterface names should be capitalized like class names.interface RasterDelegate; interface Storing;
MethodsMethods should be verbs, in Camel Case

The term compute can be used where something is computed

The term find can be used when we look up something

Is prefix is used for Boolean getter and setters

Acronyms such as XML and HTTP should be treated as words, such as getHttp(), getXml(). Not getXML() or getHTTP()

Variable names should be meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for loop control variables.

Common names for temporary variables are i, j, k, m, and n.

Boolean variable names should not be negative, for instance, isNotLoaded, unAttached.

int i; char c; float myWidth;
ConstantsThe names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores («`_`»).«` static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999; static final int GET_THE_CPU = 1; «`

Type conversions must always be done explicitly never rely on implicit type conversion

Array specifiers must be attached to the type not the variable

Providing Access to Instance and Class Variables

Don’t make any instance or class variable public without good reason. Just because it’s a class level variable doesn’t mean it needs a getter and setter.

One example of appropriate public instance variables is the case where the class is essentially a data structure, with no behavior

Referring to Class Variables and Methods

Avoid using an object to access a class (static) variable or method. Use a class name instead. For example:

String constants should be used except for «» or null, some exceptions would be logging and errors.

should be written as

It is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems. Even if the operator precedence seems clear to you, it might not be to others-you shouldn’t assume that other programmers know precedence as well as you do.

Try to make the structure of your program match the intent. Example:

should instead be written as

! operator and conditional expressions

@Override: always used

A method is marked with the @Override annotation whenever it is legal. This includes a class method overriding a superclass method, a class method implementing an interface method, and an interface method respecifying a superinterface method.

Exception:@Override may be omitted when the parent method is @Deprecated.

Caught exceptions: not ignored

Except as noted below, it is very rarely correct to do nothing in response to a caught exception. Exceptions should usually be at least logged at debug or trace level.

When it truly is appropriate to take no action whatsoever in a catch block, the reason this is justified is explained in a comment.

Exception: In tests, a caught exception may be ignored without comment if it is named expected. The following is a very common idiom for ensuring that the method under test does throw an exception of the expected type, so a comment is unnecessary here.

Each project/application should choose and enforce usage of a standard logging library (ex. Log4j)

Each project/application should document when each logging level should be used

All exceptions should at least be logged (excluding «expected» exceptions like some parsing exceptions)

Exceptions should be chained except for good reasons

When adding temporary logging for trouble shooting, add a // TODO remove temporary logs comment by set of log messages that should be removed.

Consult these resources for guidance on secure coding practices, particularly CERT standard. Some stylistic considerations are mentioned further on.

Exercise extreme caution with switch statements, especially fall-through statements. If you must use fall-through, please re-think your design approach. If, after much consideration, you still need fall-through, be judicious and deliberate to mark ALL fall-through points with a comment as indicated earlier in this guide.

Security checks should be located and maintained in a single place. Be sure to apply «Don’t Repeat Yourself» (DRY) principal frequently and comprehensively to all security check logic throughout the application.

When to Use Generics

With Collections. The Java Collections framework is excellent and should be used instead of Arrays.

If you find yourself doing a lot (or perhaps even a little) casting of types you might want to consider Generics

If you are using variables of type Object to store values form various classes, you might want to use Generics instead.

Naming Convention for Generic types in a class definition

Oracle recommends the following naming convention:

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ

Π’Π°Ρˆ адрСс email Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΠΏΡƒΠ±Π»ΠΈΠΊΠΎΠ²Π°Π½. ΠžΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ поля ΠΏΠΎΠΌΠ΅Ρ‡Π΅Π½Ρ‹ *