Clean Code Cheat Sheet



Title: Enablon Clean Code Cheat Sheet by Galad - Cheatography.com Created Date: 5014Z. JUPYTER NOTEBOOK CHEAT SHEET Learn PYTHON from experts at Keyboard Shortcuts Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. It is used for data cleaning and transformation, numerical simulation, statistical. Clean ML code and PEP8 guidelines Cheat Sheet by andreeas26 via cheatography.com/133480/cs/27273/ Handling Exceptions Promote the Happy Path with Except ions. Feb 13, 2019 - Summary of 'Clean Code' By Robert C. Martin Cheat Sheet from CosteMaxime.

Optimizing names

Reveal an inte­nti­on.Key concepts names should commun­icate:
- Why it exists?
- What

Clean Code Standards

does it do?
- How is it used?Differ­ences between variables should be as close to the beginning of the name as possible.Avoid noisy labels. Use mature optional typing system.Make Siri say it. Avoid abbrev­iation and make your names self-e­xpl­ana­tory.Adding the datatype to a variable should be replaced with typing inform­ation. features: DataFrame vs. featu­res_dfNo magic numbers. Use constants.Be consis­tent! Pick a single word per concept, and use it everywhere it fits in.Use technical names for backend, and domain names as you get closer the customer.

Optimize functions

Small is beautiful. 3, 5, maybe 5 lines max!FUNC­TIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY. Stop at maximum 3 arguments!Avoid boolean arguments. This points to the fact that the function does more than one thing.Lengthy list of config­uration arguments should be grouped in a config­uration object that Codeshare a concept.Comman Query Separation (CQS)
- Comm­and: a function is changing some external “state”.
- Query: a function is returning some “infor­mat­ion”.Avoid side effects in feature engine­ering pipelines.Make temporal couplings explicit.DRY: Don't Repeat Yourself Eliminate Duplic­ates, Doubles, and Homolo­gues.

Naming conven­tions

Func­tionUse verbs to represent actions. Use is_for functions that return a boolean.get_f­eat­ures, fit, is_com­pletedVari­­ableSee scope length guidel­ines.x, var, my_var­­iableClassUse nouns to represent objects.Model, DataLo­aderMethodLowercase word(s). Separate with unders­core.Uncle bob clean code pdfclass­_­m­ethod, methodCons­tantUppercase single letter or word(s). Separate with unders­core.CONSTANT, MY_CON­­STANT, MY_LON­­G_­C­O­NSTANTModuleShort lowercase word(s). Separate with unders­core.module.py, my_mod­­ule.pyPack­ageShort lowercase word(s). Do NOT separate with unders­core.package, mypackage

The Scope Length Guidelines

How to avoid side effects in DataFrames

Copy any data coming in the function and return a fresh copy, after all the modifi­cat­ions.Return only the transf­ormed columns as a separated objects.Clean code cheat sheet printableAppend­-only inside the functions.

Handling Exceptions

Promote the Happy Path with Except­ions.Separate the Happy Path from the Outliers.Don’t reuse unrelated Exceptions types.

Progra­mming recomm­end­ations

Don’t compare Boolean values to True or False using the equiva­lence operator.Use the fact that empty sequences are falsy in if statem­ents.Use is not rather than not ... is in if statem­ents.Don’t use if x: when you mean if x is not None:.Use .star­tsw­ith() and .ends­with() instead of slicing strings.

Blank lines

Surround top-level functions and classes with two blank lines.Surround method defini­tions inside classes with a single blank line.Use blank lines sparingly inside functions to show clear steps.Formulas always break before binary operat­ions.

Whites­paces in Expres­sions and Statements

Assignment operators (=, +=, -=, and so forth).Exce­pti­on: when = is used to assign a default value to a function argument, do not surround it with spaces.CheatCleanCompar­isons (, !=, >, <. >=, <=) and (is, is not, in, not in).It is better to only add whitespace around the operators with the lowest priori­ty, especially when performing mathem­atical manipu­lation.Booleans (and, not, or).

Indent­ation

Spaces are the preferred indent­ation method.Tabs should be used solely to remain consistent with code that is already indented with tabs.Dictio­naries should use the default formatting rules without indent­ation. Don't try to keep the values aligned.

Comments

Don’t Hide Bad Code Behind Comments. Let Code Explain Itself! Limit the line length of comments and docstrings to 72 charac­ters.Use complete sentences, starting with a capital letter.Make sure to update comments if you change your code.Indent block comments to the same level as the code they describe.Start each line with a # followed by a single space.Separate paragraphs by a line containing a single #.Write inline comments on the same line as the statement they refer to.Separate inline comments by two or more spaces from the statement.Don’t use them to explain the obvious.

Tips and tricks

Use an IDE or linters, programs that analyze code and flag errors (e.g pycode­style, flake8)You can use autofo­rma­tters that refactor your code to conform with PEP 8 automa­tically (e.g. black, autopep8, yapf).

Maximum Line Length

PEP 8 suggests lines should be limited to 79 charac­ters.Wrap long lines by using Python's implied line contin­uation inside parent­heses, brackets and braces.

I updated my clean code cheat sheet.

Clean Code Cheat Sheet 2020

This time there are just minor changes:

Clean Code Cheat Sheet Fortnite

  • Principles: mind-sized components
  • Class Design: do stuff or know others, but not both
  • Maintainability killers: tangles
  • Refactoring patterns: refactor before adding functionality, small refactorings
  • removed duplication regarding one test asserts one thing
  • TDD principles: Test domain specific languages
  • fixed a bug in the ATDD/TDD cycle (run all acceptance tests)

If you miss something or think that there is something just plain wrong, please write a comment below.

Link: Clean Code V2.4