Fish Shell:
Refactoring
How to:
Imagine you’ve got a script that’s grown quite a bit over time. It started simple, but now it’s a beast sprawling with tentacles of logic. Here’s a bite-sized example of refactoring a function to be more legible and efficient:
Before refactoring:
function old_and_clunky
set color (cat ~/.config/fish/color_theme)
if test "$color" = 'blue'
echo 'Blue theme set!'
else if test "$color" = 'red'
echo 'Red theme set!'
else
echo 'Default theme set!'
end
end
After refactoring:
function set_theme_color
set theme_color (cat ~/.config/fish/color_theme)
switch $theme_color
case blue
echo 'Blue theme set!'
case red
echo 'Red theme set!'
default
echo 'Default theme set!'
end
end
Refactoring improved the function’s name to better describe its purpose and replaced the if-else chain with a cleaner switch
statement.
Sample output:
Blue theme set!
Deep Dive
Refactoring was first described in detail in Martin Fowler’s seminal book “Refactoring: Improving the Design of Existing Code”. The book laid out a structured approach to improving code without writing new functionality. Many refactoring techniques have been introduced since then, and the concept has become a fundamental part of modern software development.
In the Fish Shell environment, refactoring might look slightly different than in other programming contexts due to its specialized syntax and command-line nature. Alternatives to refactoring scripts in Fish could involve porting to another shell language or using external tools for more advanced script management. However, keeping the native Fish syntax often means better integration with the shell’s features and a more streamlined experience overall.
When refactoring in Fish Shell, you’re mostly dealing with functions and commands as opposed to wide-scope classes or modules common in other languages. This granularity can make the task of refactoring a more immediate and direct process, but it also emphasizes the importance of clear, concise, and maintainable code.
See Also
- Martin Fowler’s Refactoring website: https://refactoring.com/
- Official Fish Shell documentation: https://fishshell.com/docs/current/index.html