awalsh3 / books

Mentor Challenge Project - API powered by Rails. An API that serves data about Books.
0 stars 0 forks source link

Learn Rails 12. Refactor 2. #26

Closed patrickquigley102 closed 1 year ago

patrickquigley102 commented 1 year ago

Overview of this refactor series.

In this series of refactor issues, we'll be exploring writing code in a consistent manner.

That consistent manner, that style, is what I use at work. As that's what I know. We'll follow it in our work together but, take/leave parts you wish to in your own work :slightly_smiling_face: As, consistency is more important that what style it actually is.

Some of this we'll pair on, some you'll do solo.

Linters

Code style. Easily a topic you could waste your entire life having opinions about.

The benefit of code style is having consistency between authors, so it's easier to read others code.

So the only thing that matters is consistent style, not what style. In my opinion.

Overview of these refactor issues.

Notice how I've broken this down into steps, steps that are very small. Always worth doing.

  1. Rubocop using default community configuration & passing locally.
  2. Rubocop setup to run in your editor.
  3. Rubocop running on each PR you push to GitHub.
  4. More to follow as I go!

Linters. Rubocop!

We've Rubocop setup in our project, lovely.

Rubocop does much heavy lifting for us. It enforces style and is highly configurable.

Refactor 2. Your editor.

You can get Rubocop to run on the fly, when editing a single file in your editor. Wooo, what a world we live in.

I've no idea which editor you use dude, so I'll take a guess :sweat_smile:

I'll share the details for Visual Studio: https://marketplace.visualstudio.com/items?itemName=rubocop.vscode-rubocop

Regardless of which editor you use, there will be a way to setup an equivalent to the above.

The best feature is the ability to autocorrect with Rubocop on file save :exploding_head: You can stop spending any time worrying about identation of characters, just save and see pretty code!

Set this up for your editor and report back your thoughts! Then we can close this issue.

If you get stuck, reach out an we can pair on this

awalsh3 commented 1 year ago

So I've got this working (sort of!) I have to press shift cmd b for it to apply in Sublime text. Autocorrect on Save would be ideal but I am not sure it works in that text editor. I am having difficulty finding the right config for it (if there is one!?)

I was also considering switching text editors. Do you have a preference?

patrickquigley102 commented 1 year ago

I use Vim, don't use Vim. It took years to get as quick and it is really cool. BUT years to be just as productive as before, I imagine you'd have to invest a lot of time to actually 'edit text as quick as thought' as it like to sell itself.

I say stick to what you know, they're all roughly the same. And never the thing slowing you down!

Vim is cool though. VERY backend nerdy.

patrickquigley102 commented 1 year ago

Sublime Text

You can use the following. I used to use Sublime Text btw! I used these things once.

Here is a blog post about linting generally that you can adapt: https://haydar-ai.medium.com/how-to-start-using-linter-on-sublime-text-285dfde38715

awalsh3 commented 1 year ago

Finally got this working! Had to write a Sublime Text plugin in python.

import sublime
import sublime_plugin
import subprocess
import os

class RubocopAutocorrectOnSaveCommand(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        filename = view.file_name()
        if filename.endswith('.rb'):
            command = ['rubocop_autocomplete_on_save', '-a', filename]
            subprocess.run(command, cwd=os.path.dirname(filename))
patrickquigley102 commented 1 year ago

Nice work Annie, I am impressed.

I think it'll pay off in terms of efficiency with a month! Never have to worry about indentation again, wahoo!

patrickquigley102 commented 1 year ago

Now, extra credit.

Put this on stack overflow (ask then answer your own question). Share with the world and get internet points?