Sunday, August 21, 2016

Leetcode 125 - Valid Palindrome - Code styles study

August 21, 2016

Here is the blog with 5+ practice.

8th practice code

Summary of study: 
Go over blogs, read 2+ hours, and then, write 9th practice; Be able to find issues in 8th practice. 

9th practice

10th practice

- best solution - extract one more function called removeNoise

11th practice

Julia likes to study code style blogs to help better performance on Leetcode 125:

1. if-else discussion on stackexchange.com

2. fail fast

3. a branched arrow head antipattern

4. https://blog.codinghorror.com/flattening-arrow-code/

5. Refactoring: Book: "improving the Design of Existing Code" - if-else structure sections

6. http://programmers.stackexchange.com/questions/167607/how-can-i-reformat-my-condition-to-make-it-better?noredirect=1&lq=1

7. http://stackoverflow.com/questions/1364946/what-is-the-highest-cyclomatic-complexity-of-any-function-you-maintain-and-how

8. A lot of discussions:
http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement

9. Guard clauses

10. Try this tool - static analysis tool

Study notes:
Get some keywords for google search, or ideas to help review of function:

1. inconsistent levels of abstraction
2. prevent inclusion in if
3. two calls to one function violate DRY principle

Execution paths concern: 
1. multiple returns - avoid it
2. only return from a function in one spot  - (Good ? Bad?)
     Julia made a terrible mistake in 2 sum algorithm, about the return issue - took more than 20 minutes to find out return issue.
3. multiple return paths
4. early returns help to avoid the arrowhead anti pattern.

Readable code

1. use a flag variable to avoid giant expression/ gross if statement.
2. orthogonal ?
3. early returns help to avoid the arrowhead anti pattern.
4. flattening arrow code
5. cyclomatic complexity value - execution paths
6. reduce a module's cyclomatic complexity make code easy to test

Avoid If statement
1. replace conditions with guard clauses.
2. decompose conditional blocks into separate functions
3. convert negative checks into positive checks
4. always opportunistically return as soon as possible from the function.
5. the goal to make code more flat, scroll vertically not horizontally  

Book "code complete" about cc - cyclomatic complexity value:
If the score is:
  • 0-5 - the routine is probably fine
  • 6-10 - start to think about ways to simplify the routine
  • 10+ - break part of the routine into a second routine and call it from the first routine
Might be a good idea to write unit tests as you break up the original routine.

A quick comparison image:
Leetcode 125 8th practice vs 9th practice on the change - if conditions inside a while loop:


Continue to review solutions on Leetcode 125. (August 22, 2016)

1. study code: 
C++ code - valid palindrome

extract one more function - removeNoise - practice #10 

Julia wrote a C# version

Great idea to extract one more function, SRP - single responsibility principle 
- make the code much more easy to follow, test, static analysis

Two tasks - put into one function - think if the function is designed for more than 1 task. 
Both are with time complexity O(N), N is the string length; 
Two tasks can be separated easily, task 1 can be done first. 

 1. remove noise
 2. compare chars 

Easily, combine the above 1 and 2 cases, there are 4 cases in the function to handle. 

2. use continue keyword -  practice #11

study code: 
using continue - C++ valid palindrome

Julia wrote a C# solution




No comments:

Post a Comment