There are only two hard things in Computer Science: cache invalidation and naming things
Phil Karlton
a = b * c;
vs
weekly_pay = hours_worked * hourly_pay_rate;
Why?
Reasons
construct | convention |
---|---|
Class | CapWords |
Functions | lowercase |
Methods | lowercase |
Type variables | CapWords |
Constants | UPPERCASE |
Package | lowercase |
Module | lowercase |
_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.
>>> class Test(object):
... def __mangled_name(self):
... pass
... def normal_name(self):
... pass
>>> t = Test()
>>> [attr for attr in dir(t) if 'name' in attr]
['_Test__mangled_name', 'normal_name']
Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.
PEP8
PEP8
https://www.python.org/dev/peps/pep-0008/
https://development.robinwinslow.uk/2014/01/05/summary-of-python-code-style-conventions/
Black
Black (Why?)
https://github.com/django/deps/blob/master/accepted/0008-black.rst
Black
isort
https://github.com/timothycrosley/isort
isort + black in our CI pipeline
1. Oddzialenie enterami ściany tekstu | black! |
wcięcia, długość linii |
black! |
3. help_texty w modelach | tak! |
4. dodawanie docs stringów do metod, klas | https://confluence.3bpo.com/display/IG/Dokumentowanie+kodu |
5. dodanie komentarza | jak powyżej |
6. unikanie skrótów myślowych | tak ale.. |
Use intention revealing names
The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent.
int d; // elapsed time in days
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;
Avoid disinformation
Avoid leaving false clues that obscure the meaning of code.
Make meaningful distinctions
Avoid leaving false clues that obscure the meaning of code.
class ProductInfo
class ProductData
void getActiveAccount()
void getActiveAccounts()
void getActiveAccountsInfo()
Use pronounceable names
If you can’t pronounce it, you can’t discuss it without sounding like an idiot. “
Date genymdhms;
Date modymdhms;
Int pszqint;
Use searchable names
Single-letter names and numeric constants have a particular problem in that they are not easy to locate across a body of text.
Avoid encodings
PhoneNumber phoneString;
client_names_list = ['John', 'Jack']
client_names: List[str] = ['John','Jack']
Classes
Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser. Avoid words like Manager, Processor, Data, or Info in the name of a class. A class name should not be a verb.
Methods
Methods should have verb or verb phrase names like postPayment, deletePage, or save.
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
Martin Fowler
Thank You