writing code for others
DESCRIPTION
TRANSCRIPT
![Page 1: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/1.jpg)
Writing Code For Others
http://www.codinghorror.com/blog/
![Page 3: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/3.jpg)
why do we write code?
• to get it executed by a machine• the machine understands 0s and 1s only• then why do we use high level languages like c,
java, or ruby• did the machine request us to use c or ruby?
http://sourcemaking.com/
![Page 4: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/4.jpg)
we use high level languages so humans can understand code by
reading it
http://www.makinggoodsoftware.com/2009/07/07/5-top-non-technical-mistakes-made-by-programmers/
![Page 5: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/5.jpg)
how do others find reading your code?
• do they find it helpful in understanding your program–the optimizations you’ve done–your naming conventions, your coding style–how long they should take to do it–more importantly, would they like looking at your
code–would they love the way you code• or do they simply find it quite boring
http://stackoverflow.com/questions/1711/what-is-the-single-most-influential-book-every-programmer-should-read
![Page 6: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/6.jpg)
you can help others to quickly read your code and understand it
http://www.joelonsoftware.com/articles/Wrong.html
![Page 7: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/7.jpg)
characteristics of good code
• easy to read• easy to use• clean, simple, straight forward• easy to make any changes• self explanatory• short methods, clear names• consistent coding standards
http://www.thoughtclusters.com/2010/09/characteristics-of-great-programmers/
![Page 8: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/8.jpg)
writing necessary comments
http://www.makinggoodsoftware.com/2009/09/01/programmers-top-10-sentences/
![Page 9: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/9.jpg)
writing necessary comments
• let’s define “necessary” comments
http://en.wikipedia.org/wiki/Code_smell
![Page 10: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/10.jpg)
writing necessary comments
• should not tell us how code works• but why code works• convert your comments into variable names or
function names, constants
http://en.wikipedia.org/wiki/Anti-pattern#Programming_anti-patterns
![Page 11: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/11.jpg)
follow same indentations
• different people love different editors• tab is treated differently by different editors• define tab = 2 or 4 spaces• good if entire team follows same definition of
tab
http://www.makinggoodsoftware.com/2011/05/23/top-7-programmers-bad-habits/
![Page 12: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/12.jpg)
more than indentations
http://www.makinggoodsoftware.com/2010/05/27/10-things-they-never-teach-in-college-about-programming/
![Page 13: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/13.jpg)
more than indentations
http://www.makinggoodsoftware.com/2010/03/13/my-ten-development-principles/
![Page 14: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/14.jpg)
write working pseudo code
•write a program• given a number• print “divisible by 3” if it is divisible by 3• print “divisible by 5 if it is divisible by 5• print “divisible by 15” if it is divisible by 15
http://www.codinghorror.com/blog/2009/05/pseudocode-or-code.html
![Page 15: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/15.jpg)
write working pseudo code
•write a program• given a number• print “divisible by 3” if it is divisible by 3• print “divisible by 5 if it is divisible by 5• print “divisible by 15” if it is divisible by 15
http://www.codinghorror.com/blog/2009/05/pseudocode-or-code.html
![Page 16: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/16.jpg)
write working pseudo code
• you might have simply optimized• reducing one divide operation
![Page 17: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/17.jpg)
write working pseudo code
•write a program• given a number• print “divisible by 3” if it is divisible by 3• print “divisible by 5 if it is divisible by 5- a change requestprint “divisible by 15” if it is divisible by 15print “divisible by 17” if it is divisible by 17
http://stackoverflow.com/questions/84556/whats-your-favorite-programmer-cartoon
![Page 18: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/18.jpg)
write working pseudo code
• If pseudo code would have been followed•without optimizations
![Page 20: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/20.jpg)
nested if-else
• avoid nested if-else chain• use switch or multiple returns• same follows for iterators while, for
http://inter-sections.net/2007/11/13/how-to-recognise-a-good-programmer
![Page 21: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/21.jpg)
god class
• a file or class or a module• knows too much or does too much• contains more than 1000 or 5000 loc• class with a large number of attributes,
operations, or both• uses heavy encapsulation
http://www.dehora.net/journal/2005/06/writing_code_for_others_that_use_it.html
![Page 22: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/22.jpg)
long methods
•would you like to read and understand a function which is of 300 lines or say 1000 lines
• or would you like to understand functions with 10 lines, 20, say 30 lines at the most
http://www.dmst.aueb.gr/dds/pubs/jrnl/2003-ACMQ-CR/html/article.html
![Page 23: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/23.jpg)
one function one responsibility
http://www.skorks.com/2010/05/why-i-love-reading-other-peoples-code-and-you-should-too/
![Page 24: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/24.jpg)
one function one responsibility
http://rubylearning.com/blog/2010/11/08/how-does-your-code-smell/
![Page 25: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/25.jpg)
long parameters list
http://www.codinghorror.com/blog/2006/05/code-smells.html
![Page 26: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/26.jpg)
more coding horrors
• uncommunicative name - naming functions and variables•magic numbers, hardcoded values• data clumps• feature Envy•middle man …and more
http://www.codinghorror.com/blog/2006/05/code-smells.html
![Page 27: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/27.jpg)
characteristics of a good code
• easy to read• easy to understand• easy to make any changes• clean, simple• looks straight forward• self explanatory• short methods, clear names
http://www.perlmonks.org/?node_id=592616
![Page 28: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/28.jpg)
characteristics of good code
• easy to read• easy to use• clean, simple, straight forward• easy to make any changes• self explanatory• short methods, clear names• consistent coding standards
http://www.thoughtclusters.com/2010/09/characteristics-of-great-programmers/
![Page 29: Writing code for others](https://reader036.vdocuments.site/reader036/viewer/2022081516/54be79904a7959af118b45e5/html5/thumbnails/29.jpg)
characteristics of a good coder
• likes to read code written by well known programmers• likes to do code review of junior or senior
colleagues• participates in pair programming• learns new things weekly w.r.t. coding• does not make unnecessary complex
optimizations• does not refractor working code again and
again
and what else would you like to suggest?
http://sourcemaking.com/refactoring