<aside> 💡 코딩 컨벤션을 정의합니다
</aside>
1-1 Naming (이름짓기)
# NO
def getUniqueVoters():
pass
# YES
def get_unique_voters():
pass
Class 혹은 Class를 반환하는 Factory Function의 이름은
첫 글자를 대문자 (InitialCaps)
# NO
class blog_writer:
pass
# YES
class BlogWriter:
pass
클래스 명은 CamelCase 로 작성
인스턴스 메소드의 첫 번째 인자는 self
class BookManager(models.Manager):
def create_book(self, title):
book = self.create(title=title)
# do something with the book
return book
class Book(models.Model):
title = models.CharField(max_length=100)
objects = BookManager()
book = Book.objects.create_book("Pride and Prejudice")
cls
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
@classmethod
def create(cls, title):
book = cls(title=title)
# do something with the book
return book
book = Book.create("Pride and Prejudice")
2.1 Import Group 작성 순서
# future
from __future__ import unicode_literals
# 1. Standard libraries
import json
from itertools import chain
# 2. Third-party libraries
import bcrypt
# 3. Django components
from django.http import Http404
from django.http.response import (
Http404, HttpResponse, HttpResponseNotAllowed, StreamingHttpResponse,
cookie,
)
# 4. Local Django components
from .models import LogEntry
# 5. try/excepts
try:
import yaml
except ImportError:
yaml = None
CONSTANT = 'foo'
class Example(object):
# ...
2.2 Indentation / Blank Line (들여쓰기 / 줄 공백)
<Indentation>
from django.http.response import (
Http404, HttpResponse, HttpResponseNotAllowed, StreamingHttpResponse,# Line Breaking 후 공백 4개로 들여쓰기
cookie,# 마지막 Import 뒤에도 Comma 포함
)# 닫는 괄호가 있는 줄에는 괄호만 존재
<Blank Line>
import requests
import json
# --------- 1줄 공백
from django.http.response import (
Http404, HttpResponse, HttpResponseNotAllowed, StreamingHttpResponse,
cookie,
)
# --------- 2줄 공백
# --------- 2줄 공백
class Example:
pass
3.1 Spaces
<!-- NO -->
{{var}}
{{ var }}
<!-- YES -->
{{ var }}
4-1. Request Parameter
request
권장# NO
def my_view(req, foo):
pass
# YES
def my_view(request, foo):
pass