Monday, April 29, 2024
HomeRuby On RailsThe way to develop into 10x developer with a assist of ChatGPT

The way to develop into 10x developer with a assist of ChatGPT


Is being a 10x developer nonetheless a factor? Traits change, presently there’s quite a lot of chatter round whether or not LLMs will take our jobs, sorry couldn’t stand this South Park reference.

I by no means permit Copilot to write down the total implementation for me, however its contextual options are getting higher and higher on daily basis. Fairly tempting to make use of it, proper? I consider that fixing issues written mindlessly with put–your–favourite–llm–mannequin–right here shall be a giant market within the close to future. It’s not a lot completely different from fixing issues coming from StackOverflow, however the opponent is likely to be tougher to defeat. Time will inform.

Nonetheless, there are couple eventualities the place instruments backed with LLMs shine and may increase your productiveness.

Writing verbose items of code, like exams

I want self–contained check instances, not depending on shared setup and micro optimizations coming from poorly understood DRY precept. GitHub’s Copilot shines in such state of affairs. Let me share current instance of check which I added when implementing a worth object in RailsEventStore:

specify "adapter_name" do
  count on(DatabaseAdapter::PostgreSQL.new.adapter_name).to eql("postgresql")
  count on(DatabaseAdapter::MySQL.new.adapter_name).to eql("mysql2")
  count on(DatabaseAdapter::SQLite.new.adapter_name).to eql("sqlite")
finish

First expectation was written by me, two different had been prompt by Copilot, so I’ve hit tab.

Identical goes for this instance:

specify "MySQL would not assist jsonb" do
  count on { DatabaseAdapter::MySQL.new("jsonb") }.to raise_error(InvalidDataTypeForAdapter, "MySQL would not assist "jsonb". Supported varieties are: binary, json.")
finish

It was written solely by a Copilot, primarily based on different already current examples for various adapter varieties.

This hastens my work, why not make a use of it?

Writing utility snippet with a library you hardly ever use

I lately needed to discover all occurrences of code like within the snippet under:

TRANSLATIONS = { 
    foo: t('foo'),
    bar: t('bar'),
    baz: t('baz'),
}

Sure, calls to I18n library api, from inside Hash assigned to a relentless. Nice optimization, but it surely hadn’t had an opportunity to work in request–response cycle the place it was used. I’ve might attempt to discover this code with fastidiously crafted Regex, spending half a day on it. I shortly discovered that the quickest means it might be to make use of glorious Parser gem for that goal and discover this code occurrences inside Ruby’s Summary Syntax Tree. I don’t use this library each day, fairly every so often. Most likely I’ve would written desired code inside half an hour. As an alternative I’ve requested ChatGPT to write down this snippet for me. In pure language. It took me 3 minutes to have code in a form which solved my downside — 1/tenth of a time. Can I name myself 10x developer now? See the way it went:

— Write me a code snippet utilizing Ruby Parser gem for AST parsing to get all of the occurrences of hashes assigned to constants. These hashes have methodology t with string argument as values.

— Positive! You need to use the parser gem in Ruby to parse Ruby code and traverse the Summary Syntax Tree (AST) to search out particular patterns. On this case, you’re in search of hashes assigned to constants, the place the values of the hash are calls to a technique t with a string argument.

Beneath is a code snippet that does simply that. You’ll want to put in the parser gem should you haven’t already.

require 'parser/present'

code = <<-RUBY
  MY_CONSTANT = { key1: t('value1'), key2: t('value2') }
  ANOTHER_CONSTANT = { key: 'worth' }
  MY_SECOND_CONSTANT = { key3: t('value3'), key4: t('value4') }
RUBY

buffer = Parser::Supply::Buffer.new('(string)')
buffer.supply = code

parser = Parser::CurrentRuby.new
tree = parser.parse(buffer)

# Outline customer to deal with nodes
class Customer
  def go to(node)
    case node.sort
    when :casgn
      handle_casgn(node)
    finish
    node.youngsters.every  go to(baby) if baby.is_a?(AST::Node) 
  finish

  def handle_casgn(node)
    constant_name = node.youngsters[1]
    value_node = node.youngsters[2]

    if value_node.sort == :hash
      value_node.youngsters.every do |pair|
        key, worth = pair.youngsters
        if worth.sort == :ship && worth.youngsters[1] == :t
          argument = worth.youngsters[2]
          if argument.sort == :str
            places "Discovered hash assigned to fixed #{constant_name}: #{key.youngsters[0]} => t('#{argument.youngsters[0]}')"
          finish
        finish
      finish
    finish
  finish
