Find unused local variables in ruby project

Inspired by SublimeLinter plugin for ruby, using ruby -wc

1
parallel "ruby -wc {}" ::: $(find . -name \*.rb) 1> /dev/null

Known issue

it only detects local variables for “warning: assigned but unused variable - xxx”

not working for instance variables

probably work with unused-code/unused to detect other unused tokens

unused

1
2
3
4
5
6
7
8
# https://github.com/universal-ctags/homebrew-universal-ctags
brew install --HEAD universal-ctags/universal-ctags/universal-ctags
brew install unused --with-mimalloc

alias ctags='/usr/local/bin/ctags'

# go to project working dir
ctags --recurse . && ls -lh tags && unused

Linus torvalds talking about good taste

“The mind behind Linux | Linus Torvalds”

Sometimes you can see a problem in a different way and rewrite it so that a special case goes away and becomes the normal case. And that’s good code.

To me , the sign of good people I really want to work with is that they have good taste.

This is a small example , Good Taste is much bigger than this.

Good taste is about really seeing the big patterns, and kind of instinctively and knowing what’s the right way to do things.

install ruby-3.2.0 on macos

Background

Steps

1
2
3
4
5
6
7
rvm reinstall "ruby-3.2.0" --with-openssl-dir=`brew --prefix openssl@3` # not ok, [ruby don't support openssl 3 yet.](https://github.com/ruby/openssl/issues/369)

brew uninstall openssl@3
brew reinstall openssl@1.1

# new shell
rvm reinstall "ruby-3.2.0" --with-openssl-dir=`brew --prefix openssl@1.1` --disable-binary # ok

Other ref

Reading List of 2022

  • review: unix-processes 15 * 15 min = 225 min(done 20220517)

  • read: 元编程 240 页 / (10页/15 min) = 360 min(done 20220521)

  • listen/review: 史蒂夫乔布斯传 (76 - 18 + 1) * 20 min = 1180 min(done 20220524)

  • read: 暗时间 244 页 / (10页/15 min) = 366 min(done 20220623)

  • read: CSAPP 730 页 / (10页/20 min) = 1460 min(ff)(done 20220512 -> 20220925)

  • review: 现实一种(余华) (done 20220925 -> 20221001)

  • read: unix编程艺术 (done 20200704 -> 20220924)

  • read: refactoring (done 20220929 -> 20221012)

  • read: DDD (done 20221107 -> 20221213)

Testing and Refactoring sharing

  • Bi-weekly Tech Sharing

Agenda

  • Experience
  • Testing
  • Refactoring
  • References

Experience

  • best thing learned this year

  • I’m proud that we have testing culture and code review

  • how I test my code before?

    • coding -> run program -> postman / repl -> verify by eye -> QA
  • how I test my code now?

    • coding -> add tests -> green -> refactoring -> coding -> repeat -> code review
  • hopefully I’ll benefit from TDD style sooner

Testing

  • raise a question: we’re relying on tests to guarantee code work as expected, who guarantees our tests are correct then?

    • short answer: we can only rely on simple tests
    • too simple to be wrong(ideal world)
  • testing proportion

  • testing helped on

    • building confidence
    • building solid(bug free) code as possible
    • gives me a button to click
      • “if you ever get that button, you’ll never want to lose it or work other way round so you wouldn’t have it.”
  • testing drives me to write simple code

    • I want class to be small
    • I want responsiblilty to be single/simple/clear
    • I want method to be short
    • I want interface to be well defined
    • I love good names
  • example of using let(private git repo pull request)

    • demo for independant tests
    • avoid mutable variable
    • discipline posed upon assignment/mutability
  • key points:

    • write simple code & test when possible
    • more unit tests
    • “write enough tests to be confident”

Refactoring

  • let’s put performance aside while coding & refactoring

  • let’s focus on clarity/simplicity/readability/maintainability, not performance

    • make it simple
    • make it easier to read/review
    • make it easier to change/maintain
  • How I do code refactoring?

    • clean up feature flags
    • check code coverage and make it up to 100%
    • remove code smell
    • simplify code
    • enjoy the tiny changes/improvements
    • enjoy the green dots
    • enjoy the red dots(it shows I didn’t fully understand my code)
    • remove unused/dead code(unused)
    • check with flog(rely on metric tools, not “feeling”)
    • choose a better name for variables/methods/class/file/moudles
    • simplify tests
    • I got better understanding of the code, and I can repeat the above process…
  • example of simplifying code(private git repo pull request)

  • The point is: “business complixity is the lower limit of system complixity”

    • so I want my code to be as simple/readable as possible
    • try functional thinking: e.g. use filter/map/reduce
    • ruby: Enumerable module is amazing
  • key points:

    • lean on tests, no tests, no refactoring
    • do your refactoring with small steps(highlighted in both “refactoring” and “99 bottles of OOP”)
    • refactoring is just pattern matching(focus on code smell) and follow the solution

References

At last

these are just my limited experience on testing & refactoring; I’m pretty sure I have a lot more to learn

I really want to learn from everyone about these two topics

pls correct me if I’m wrong. pls discuss with me if you’re interested~ Orz

A leading space in command wastes lots of my time

This one doesn’t have a TL;DR

Background

What I did

  • I wrote two specific shell aliases for rspec, re for running specs without error backtraces, ree for with error backtraces

    1
    2
    alias re="DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile"
    alias ree="DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile --backtrace"
  • One day, I found it looks better if I change them to this(alignment):

    1
    2
    alias re=" DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile"
    alias ree="DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile --backtrace"

  • Later, I forgot the change

  • Somehow, I find the frequently used re command never got remembered by type re and press the up arrow(which acts as searching from history in OMZ)

Trouble shooting

  • I didn’t think too much about what I’v recently changed
  • I thought I screwed up my OMZ environment, so I keep creating new terminal session, didn’t work
  • I thought my history file is too big, so I cleared the history file, didn’t work
  • I put this issue aside for a few days without any solution, suffering from typing/copy-paste the frequently used rspec commands again and again
  • A few days later, I can’t stand it anymore, and at this time, I found that only that rspec related command won’t be remembered by history!(still didn’t connect to previous changes…)
  • Until second day when I tried to optimize my OMZ plugin, I noticed the git history: THERE IS A LEADING SPACE IN MY RSPEC ALIAS!!! because of the code alignment

The reason

The fix

  • Remove the leading space in shell alias, simple enough

Learnings