Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Readability Counts

Avatar for smit thakkar smit thakkar
December 16, 2017

Readability Counts

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Have you found unreadable code and wondered how to fix it? Have you ever seen code that was simply a pleasure to read?

If you've ever wondered what makes code easy to read, this talk is for you.

During this talk we'll learn a number of techniques for refactoring code to improve readability and maintainability.

Avatar for smit thakkar

smit thakkar

December 16, 2017
Tweet

More Decks by smit thakkar

Other Decks in Programming

Transcript

  1. README.md • Chief Architect at Corridor Funds • Opensource Contributor

    at Gluster, fossasia, and NgUI • Organizer at GDG Gandhinagar • Github: github.com/smitthakkar96 • Email: [email protected] 2
  2. Why does readability matter? • Readability is about making our

    lives easier • Code is read more often then it is written • Better readability means better maintainability • Readable code makes on boarding new contributors/ developers easier. 4
  3. We’ll not discussing • Optimizing the code to run fast

    • Design Patterns • Programming Language Speaking robots
 
 
 
 
 
 
 5
  4. We’ll discuss • Programming Style Guides • Choosing Meaningful Name

    • Writing meaningful comments • Program Organization
  5. – wikipedia “Programming style is a set of rules or

    guidelines used when writing the sourcecode for a computer program..”
  6. I am new to programming, I am not aware about

    standards how to I make a style guide?
  7. Build one on top of the standard ones • Google

    Style Guides • PEP8 for Python • Java specs by Oracle • etc..
  8. Avoid using datatype as name array = [1,2,3] string =

    "girlscriptsummit is awesome" numbers = [1,2,3] review = "girlscriptsummit is awesome"
  9. Nouns & Verbs Use nouns for variables and classes. size,

    price, Task, Scheduler, Bank Account Use verbs for functions. get_file_size, make_account, deposit
  10. Don’t say the obvious # increments x by 2 x

    = x + 2 # compensate for border on both the sides x = x + 2
  11. # The following is an optimization to save # lot

    of memcache calls. Handle with care! ... Explain why you made that choice
  12. # -- XXX -- Smit - Sep 2015 -- #

    UTF-conversion was failing for a Chinese # user for reasons I couldn't understand. # Added "ignore" as second argument to handle # that temporarily. name = name.encode("utf-8", "ignore") Document special cases
  13. # find length of the longest line n = max([len(line)

    for line in lines]) n = len(longest(lines)) Make Comments Unnecessary
  14. The 7 ± 2 Rule The number of objects an

    average human can hold in working memory is 7 ± 2. - Miller's Law
  15. Separate what and how def main(): filename = sys.argv[1] words

    = read_words(filename) freq = wordfreq(words) print_freq(freq)
  16. Avoid too many nested levels def update_post(...): post = get_post()

    if action == 'update_title': update_post_title() elif action == ‘add-tag’: update_post_add_tag() else: update_other_data() def update_post(...): post = get_post() if action == 'update_title': if title == '': ... else: ... elif action == 'add-tag': ... else: ...