Tech and Digital Media

Sunday, December 4, 2022

[New post] GitHub Copilot reduces writing Java boilerplate to two simple comments

Site logo image Lucas Jellema posted: "GitHub Copilot helped me write a Java application to process a CSV file. Not a hard task – though one that in Java always feels much harder than in Python or Node. This time round -  because of Copilot – all I have to was write two simple instruction" AMIS, Data Driven Blog - Oracle & Microsoft Azure

GitHub Copilot reduces writing Java boilerplate to two simple comments

Lucas Jellema

Dec 4

GitHub Copilot helped me write a Java application to process a CSV file. Not a hard task – though one that in Java always feels much harder than in Python or Node. This time round -  because of Copilot – all I have to was write two simple instructions (and accept the appropriate suggestion from my pAIred programmer. See the result in this GitHub Repo.

image

These two instructions were:

  • class for loading a csv file and storing data in a list of maps
  • print names of columns

Let me show you the steps in more detail.

Get going with Copilot

First I have to get going with Copilot:  After installing the VS Code extension for Copilot

Install GitHub Copilot extension Visual Studio Code

and signing up for Copilot (for the free 60 day trial to start with) - as per the instructions I have made my first few steps with Copilot in VS Code to see if it can be a useful AssIstant while programming - a veritable pAIr programmer.

Assemble Java Application to process a CSV Data File

The first trial - create a simple Java application. That is: a Java application for a simple task that requires more cumbersome steps than a comparable Node or Python program. Or at least, it feels that way. Read a CSV file and turn the data into an in memory structure that can easily be processed.

To create the Java class to process the CSV file

  • create a file called DataProcessor.java in directory `javapilot`
  • type `// class for loading a csv file and storing data in a list of maps`
  • press Ctrl+Enter

image

Select the first solution from the list that is presented in the GitHub Copilot tab that is opened. Position the cursor at the end of the pasted code. Press Ctrl+Enter to get suggestions for how to proceed.

image

Accept the first proposal - to add methods `getData` and `getColumnNames` and method `main`

Create a sample CSV File with Country Data

To create the CVS file with country data:

  • create a file called data.csv
  • type `// records for countries with comma separated values for country name, language, capital and population`
  • press Ctrl+Enter

image

Accept the first solution

Some post processing is required, to remove // Example: " and " + newline

image

Accept the suggestion from Copilot for the first line with column names. Remove original comment in the first line.

Run the Java class. The output presents the country data from the CSV as processed by the Java application.

image

It would be nice to also have the names of the columns printed to the output. To make that happen:

  • add a comment in method `main`, just  before the `for` loop:  `// print names of columns`
  • press Ctrl+Enter
  • accept first solution

image

Run the Java class again. Now the column names are there as well.

image

Conclusion

with typing all of two suggestions after creating an empty Java file:

  • // class for loading a csv file and storing data in a list of maps
  • // print names of columns

and making use of Ctrl+Enter three times to get code fragments for the application I was able to compose a Java program that does what I set out to do. Not being a fluent Java programmer these days, I could have done this myself but it would have taken much longer (if only for all the typos I am apparently adding to any text or code I type) and probably resulted in far more frustration (with myself, with Java, with the IDE, with the world).

One additional comment in an empty csv file was required to produce a sample csv file with country records:

  • // records for countries with comma separated values for country name, language, capital and population

after accepting the proposal from Copilot I had to do a little bit of refining the file in order to make it usable. In fairness, Copilot suggested a few ready to roll CSV files from GitHub.

Note: if you were to try the exact same steps that I went through, you still may get different results. Copilot is constantly evolving and suggestions can differ on that evoluti0n as well as the context in which it is used (and on factors that perhaps no one quite understands)

Appendix: Download CSV data from URL

Let's try to create a Java application that gets its CSV file from GitHub and does similar processing as done before.

  • create an empty file called DataProcessor2.java
  • add comment // class for downloading a csv file from GitHub and storing data in a list of maps
  • press Ctrl+Enter
  • select the third solution from the list that is presented in the GitHub Copilot tab that is opened

image

The final result is very much like the previous DataProcessor. There are three differences:

1. the input parameter to the constructor is called *url*

2. the InputStreamReader is creasted from a URL object

image

3. the value passed into the constructor should be a URL rather than a file location

Comment

Unsubscribe to no longer receive posts from AMIS, Data Driven Blog - Oracle & Microsoft Azure.
Change your email settings at manage subscriptions.

Trouble clicking? Copy and paste this URL into your browser:
https://technology.amis.nl/software-development/software-engineering/github-copilot-reduces-writing-java-boilerplate-to-two-simple-comments/

Powered by Jetpack
Download on the App Store Get it on Google Play
at December 04, 2022
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

[New post] ‘Everyone Is Freaking Out’: Disney Explores Sale of ABC Network and Stations Amid Financial Challenges

...

  • [New post] Xiaomi’s Mi Smart Band 6 NFC is finally available in Europe officially
    Tech News For Today posted: "Xiaomi's Mi Smart Band 6 NFC is finally available in Europe officially At Xiaomi's bi...
  • [New post] ‘Everyone Is Freaking Out’: Disney Explores Sale of ABC Network and Stations Amid Financial Challenges
    ...
  • [New post] Things to Keep in Mind When Creating a Health Mobile App | HackerNoon
    Techi...

Search This Blog

  • Home

About Me

Tech and Digital Media
View my complete profile

Report Abuse

Labels

  • 【ANDROID STUDIO】navigation
  • 【FLUTTER ANDROID STUDIO and IOS】backdrop filter widget
  • 【GAMEMAKER】Scroll Text
  • 【PYTHON】split train test
  • 【Visual Studio Visual Csharp】Message Box
  • 【Visual Studio Visual VB net】Taskbar properties
  • 【Vuejs】add dynamic tab labels labels exceed automatic scrolling

Blog Archive

  • September 2023 (502)
  • August 2023 (987)
  • July 2023 (954)
  • June 2023 (1023)
  • May 2023 (1227)
  • April 2023 (1057)
  • March 2023 (985)
  • February 2023 (900)
  • January 2023 (1040)
  • December 2022 (1072)
  • November 2022 (1145)
  • October 2022 (1151)
  • September 2022 (1071)
  • August 2022 (1097)
  • July 2022 (1111)
  • June 2022 (1117)
  • May 2022 (979)
  • April 2022 (1013)
  • March 2022 (982)
  • February 2022 (776)
  • January 2022 (681)
  • December 2021 (1197)
  • November 2021 (3156)
  • October 2021 (3212)
  • September 2021 (3140)
  • August 2021 (3271)
  • July 2021 (3205)
  • June 2021 (2984)
  • May 2021 (732)
Powered by Blogger.