Semester 1, AY 2014/15
10 September 2014
Programming is not just about implementing a software. It is about communicating with other developers using code, comments, commit messages, API documentations, developers' guide etc. Getting the code to compile and run correctly is just a small part of the process. Take time to communicate properly.
-
Your reputation as a software engineer depends on your code (extreme case: github is your resume).
-
Review your CS2103's Handouts/Slides on Good Code, Bad Code.
-
Properties of Good Code:
- Correct
- Cheanglable
- Readable (by human)
- Extensible
- Maintainable (Boehm's curve; common to have a cost of 100:1 after delivery)
-
Check out Page 1-2 of the Clean Code Cheat Sheet for the dos and don'ts for writing clean code.
-
Advices:
- Don't try to be clever or terse
- Make your code self-explanatory (write as little comment as possible)
- Comments are for high-level descriptions (what and why)
- Use English
- Don't be afraid of long names (thanks to autocomplete) (e.g.,
StudentViewController *studentViewController = [storyboard instantiateViewControllerWithIdentifier:@"StudentViewControllerID"]
) - Use tools to indent/format your code
-
Don't underestimate the importance of indentation (see Apple's goto fail bug)
-
Learning to write good code
- practice: write and rewrite
- read good code from others
- Advocated by Kent Beck (as seen in CS2103): Red-Green-Refactor
- DHH thinks that TDD is dead. Martin Fowler questioned that.
- Nachi Nagappan studied TDD usage in four industrial team and showed that TDD reduces defect density by 40-90%, with 15-35% increase in development time.
- We will do TDD-redux in CS3281/2:
- automated testing process
- write test cases early;
- perform testing concurrently with development.
- Goal: to have a peace of mind that your changes did not break anything.
- "Rigorous inspections can remove up to 90% of errors from a software product before the first test case is run." -- “Facts and Fallacies of Software Engineering,” Robert Glass.
- "The average defect detection rate is only 25% for unit testing, 35% for function testing, and 45% for integration testing. In contrast, the average effectiveness of design and code inspections are 55 and 60%." -- - “Code Complete,” Steve McConnell.
- Best practices by SmartBear
- review small chunk (<400 lines) at a time
- take you time
- author should make sure code is ready before review (self-review, tested, make code readable)
- keep a checklist of common errors
- be positive (finding bugs is a good thing)
- Gitflow Workflow
- Commit often and write good commit message
- Good practices by Tarin Gamberini
- Describe specifically what has changed and why
- At the end of every iteration:
- Clean up code as needed
- Pass test cases written for work done in that iteration
- Have code written reviewed by another peer
- Merge code into the main branch