finish

# Go to the AST
Customer.new.go to(tree)

— Print the filename and line of code moreover

require 'parser/present'

filename = 'instance.rb'
code = File.learn(filename)

buffer = Parser::Supply::Buffer.new(filename)
buffer.supply = code

parser = Parser::CurrentRuby.new
tree = parser.parse(buffer)

# Outline customer to deal with nodes
class Customer
  def initialize(filename)
    @filename = filename
  finish

  def go to(node)
    case node.sort
    when :casgn
      handle_casgn(node)
    finish
    node.youngsters.every  go to(baby) if baby.is_a?(AST::Node) 
  finish

  def handle_casgn(node)
    constant_name = node.youngsters[1]
    value_node = node.youngsters[2]

    if value_node.sort == :hash
      value_node.youngsters.every do |pair|
        key, worth = pair.youngsters
        if worth.sort == :ship && worth.youngsters[1] == :t
          argument = worth.youngsters[2]
          if argument.sort == :str
            line_number = node.location.line
            line = File.readlines(@filename)[line_number - 1].strip
            places "Present in #{@filename}, line #{line_number}: #{line} - #{constant_name}: #{key.youngsters[0]} => t('#{argument.youngsters[0]}')"
          finish
        finish
      finish
    finish
  finish
finish

# Go to the AST
Customer.new(filename).go to(tree)

— Might enhance it so it takes a number of directories as argument from command line?

require 'parser/present'
require 'discover'

# Outline customer to deal with nodes
class Customer
  def initialize(filename)
    @filename = filename
  finish

  def go to(node)
    case node.sort
    when :casgn
      handle_casgn(node)
    finish
    node.youngsters.every  go to(baby) if baby.is_a?(AST::Node) 
  finish

  def handle_casgn(node)
    constant_name = node.youngsters[1]
    value_node = node.youngsters[2]

    if value_node.sort == :hash
      value_node.youngsters.every do |pair|
        key, worth = pair.youngsters
        if worth.sort == :ship && worth.youngsters[1] == :t
          argument = worth.youngsters[2]
          if argument.sort == :str
            line_number = node.location.line
            line = File.readlines(@filename)[line_number - 1].strip
            places "Present in #{@filename}, line #{line_number}: #{line} - #{constant_name}: #{key.youngsters[0]} => t('#{argument.youngsters[0]}')"
          finish
        finish
      finish
    finish
  finish
finish

def process_file(filename)
  code = File.learn(filename)
  buffer = Parser::Supply::Buffer.new(filename)
  buffer.supply = code

  parser = Parser::CurrentRuby.new
  tree = parser.parse(buffer)

  # Go to the AST
  Customer.new(filename).go to(tree)
finish

def process_directory(listing)
  Discover.discover(listing) do |path|
    if FileTest.listing?(path)
      subsequent
    else
      if path.end_with?('.rb')
        process_file(path)
      finish
    finish
  finish
finish

if ARGV.empty?
  places "Please present a number of listing paths to seek for Ruby information."
else
  ARGV.every do |listing|
    if File.listing?(listing)
      process_directory(listing)
    else
      places "#{listing} is just not a sound listing."
    finish
  finish
finish

Due to that I used to be in a position to quickly find fallacious code residing in app/ and lib/ directories of the applying I used to be engaged on at the moment.

Dealing with communication points

The scenario has acquired fairly tense and also you begin performing like Gordon Ramsay? There it goes:

— Write “you f*cking donkey” in a pleasant method

— Actually! A extra well mannered option to specific the sentiment could possibly be “You’ve made a mistake.”

— How would I say that to a coworker?

— If you want to handle a difficulty with a coworker, it’s essential to be respectful {and professional}. You may say one thing like:

“Hey [Name], it seems to be like there was a small error right here. Might we focus on the best way to appropriate it?”

This fashion, you convey consideration to the error with out being confrontational, permitting for a constructive dialog.

You’ve simply prevented assembly with HR. Fairly good, isn’t it?

I’ve tried to search for a humorous instance. Clearly you possibly can have interaction LLM for a extra critical use like in search of arguments in assist of the thesis you’re attempting to advance. Or higher understanding area you’re engaged on should you don’t have area professional subsequent to you.

What’s your tackle that? Hit me up on X, I’m comfortable to debate different use–instances.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments