Slide 1

Slide 1 text

Readability Counts @smitthakkar96 1

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

–Wikipedia “Readability is the ease with which a reader can understand a written text.” 3

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

We’ll not discussing • Optimizing the code to run fast • Design Patterns • Programming Language Speaking robots
 
 
 
 
 
 
 5

Slide 6

Slide 6 text

We’ll discuss • Programming Style Guides • Choosing Meaningful Name • Writing meaningful comments • Program Organization

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Programming Style Guide

Slide 9

Slide 9 text

– wikipedia “Programming style is a set of rules or guidelines used when writing the sourcecode for a computer program..”

Slide 10

Slide 10 text

I am new to programming, I am not aware about standards how to I make a style guide?

Slide 11

Slide 11 text

Build one on top of the standard ones • Google Style Guides • PEP8 for Python • Java specs by Oracle • etc..

Slide 12

Slide 12 text

Choosing meaningful names

Slide 13

Slide 13 text

–Phil Karlton “Two hard things in computer science are cache invalidation and naming things.”

Slide 14

Slide 14 text

Avoid Generic Names tmp tmp2 mana ger data

Slide 15

Slide 15 text

ucf = UpperCaseFormatter() ba = BankAccount() formatter = UpperCaseFormatter() account = BankAccount() Avoid Abbreviations

Slide 16

Slide 16 text

Avoid using datatype as name array = [1,2,3] string = "girlscriptsummit is awesome" numbers = [1,2,3] review = "girlscriptsummit is awesome"

Slide 17

Slide 17 text

Nouns & Verbs Use nouns for variables and classes. size, price, Task, Scheduler, Bank Account Use verbs for functions. get_file_size, make_account, deposit

Slide 18

Slide 18 text

number = [1,2,3,4] numbers = [1,2,3,4] Use plural names for data structures like arrays

Slide 19

Slide 19 text

Comments

Slide 20

Slide 20 text

Don’t say the obvious # increments x by 2 x = x + 2 # compensate for border on both the sides x = x + 2

Slide 21

Slide 21 text

# The following is an optimization to save # lot of memcache calls. Handle with care! ... Explain why you made that choice

Slide 22

Slide 22 text

# -- 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

Slide 23

Slide 23 text

# find length of the longest line n = max([len(line) for line in lines]) n = len(longest(lines)) Make Comments Unnecessary

Slide 24

Slide 24 text

Program Organization

Slide 25

Slide 25 text

Divide & Conquer Split the program into small independent modules and functions.

Slide 26

Slide 26 text

The 7 ± 2 Rule The number of objects an average human can hold in working memory is 7 ± 2. - Miller's Law

Slide 27

Slide 27 text

Separate what and how def main(): filename = sys.argv[1] words = read_words(filename) freq = wordfreq(words) print_freq(freq)

Slide 28

Slide 28 text

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: ...

Slide 29

Slide 29 text

White spaces and line breaks

Slide 30

Slide 30 text

Questions?