Writing Beautiful Code

Anand Chitipothu (~anandology)


27

Votes

Description:

Writing code is hard. Writing beautiful code is even more so.

How to write code that is pleasant to read, easy to understand and joy to maintain?

Simple things like using right variable names, avoiding to much nesting, using white space and comments etc. makes a lot difference to readability of the code. Even though these things look very simple, it is very hard to put them in practice.

In this talk, I'll try summarise the age old wisdom of writing beautiful code using python examples.

The following is the rough outline of the talk and each item will be explained with one or more Python examples.

Outline

  • Choose meaningful variable names
  • Avoid generic names like tmp
  • Give names to magic numbers
  • Parenthesize to avoid ambiguity.
  • Unparenthesize to improve readability.
  • Avoid too many nested levels
  • Avoid too many branches
  • Use "telephone test" for readability
  • Use idioms of the language
  • Avoid superficial comments
  • Add comments to explain complex piece of software
  • Avoid writing comments by writing code clearly
  • Avoid side effects
  • Try to make functions stateless
  • Let one function do one thing well
  • Write smaller functions - the 7 plus or minus 2 rule
  • Separate "what" from "how"
  • Don't sacrifice clarity for efficiency

Prerequisites:

Open mind.

Content URLs:

https://github.com/anandology/writing-beautiful-code

Speaker Info:

Anand is a software consultant and trainer. He offers corporate trainings on Python and conducts public courses on Python programming in India.

Anand has worked at Internet Archive and Stand Life Sciences. He blogs at http://anandology.com/.

Speaker Links:

  • http://anandology.com/
  • http://github.com/anandology

Recent Speaking Experience

Section: Core Python
Type: Talks
Target Audience: Beginner
Last Updated: