Showing posts with label code smells. Show all posts
Showing posts with label code smells. Show all posts

Tuesday, September 20, 2016

Code smells - Book chapter reading

Sept. 20, 2016

Read clean code book - Chapter 17 - Smells and Heuristics

Julia's notes:
Comments
Environments
General
Java
Names
Tests

Comments:
C1: Inappropriate Information
C2: Obsolete Comment
C3: Rudundant Comment
C4: Poorly Written Comment
C5: Commented-Out Code

Environment:
E1: Build Requires More Than One Step
E2: Tests Require More Than One Step

Functions:
F1: Too Many Arguments
F2: Output Arguments
F3: Flag Arguments
F4: Dead Function

General - (hightlights to help memorize - red color, Julia produced the code with smells...)
G1: Multiple Languages in One Source File
G2: Obvious Behavior Is Unimplemented
G3: Incorrect Behavior at the Boundaries
G4: Overridden Safeties
G5: Duplication
G6: Code at Wrong Level of Abstraction
G7: Base Classes Depending on Their Derivatives
G8: Too Much Information
G9: Dead Code
G10: Vertical Separation
G11: Inconsistency  - the principle of least surprise
G12: Clutter
G13: Artificial Coupling - lazy and careless of placement of a variable, constant or function
G14: Feature Envy -
G15: Selector Arguments

Great blog about clean code: CMU - top 4 computer science - a professor
http://jxyzabc.blogspot.ca/2012/12/treat-yo-self-clean-up-your-code.html
"During the course of grad school, my relationship with my code has become increasingly important.  Clean, modular, and well-documented code (with tests!) is not only less likely to have bugs but will be useful for longer.  Clean code provides a solid foundation for you and potentially other researchers.  Modular code makes it easier to reuse parts of your code. Also, knowing exactly what your code does just feels good."

Editorial notes:
1. Clean, modular, and well-documented code (with tests!)
Julia, let us hold high standards as a software programmer -
Review common code smells before you write code for a project. 

2. Work on blog writing, and see if you can write good blogs as
http://jxyzabc.blogspot.ca

3. work on the blog:
http://jxyzabc.blogspot.ca/search/label/academic%20advice

study writing - write down things to learn from the above article

4. Borrow some ideas from website usability & user experience - Julia, you need to read 60 pages - page by page first.

Study Website Usability & User Experience (UX) Definition
(60 pages - plan to read 1- 2 hours, understand the concepts first)

http://blog.usabilla.com/the-abc-of-usability/
http://blog.usabilla.com/6-ted-talks-every-ux-professional-should-watch/

5. How Code smells research leads to the website Usability research? User experience research?




Sunday, September 11, 2016

Code smells - small research

Sept. 11, 2016

Small research about Common code smells

Warm up the topic:
Benefits:
1. Reduce stress to write and also maintain the code
2. Build a good habit to write/ refactor code/ design concern/ thinking logically,
3. Amazon leadership principle: internal customer support -> external support, high standard, genesis of AWS - from infrastructure support to AWS

https://www.amazon.jobs/principles

Insist on the Highest Standards
Leaders have relentlessly high standards

Train to be a leader.
End of warm up



On Sept. 12 evening, spent 2 hours reading code smells -
Read the boook "refactor "
http://goo.gl/8r2AJO

Code smells:
Refactoring: Improving the design of existing code

Chapter 3: Bad smells in code:
Duplicate code   ()
Long method      ()
Large Class
Long Parameter Lsit
Divergent change
Shortgun surgery
Feature Envy
Data Clumps
Primitive Obsession
Switch Statements
Parallel Inheritance Hierarchies
Lazy Class
Speculative Generality
Temporary Field
Message Chains
Middle Man
Inappropriate Intimacy
Alternative Classes with Different Interfaces
Incomplete Library Class
Data Class
Refused Bequest
Comments

End of 2 hours of reading - have the above notes

Read the article:

1.https://en.wikipedia.org/wiki/Code_smell

Julia's Notes

Application-level smells:
1. Duplicate code
2. Contrived complexity

Class-level smells:
1. Large class
2. Feature envy
3. Refused bequest  (Liskov substitution principle)
4. Lazy clss/ freeloader
5. Excessive use of literals
6. Cyclomatic complexity
7. Downcasting
8. Orphan variable or constant class

Method-level smells
1. Too many parameters
2. Long method
3. Excessively long identifiers
4. Excessively short identifiers
5. Excessive return of data

2. https://en.wikipedia.org/wiki/Cyclomatic_complexity

a positive correlation between cyclomatic complexity and defects: functions and methods that have the highest complexity

tend to also contain the most defects

3.Static program analysis
https://en.wikipedia.org/wiki/Static_program_analysis

Blog reading:
Leetcode 247: Segment Tree Query (II)
Leetcode 247 Segment Tree Query (II)

http://www.tangjikai.com/algorithms/leetcode-247-segment-tree-query-ii

Will come back to work on the algorithm.

Julia's most favorite concept - Cyclomatic complexity - No more long function!

Sunday, November 8, 2015

testing, automation and testing patterns

Nov. 8, 2015

  Watch the video:

Automated Testing Patterns and Smells

 https://www.youtube.com/watch?v=Pq6LHFM4JvE

Julia is a developer in the city of Vancouver last 5 years, she enjoyed the understanding how to minimize time 70% by following one simple principle: DRY - Do not repeat yourself.

