Code Less and Ship faster with OpenAI

Talk at saigon.rb on 21 March 2023.

How AI makes me better

3 use cases: LLM fill in the gaps where I...

  1. am not good at
  2. have no time for
  3. am lazy of forgetful

Bonus: Use API for text + creativity only, not for right answers

  1. am not good at
  2. no time
  3. am lazy of forgetful
  1. am not good at = Frontend
  2. no time = ?
  3. am lazy of forgetful = ?
  1. am not good at = Frontend
  2. no time = Tests & Refactoring
  3. am lazy of forgetful =
  1. am not good at = Frontend
  2. no time = Tests & Refactoring
  3. am lazy of forgetful = Rails API

1. Do frontend work

I have this ViewComponent in Rails and I use stimulus. I want to hide all this code behind a box that looks like a dropdown box. If any tag is included (selected_values contains one), turn the select box green. Else gray.
Only use stimulus and tailwind and Rails/ERB.

<%= helpers.turbo_frame_tag "tag_filter" do %>
... (all my ERB code)

1-2 Tweaks

make the dropdown button look more like a dropdown menu with an upside / downside angle icon

rotate the angle up then the dropdown is expanded

Results


Example "make it pretty"

This form looks ugly. Please make it pretty! [code]

2a. Write tests faster

GPT4: Just throw it all in!

rake prompts:copy folder_path=app/models | pbcopy

Write me an integration test for this Rails data model:

Ruby file: /app/models/api_key.rb
class ApiKey < ApplicationRecord
  include Discard::Model
...
Ruby file: /app/models/user.rb 
... (+contents)
Ruby file: /app/models/event.rb ...
... (+contents)

This is not an integration test :(

However, this works

Give me some ideas for integration tests that test the collaboration of these models.

...
Test stock changes.
    Test creating stock changes with different kinds 
    (e.g., add, remove).
    Test validation for presence and numericality of 
    quantity and initial_quantity.
...

Take #9 and code it in rspec.

2b. Refactoring

refactoring

I have 2 Rails ViewComponents, which filter in the view. They save via hotwire into the session.

How can I refactor this to build reusable filter components and not clutter my controller?
Here's the controller:

controller code

Result: great!

This has been done many times before

Reviews my structure:

This is my data model in Rails. Do you see any problems with it?

3a. Code lazily

Copilot works great for contained methods and algorithms. Not great for structures.

3b. When I forget the API

write me the relations in the model StockChange corresponding to this:

Result:

4. Use it as a general API

I had hope for GPT-4 :(

Maybe this is not the use case after all

Great for creative things

Figured out tokenization:

Split this into Vietnamese words that make sense. For every correct word use the format {"vi": <word>, "en": <English translation>} : " Tôi có một ..."

Conclusion

  • AI is not "Intelligent". It only knows how to figure out the next word, based on a lot of inputs.
  • You won't get novel ideas. But you get over the boring stuff, fast.
  • Think: What can you build with it?

Photo by Zac Wolff.
2023, TillCarlos.com.

Photo by <a href="https://unsplash.com/@zacwolff?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Zac Wolff</a> on <a href="https://unsplash.com/photos/rv2ooDQuNuI?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

```ruby class Rental < ApplicationRecord belongs_to :item belongs_to :renter, class_name: 'Person', foreign_key: 'renter_id' belongs_to :checkout, class_name: 'StockChange', foreign_key: 'checkout_id' belongs_to :return, class_name: 'StockChange', foreign_key: 'return_id', optional: true ```