.title-slide # 7 code editing tips in PyCharm .right[Andrey Vlasovskikh] .right[JetBrains] .right[#PyConRU 2018] .right[2018-07-23] --- .center[
] * I'm [@vlasovskikh](http://twitter.com/vlasovskikh) on Twitter * I'm the PyCharm Community Lead * I also maintain IdeaVim, intellij-micropython * I've been working on the Python type system * PEP 484: Type Hints --- # My friends are slower than me at code editing * Despite years of experience with PyCharm * Let me do it faster! ๐ * Me at every hackathon or programming contest * They use PyCharm as a simple text editor * Type everything by themselves * Occasional use of code completion --- # IDEs are best known for 1. Tooling integrations * Debugger, VCS, testing, ... 1. Navigation * Go to definition, find usages, ... 1. Code inspections * Highlight possible bugs and code style problems 1. _Editing?_ * Probably only code completion * We'll explore it in this talk --- .title-slide # Tip 1. Optimize tooling ๐จ and navigation ๐ first Editing is only a fraction of development time --- # IDE as a
text
_code_ editor? ๐ค * PyCharm knows Python syntax and semantics * Navigation and code inspections use it * Re-use the code model for editing * Syntax- and semantics-aware editing actions * Hard or impossible to implement in text editors --- .title-slide # Tip 2. Code is a tree ๐ฒ with references It's a syntax tree with semantic references --- # Abstract syntax tree from os import stat . class Path(object): |- ImportFrom('os', 'stat') def __init__(self, name): `- ClassDef('Path') self.name = name |- bases=Name('object') `- FunctionDef('__init__') |- args= | |- Name('self') | `- Name('name') `- Assign |- Target('self.name') `- Name('name') --- # "Help | Find Action" * The **only** shortcut you must know *
Ctrl+Shift+A
* Windows, Linux *
โงโA
* macOS * Cannot find .action[Find Action] in "Find Action" .center[
] --- # .action[Extend] / .action[Shrink Selection] .center[
] --- # .action[Declaration] and .action[Find Usages] .center[
] --- .title-slide # Tip 3. Refactor automatically ๐ค, not by hand PyCharm edits the tree and updates references for you --- # .action[Rename] * Skips non-usages, suggests probable usages .center[
] --- # In-place rename for locals .center[
] --- # .action[Change Signature] * Reorder, add default, convert to keyword-only .center[
] --- # .action[Refactor This] .center[
] --- .title-slide # Tip 4. Fix bugs ๐ highlighted by code inspections Eliminate errors, warnings, weak warnings, typos --- # Unresolved name: Insert import * .action[Show Intention Actions] .center[
] --- # Missing await: Insert await .center[
] --- # PEP 8 violation: Reformat file * Reformat according to your code style .center[
] --- .title-slide # Tip 5. Look at the actions the lightbulb ๐ก offers Typical editing actions with this fragment of code --- # Change string formatting .center[
] --- # Change if-statements .center[
] --- .title-slide # Tip 6. Use type hints ๐ท to get better code completion Type hints for functions better define your API --- .title-slide # Tip 7. Learn new tricks in the productivity ๐ guide Improve your editing statistics --- # .action[Productivity Guide] .center[
] --- .title-slide # Analysis --- # The upsides * "Help | Productivity Guide" * Code completion: 107 chars / day * Intention actions: 5 possible bugs / day * How did you do it? ๐ฏ * My friends at hackathons or programming contests * Fewer boring things to type --- # The downsides * Not the biggest time-saver * Editing is only a fraction of your development time * The editor is slow sometimes * Requires parsing and static code analysis * The actions and shortcuts are hard to memorize * They are only useful if you remember them --- # Conclusion * 7 code editing tips in PyCharm 1. Optimize tooling ๐จ and navigation ๐ first 2. Code is a tree ๐ฒ with references 3. Refactor automatically ๐ค, not by hand * .action[Refactor This] 4. Fix bugs ๐ highlighted by code inspections * .action[Show Intention Actions] 5. Look at the actions the lightbulb ๐ก offers 6. Use type hints ๐ท to get better code completion 7. Learn new tricks in the productivity ๐ guide * Follow me on Twitter: [@vlasovskikh](http://twitter.com/vlasovskikh) --- # Extra 1. Remove default argument .center[
]