For instance, to calculate something, the same code repeats twice; Just a simple example, Julia does not like to see the code repetition in the C# code, so she gives out a try to use base class, and then, merge to one function applying to the base class. But, all other repetitions are everywhere, so she cuts other places similar to the one, 2 to 1. So, do a math, the code needs change for cases 2 x 2 x 2, 3 place duplication, becoming 1 x 1 x 1; Done, reduce the 7/8 work to work with design, unit test, and bugs related to design. (similar ideas seen video, watched on Nov. 13, 2015 - https://www.youtube.com/watch?v=bxQK7hcVyGs&list=PLD4GnSXHpkQ2_eKG5fKzszXs5hYcEsft7)

Julia tries to contribute the ideas to show how important it is to following DRY principle, and then, reduce test cases, improve code quality, and then, shorten time to work on a project.

Back to the video, Julia likes to write down the notes from the video, she does not have time to read 600 pages book, but she knows the tip: get a good video, google talk, and then, invest 2-3 hours:

1. video time 16:07/59.33
What is a " Test smell"
1. duplicate code, hard coded values, etc.

A set pf symptoms of an underlying problem in test code. Like duplicate code, breaking DRY principle.

In the talk, using changing diaper as an example, how to know when to change diaper? When diaper stinks.

code smells - visible problems in test code
behavior smells - test behaving badly
project smells - testing-related problems visible to a project manager

2. video time 19:27 / 59:33
What's a "Test Pattern"?

A "test pattern" is a recurring solution to a test automation problem
- E.g. A "Mock Object" solves the problem of verifying the behavior of an object that should delegate behavior to other objects

Test Patterns occur at many levels:
- Test automation strategy pattern
 recorded test vs scripted test
- Test design Patterns
 implicit setup  vs delegated setup
- Test coding patterns
  assertion method, creation method
- Language-specific test coding idioms
  expected exception test, constructor test

3. video time 20:28/59:33
Common code smells
  • conditional test logic
  • hard to test code
  • obscure test
  • test code duplication 
  • test logic in production 
4. Example to explain, best part
video time 22:16/59:33

1. Better assertion
2. Hard-wired test data - what is the relationship between those data? What is math? Do we need math? 30, why it is not 1, 19.99, why it is not 2, 69.96, why it is not 3? 6 months later, what is business logic?
    Hard-wired test data - will lead to fragile test

3. Introduce custom assert

  Avoid any conditional test logic in the test method - make less test code.
  Problem of conditional test logic is not sure what you are testing. <-  Julia likes this argument
  use customer assert, make a compaction of the code.

video time 28:58 / 59:33
4. Automated fixture teardown

tear down logic is a smell, such as, to separate business logic with presence logic

explicitly create deleteAllTestObjects instead of those complicated logic checking - nested try methods.

5. video time 32:07 / 59:33
Obscure test - irrelevant information

create address
create ..                               vs   createAnnoymousCustomer       vs createAnnoymounsInvoice
create customer                         createAnnoymounsInvoice

Hide detail in creation method - encapsulation, what is level of info - different levels info stays in one function - create more functions - each function much easy to be understandable, easy for unit test, variable scope less than a few lines

6. Test coverage, rapid test writing

Work outside in, create functions not existed yet, type test what you like to look

- Julia's comment:
If the code she wrote, it seems that the task is not showed in Leetcode questions, that means the code is not abstracted, not using object-oriented programming, cannot be unit test easily, people cannot read it without too much memorization, one function does all kinds of tasks; results? just too much time wasted for development, maintenance, testing, and it is bad behavior, not a pro. 


7. video time
44:08/ 59:38
Reducing Erratic Tests - Shared Fixture

Build new Shared Fixture for each run
- Avoids Unrepeatable Tests
- When:
 >> Lazy Setup
>> Setup Decorator
>> SuiteFixture Setup

Fragile Tests:
Causes:
Interface Sensitivity
- Every time you can change the SUT (S- , U- , T- tear down), tests won't compile or start failing
- You need to modify lots of tests to get things "Green" again
- Greatly increases the cost of maintaining the system

Behavior Sensitivity
- Behavior of the SUT changes but it should not affect test outcome
- Caused by being dependent on too much of the SUT's behavior.

Data sensitivity

Context sensitivity
- something outside the SUT changes
e.g. system time/date, contents of another application

46:19/ 59:33
Hard to test code
. code can be hard to test for a number of reasons:
- too closely coupled to other software
- No interface provided to set state, observe state
- Only asynchronous interfaces provided

. Root cause is lack of design for testability
- comes naturally with Test-Driven development
- Must be retrofitted to legacy (test-less) software

. Temporary workaround is Test Hook
- Becomes test logic in production (code smell) not removed.

50:17/59:33
What Does it take to be successful?
Programming Experience
+ xUnit Experience
+ Testing Experience
+ Design for Testability
-  Test smells
+ Test Automation Patterns
+ Fanatical Attention to Test Maintainability

read the website:
http://xunitpatterns.com/

Julia's comment:
Source code should be only written for machine to understand             - 20%
Source code should be readable for herself after 6 months / years        - 20%
Source code should be minimal - DRY principle - All other principle for easy to understand,
relax for herself - any time                                                                     - 30%
Source code should be fun to read, with documented unit test cases, log history etc.                                           - ?