Π§ΡΠΎ ΡΠ°ΠΊΠΎΠ΅ 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 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ², ΡΠ°ΠΊ ΠΊΠ°ΠΊ Π»ΡΠ΄ΠΈ ΡΠ΅Π΄ΠΊΠΎ Π½Π° Π½ΠΈΡ
ΡΠΌΠΎΡΡΡΡ. Π’Π°ΠΊΠΆΠ΅ ΡΡΠΎ ΡΠΏΡΠΎΡΠ°Π΅Ρ Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΠΈΠ½ΡΡΡΡΠΌΠ΅Π½ΡΠΎΠ².
Π‘ΠΎΠΊΡΠ°ΡΠ΅Π½ΠΈΡ Π² ΠΈΠΌΠ΅Π½Π°Ρ
Π Π°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°ΠΉΡΠ΅ ΡΠΎΠΊΡΠ°ΡΠ΅Π½ΠΈΡ ΠΈ Π°Π±Π±ΡΠ΅Π²ΠΈΠ°ΡΡΡΡ ΠΊΠ°ΠΊ ΡΠ»ΠΎΠ²Π°. ΠΠΌΠ΅Π½Π° Π±ΠΎΠ»Π΅Π΅ ΡΠ΄ΠΎΠ±ΠΎΡΠΈΡΠ°Π΅ΠΌΡ:
Π₯ΠΎΡΠΎΡΠΎ | ΠΠ»ΠΎΡ ΠΎ |
XmlHttpRequest | XMLHTTPRequest |
getCustomerId | getCustomerID |
ΠΡΠΎΡ ΡΡΠΈΠ»Ρ ΡΠ°ΠΊΠΆΠ΅ ΠΏΡΠΈΠΌΠ΅Π½ΡΠ΅ΡΡΡ, ΠΊΠΎΠ³Π΄Π° ΡΠΎΠΊΡΠ°ΡΠ΅Π½ΠΈΠ΅ ΠΈ Π°Π±Π±ΡΠ΅Π²ΠΈΠ°ΡΡΡΠ° β ΡΡΠΎ ΠΏΠΎΠ»Π½ΠΎΠ΅ ΠΈΠΌΡ:
Π₯ΠΎΡΠΎΡΠΎ | ΠΠ»ΠΎΡ ΠΎ |
class Html | class HTML |
String url; | String URL; |
long id; | long ID; |
Π‘ΡΠΈΠ»Ρ TODO
ΠΡΠΏΠΎΠ»ΡΠ·ΡΠΉΡΠ΅ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ TODO Π΄Π»Ρ ΠΊΠΎΠ΄Π°, ΠΊΠΎΡΠΎΡΡΠΉ ΡΠ²Π»ΡΠ΅ΡΡΡ Π²ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΌ, ΠΊΡΠ°ΡΠΊΠΎΡΡΠΎΡΠ½ΡΠΌ, ΠΈΠ»ΠΈ Ρ ΠΎΡΠΎΡΠΈΠΌ, Π½ΠΎ Π½Π΅ ΠΈΠ΄Π΅Π°Π»ΡΠ½ΡΠΌ. ΠΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΉ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΠΊΠ»ΡΡΠ°ΡΡ Π² ΡΠ΅Π±Ρ Β«TODO:Β», Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ:
ΠΡΠ»ΠΈ Π²Π°Ρ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΉ ΠΈΠΌΠ΅Π΅Ρ Π²ΠΈΠ΄ Β«Π Π±ΡΠ΄ΡΡΠ΅ΠΌ ΡΠ΄Π΅Π»Π°ΡΡ ΡΡΠΎ-ΡΠΎΒ», ΡΠΎ ΡΠ±Π΅Π΄ΠΈΡΠ΅ΡΡ, ΡΡΠΎ ΠΎΠ½ Π²ΠΊΠ»ΡΡΠ°Π΅Ρ Π² ΡΠ΅Π±Ρ ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΡΡ Π΄Π°ΡΡ (1 ΡΠ½Π²Π°ΡΡ 2011 Π³ΠΎΠ΄Π°), ΠΈΠ»ΠΈ ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΠΎΠ΅ ΡΠΎΠ±ΡΡΠΈΠ΅ Β«Π£Π΄Π°Π»ΠΈΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΡ ΠΎΠ΄Π° Π²Π΅ΡΡΠΈΠΈ 2.1Β».
Π‘ΠΎΠ³Π»Π°ΡΠΎΠ²Π°Π½Π½ΠΎΡΡΡ
ΠΡΠ»ΠΈ Π²Ρ ΠΈΠ·ΠΌΠ΅Π½ΡΠ΅ΡΠ΅ ΠΊΠΎΠ΄, ΡΠΎ ΠΏΠΎΡΡΠ°ΡΡΡΠ΅ ΠΌΠΈΠ½ΡΡΡ Π½Π° ΡΠΎ, ΡΡΠΎΠ±Ρ ΠΏΠΎΡΠΌΠΎΡΡΠ΅ΡΡ Π½Π° ΠΊΠΎΠ΄ Π²ΠΎΠΊΡΡΠ³ Π²Π°Ρ ΠΈ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ Π΅Π³ΠΎ ΡΡΠΈΠ»Ρ. ΠΡΠ»ΠΈ Π² Π½Π΅ΠΌ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡΡΡΡ ΠΏΡΠΎΠ±Π΅Π»Ρ, ΡΠΎ ΠΈ Π²Π°ΠΌ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ. ΠΡΠ»ΠΈ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠ°Ρ Π½Π΅Π±ΠΎΠ»ΡΡΠΎΠΉ Π½Π°Π±ΠΎΡ Π·Π²Π΅Π·Π΄ΠΎΡΠ΅ΠΊ, ΡΠΎ ΠΈ Π²Π°ΠΌ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ.
ΠΠ΅ΡΡ ΡΠΌΡΡΠ» ΡΠ΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°ΡΠΈΠΉ ΠΊ ΡΡΠΈΠ»Ρ ΠΊΠΎΠ΄Π° Π² ΡΠΎΠ·Π΄Π°Π½ΠΈΠΈ ΠΎΠ±ΡΠ΅ΠΉ Π»Π΅ΠΊΡΠΈΠΊΠΈ, ΡΡΠΎΠ±Ρ Π»ΡΠ΄ΠΈ ΠΊΠΎΠ½ΡΠ΅Π½ΡΡΠΈΡΠΎΠ²Π°Π»ΠΈΡΡ Π½Π° ΡΠΎΠΌ, ΡΡΠΎ ΠΎΠ½ΠΈ Π³ΠΎΠ²ΠΎΡΡΡ, Π²ΠΌΠ΅ΡΡΠΎ ΡΠΎΠ³ΠΎ ΠΊΠ°ΠΊ ΠΎΠ½ΠΈ Π³ΠΎΠ²ΠΎΡΡΡ. ΠΡ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»ΡΠ΅ΠΌ Π³Π»ΠΎΠ±Π°Π»ΡΠ½ΡΠ΅ ΠΏΡΠ°Π²ΠΈΠ»Π° ΡΡΠΈΠ»Ρ, ΡΡΠΎΠ±Ρ Π»ΡΠ΄ΠΈ Π·Π½Π°Π»ΠΈ ΡΡΡ Π»Π΅ΠΊΡΠΈΠΊΡ. ΠΠΎ Π»ΠΎΠΊΠ°Π»ΡΠ½ΡΠΉ ΡΡΠΈΠ»Ρ ΡΠ°ΠΊΠΆΠ΅ Π²Π°ΠΆΠ΅Π½. ΠΡΠ»ΠΈ ΠΊΠΎΠ΄, ΠΊΠΎΡΠΎΡΡΠΉ Π²Ρ Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΡΠ΅ Π² ΡΠ°ΠΉΠ» Π²ΡΠ³Π»ΡΠ΄ΠΈΡ ΡΠ΅Π·ΠΊΠΎ ΠΎΡΠ»ΠΈΡΠ½ΡΠΌ ΠΎΡ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π±ΡΠ», ΡΠΎ ΡΡΠΎ Π²ΡΠ±ΡΠΎΡΠΈΡ Π±ΡΠ΄ΡΡΠ΅Π³ΠΎ ΡΠΈΡΠ°ΡΠ΅Π»Ρ ΠΈΠ· Π΅Π³ΠΎ ΡΠΈΡΠΌΠ° ΠΈ Π±ΡΠ΄Π΅Ρ ΠΌΠ΅ΡΠ°ΡΡ Π΅ΠΌΡ ΠΏΠΎΠ½ΠΈΠΌΠ°ΡΡ ΡΡΡΡΠΊΡΡΡΡ. Π‘ΡΠ°ΡΠ°ΠΉΡΠ΅ΡΡ ΠΈΠ·Π±Π΅Π³Π°ΡΡ ΡΡΠΎΠ³ΠΎ.
Java Code Style: ΠΊΠ°ΠΊ ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎ ΠΎΡΠΎΡΠΌΠ»ΡΡΡ ΠΊΠΎΠ΄ Java
Π‘ΡΠ°ΡΡΡ ΠΎΠ± ΠΈΠΌΠ΅Π½Π°Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ , ΠΊΠ»Π°ΡΡΠΎΠ² ΠΈ ΠΌΠ΅ΡΠΎΠ΄ΠΎΠ², ΡΠΊΠΎΠ±ΠΊΠ°Ρ , ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΡΡ , ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΠΈ ΠΊΠΎΠ΄Π° ΠΈ Π΄ΡΡΠ³ΠΈΡ ΠΏΡΠ°Π²ΠΈΠ»Π°Ρ Ρ ΠΎΡΠΎΡΠ΅Π³ΠΎ ΡΠΎΠ½Π°, ΠΏΡΠΈΠ½ΡΡΡΡ Π² ΡΠ·ΡΠΊΠ΅ JavΠ°.
Π ΡΠ·ΡΠΊΠ΅ Java, ΠΊΠ°ΠΊ ΠΈ Π²ΠΎ ΠΌΠ½ΠΎΠ³ΠΈΡ Π΄ΡΡΠ³ΠΈΡ ΡΠ·ΡΠΊΠ°Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ, Π΅ΡΡΡ Π½Π΅ΠΎΡΠΈΡΠΈΠ°Π»ΡΠ½ΡΠ΅, Π½ΠΎ ΠΏΡΠΈΠ½ΡΡΡΠ΅ ΡΠΎΠΎΠ±ΡΠ΅ΡΡΠ²ΠΎΠΌ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ² ΡΠΎΠ³Π»Π°ΡΠ΅Π½ΠΈΡ ΠΏΠΎ ΠΎΡΠΎΡΠΌΠ»Π΅Π½ΠΈΡ ΠΊΠΎΠ΄Π°. ΠΠ°Π²Π°ΠΉΡΠ΅ ΠΏΠΎΠΏΡΠΎΠ±ΡΠ΅ΠΌ ΡΠ°Π·ΠΎΠ±ΡΠ°ΡΡΡΡ, ΡΡΠΎ ΡΡΠΎ ΡΠ°ΠΊΠΎΠ΅, ΠΊΠ°ΠΊ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ ΠΈ ΠΏΠΎΡΠ΅ΠΌΡ Π²Π°ΠΆΠ½ΠΎ.
ΠΠ°ΡΠ΅ΠΌ Π½ΡΠΆΠ΅Π½ Π΅Π΄ΠΈΠ½ΡΠΉ ΡΡΠΈΠ»Ρ ΠΊΠΎΠ΄Π°
Java Code Style β ΡΡΠΎ ΡΠ΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°ΡΠΈΠΈ ΠΈ ΡΠΎΠ³Π»Π°ΡΠ΅Π½ΠΈΡ ΠΎ ΡΡΠΈΠ»Π΅ ΠΊΠΎΠ΄Π°, ΡΠΎΠ±ΡΠ°Π½Π½ΡΠ΅ Π²ΠΌΠ΅ΡΡΠ΅. ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ:
Π Π΅Π³Π»Π°ΠΌΠ΅Π½ΡΠΈΡΡΠ΅ΡΡΡ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ Π²Π΅ΡΠ΅ΠΉ, ΡΠ²ΡΠ·Π°Π½Π½ΡΡ Ρ ΠΎΡΠΎΡΠΌΠ»Π΅Π½ΠΈΠ΅ΠΌ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠ΅ΠΊΡΡΠ° ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌ, ΠΈ ΠΏΡΠΈΠ²ΠΎΠ΄ΡΡΡΡ ΡΡΠ΅Π±ΠΎΠ²Π°Π½ΠΈΡ, ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎ ΡΡΠΈΡΡΠ²Π°ΡΡ. ΠΠΎΡΠ΅ΠΌΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΈ ΡΡΠΈ ΡΡΠ°Π½Π΄Π°ΡΡΡ?
ΠΡΠΈΡΠΈΠ½Ρ, ΠΏΠΎ ΠΊΠΎΡΠΎΡΡΠΌ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΏΡΠΈΡΠ»ΠΈ ΠΊ ΡΠ°ΠΊΠΈΠΌ ΡΠΎΠ³Π»Π°ΡΠ΅Π½ΠΈΡΠΌ, Π»ΠΎΠ³ΠΈΡΠ½Ρ ΠΈ ΠΏΡΠΎΡΡΡ:
ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅ΠΏΡΠΈΠ½ΡΡΡΡ ΡΠΎΠ³Π»Π°ΡΠ΅Π½ΠΈΠΉ ΠΏΠΎ ΠΎΡΠΎΡΠΌΠ»Π΅Π½ΠΈΡ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΡΠ΄Π΅Π»Π°ΡΡ ΠΊΠΎΠ΄ Π±ΠΎΠ»Π΅Π΅ Π°ΠΊΠΊΡΡΠ°ΡΠ½ΡΠΌ, ΠΈΠ·Π±Π΅ΠΆΠ°ΡΡ ΠΎΡΠΈΠ±ΠΎΠΊ, ΡΠ²ΡΠ·Π°Π½Π½ΡΡ Ρ ΡΠ°Π·Π½ΡΠΌΠΈ ΡΡΠΈΠ»ΡΠΌΠΈ Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΡ Ρ Π»ΡΠ΄Π΅ΠΉ, ΡΠ°Π±ΠΎΡΠ°ΡΡΠΈΡ Π½Π°Π΄ ΠΎΠ΄Π½ΠΈΠΌ ΠΏΡΠΎΠ΅ΠΊΡΠΎΠΌ.
ΠΡΠ΅ΠΏΠΎΠ΄Π°Π²Π°ΡΠ΅Π»Ρ 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 Type | Rules of Naming | Examples |
Packages | The 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. | |
Classes | Class 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; |
Interfaces | Interface names should be capitalized like class names. | interface RasterDelegate; interface Storing; |
Methods | Methods 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; |
Constants | The 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: