Flex SDK coding conventions and best practices - Naming
Study/Flex 2009/01/28 18:04 |Abbreviations
약어 쓰는거 비추. 예를 들어 calculateOptimalValue() 은 calcOptVal() 보다 나은 메소드명이다.
글자수 줄이는것보다 명확히 하는게 중요하다. 약어를 쓰지 않으면 개발자들은 굳이 “qual”이나 “qlfd”가 “qualified” 를 뜻한다는 것을 외우지 않아도 된다.
하지만, 표준화된 약어가 몇 개 있다:
- acc for accessibility, as in ButtonAccImpl
- auto for automatic, as in autoLayout
- eval for evaluate, as in EvalBindingResponder
- impl for implementation, as in ButtonAccImpl
- info for information, as in GridRowInfo
- num for number of, as in numChildren
- min for minimum, as in minWidth
- max for maximum, as in maxHeight
- nav for navigation, as in NavBar
- regexp for regular expression, as in RegExpValidator
- util for utility, as in StringUtil
가끔 약어들이 일관성있게 사용되지 않는 경우도 있다. 예를 들면, “horizontal” 과 “vertical”은 일반적으로 horizontalScrollPolicy 그리고 verticalScrollPolicy로 쓰기도 하지만, 확 줄여서 HBox, VBox 라고도 사용한다.
Acronyms
약어는 대문자로만 사용하거나 소문자로만 사용한다 (SWF 또는 swf, 절대 Swf로는 사용하지 않는다). 소문자로만 표기되는 경우는 약어가 자체 식별자이거나 식별자의 시작단어이면서 소문자로 시작해야 하는 경우이다.
예) CSSStyleDeclaration, IUID, uid, IIME, and imeMode.
Word boundaries
식별자가 여러 단어로 구성되어있을 경우, 두 가지 방법으로 단어들을 구분할 수 있다.
LayoutManager의 방법과 object_proxy의 방법.
가끔 단어들의 조합이 어떤 한 단어의 의미를 명확히 하지 못하는 때가 있기도 하고, 불행히도 이 규칙을 맞추지 않고 사용하는 경우도(dropdown, popUp, pulldown) 있다.
두 약어가 합쳐지더라도 약어 규칙을 따라야한다. 실제 사용되지는 않지만 loadCSSURL(). 같은 경우가 그렇다. 하지만 그런 경우를 피하려 노력한다.
Type-specifying names
만약 유형을 이름에 넣고싶으면 마지막 단어로 넣어라. AS1에서 사용하던 방식인 '변수명_유형' 은 사용하지 말 것. 예를들어 border, borderSkin, orborderShape은 좋지만 border_mc는 쓰지 않는다.
가끔 객체의 심플한 이름으로 가장 좋은 것은 유형을 이름으로 해 주는 것으로 위 설명과는 다른 방법이다.
var button:Button = new Button();
Package names
소문자로 시작하고 다음이어지는 단어의 첫 글자를 대문자로한다: controls, listClasses.
패키지명은 항상 명사나 동명사(동사+ing)로한다. 동사, 형용사, 부사는 안된다.
패키지는 비슷한 것들을 포함한 것이므로 복수형으로 한다: charts, collections, containers...
개념을 포함한 패키지는 일반적으로 동명사를 사용한다: binding, logging, messaging, printing. 그렇지 않으면 개념명사(concept nouns)를 사용한다: accessibility, core, graphics, rpc.
컴포넌트를 지원하는 클래스를 포함한 패키지의 컴포넌트의 이름 뒤에 Classes를 붙인다.
FooBar 컴포넌트를 지원하는 클래스를 포함했으면 fooBarClasses.
File names
For importable APIs, the file name must be the same as the public API inside. But include files don't have to follow this rule.
include될 Style metadata 파일은 대문자로 시작하고 이어지는 단어의 첫글자도 대문자로 한다. 그리고 마지막 단어는 “Styles”를 붙여준다: BorderStyles.as, ModalTransparencyStyles.as.
individual asset 파일은 소문자로 시작해서 단어 사이에는 '_'를 넣어준다: icon_align_left.png.
Namespace names
소문자로 시작하고 단어 사이에는 '_'를 넣어준다: mx_internal, object_proxy.
Interface names
대문자 I로 시작하고 이어지는 단어의 첫글자는 대문자로 한다: IList, IFocusManager, IUID.
Class names
대문자로 시작하고 이어지는 단어의 첫 글자도 대문자로 한다: Button, FocusManager, UIComponent.
Event의 하위 클래스: FooBarEvent.
Error의 하위 클래스: FooBarError.
EffectInstance의 하위 클래스 associated with effect FooBar FooBarInstance.
Formatter의 하위 클래스: FooBarFormatter.
Validator의 하위 클래스: FooBarValidator.
skinning classes: FooBarBackground, FooBarBorder, FooBarSkin, FooBarIcon, FooBarIndicator, FooBarSeparator, FooBarCursor, etc.
utility classes: FooBarUtil (FooBarUtils는 안됨. 패키지는 복수지만 클래스는 단수).
일반적인 기본 클래스명 FooBarBase: ComboBase, DateBase, DataGridBase, ListBase.
Event names
소문자로 시작하고 이어지는 단어의 첫 글자는 대문자로 한다: "move", "creationComplete".
Style names
소문자로 시작하고 이어지는 단어의 첫 글자는 대문자로 한다: color, fontSize.
Enumerated values for String properties
소문자로 시작하고 이어지는 단어의 첫 글자는 대문자로 한다: "auto", "filesOnly",
Constant names
대문자로만 작성하고 단어 사이에는 '_'로 구분한다: OFF, DEFAULT_WIDTH.
상수값이 문자열이면 식별자는 상수값과 동일하게 해야한다:
public static const FOO_BAR:String = "fooBar";
Property (variable and getter/setter) names
소문자로 시작하고 이어지는 단어의 첫 글자는 대문자로 한다: i, width, numChildren.
반복문에서 반복인덱스는 i를 사용하고 상한선 조건은 n을 사용한다. 반복문 안의 반복문의 경우, 반복인덱스는 j를 사용하고 상한선 조건은 m을 사용한다:
for (var i:int = 0; i < n; i++)
{
for (var j:int = 0; j < m; j++)
{
...
}
}
for-in 루프의 property 변수는 p를 사용한다:
for (var p:String in o)
{
...
} getter/setter를 오버라이드하고 기본 getter/setter를 계속 사용하고 싶으면, 기본명 앞에 '$'를 붙인다. 이 getter/setter는 final 로 지정되고 더 이상 supergetter/setter에 아무짓도 안하게 된다.
mx_internal final function get $numChildren():int
{
return super.numChildren;
}
Storage variable names
Give the storage variable for the getter/setter foo the name _foo.
Method names
소문자로 시작하고 이어지는 단어의 첫 글자는 대문자로 한다: measure(), updateDisplayList().
메소드명은 항상 동사를 사용한다.
Parameter가 없는 메소드는 일반적으로 getFooBar() 나 setFooBar() 의 이름을 사용하지 않는다; these should be implemented as getter/setters instead. However, if getFooBar() is a slow method requiring a large amount of computation, it should be named findFooBar(),calculateFooBar(), determineFooBar(), etc. to suggest this, rather than being a getter.
If a class overrides a method and wants to continue to expose the base method, it should do so by implementing a method whose name is the base name with a $ prepended. This method should be marked final and should do nothing more than call the supermethod.
mx_internal final function $addChild(child:DisplayObject):DisplayObject
{
return super.addChild(child);
}
Event handler names
이벤트 핸들러는 이벤트의 타입 뒤에 “Handler”를 붙여준다: mouseDownHandler().
만약 이벤트 핸들러가 하위 컴포넌트에 이벤트를 dispatch하는 경우(this는 예외)If the handler is for events dispatched by a subcomponent (i.e., not this), 하위 컴포넌트 이름을 앞에, 핸들러 이름을 뒤에 적어주고 '_'로 구분한다: textInput_focusInHandler().
Argument names
모든 setter의 argument는 value 를 사용한다:
이렇게:
public function set label(value:String):void
또는 이렇게:
public function set label(labelValue:String):void
또는 이렇게:
public function set label(val:String):void
이건 하지마:
public function set label(lab:String):void
이벤트 핸들러의 argument는 event 를 사용한다(e, evt, or eventObj 쓰지마) :
protected function mouseDownHandler(event:Event):void
Resource bundle names
If a resource bundle contains resources for a particular package, name the bundle the same as the package: controls, {formatters}}, validators.
Resource key names
소문자로 시작하고 이어지는 단어의 첫 글자는 대문자로 한다: pm, dayNamesShort.
Miscellaneous nomenclature
“object”는 막연한 의미이므로 피한다.
“item”은 data item이지 DisplayObject가 아니다.
“renderer”는 data item인 DisplayObject이다.
“type”은 AS3의 자료형이다; "kind"나 대충 그런의미의 다른 단어를 사용해라.
댓글을 달아 주세요
대단한데??
멋지냐? 해석하느라 머리 터지는 줄 알았다.
이정도 가지고 머리아프면 안되는데...
좋은 글
우후훗 ㅡㅡv
해석하니까 머리에 더 쏙쏙 들어와
멋진 글이예요~~~ 잘봤습니다.
감사합니다~
스타플 잘 쓰고있어요~