Maximizing Efficiency with ChatGPT: Tips for Software Engineers

What is ChatGPT?

ChatGPT is a text-based chatbot software application that was released in November 2022.

The application has attracted a vast amount of media attention because of its advanced capabilities, its human-like responses to requests (known as ‘prompts’) and its ability to write and debug code.

Will Chat GPT replace software engineers?

No. The prospect of an AI chatbot being able to write complex code and debug software seems to be a cause for concern among software engineers.

However, early testing of these capabilities reveals drawbacks and inconsistencies in how the chatbot writes code. The strengths of ChatGPT are quickly completing time-consuming tasks such as debugging code, creating common components, generating dummy data and writing unit tests.

Rather than a threat to traditional software engineering practices, we at FusionHit see this development as an opportunity that benefits our customers.

The strengths of ChatGPT

Although still in its early stages, ChatGPT is strongest in areas such as project management, project design, testing, and assessment. While it cannot create a complete web or mobile application, ChatGPT’s influence on software engineering goes beyond basic coding.

ChatGPT has the potential to revolutionize the daily routines of software engineers. The chatbot can assist with tasks such as code generation, bug fixing, natural language processing, and other forms of automation. It can also answer technical questions, explain programming concepts, debug and troubleshoot code, and provide research on new technologies and best practices. By offering support in these areas, ChatGPT can help software engineers to be more efficient and effective in their work.

How software engineers may use ChatGPT

As software engineers, we are constantly seeking ways to streamline our work and improve our productivity. With the introduction of cutting-edge technologies such as OpenAI’s ChatGPT, the possibilities for achieving this goal have greatly expanded. In this post, we’ll explore some ways software engineers can use ChatGPT to make their work easier, faster, and more accurate.

Code generation and completion
ChatGPT can be used to generate code snippets and complete existing code, significantly reducing the time and effort required to write code from scratch. This feature can also improve code accuracy, reducing the risk of bugs and errors. Simply provide ChatGPT with the desired code syntax, and it will generate the code for you.

Suppose you want to generate a Python function that takes a list of numbers and returns the sum of the squares of those numbers. You can provide the following prompt to ChatGPT:

Generate a Python function that takes a list of numbers and returns the sum of the squares of those numbers

ChatGPT may respond with the following code:

def sum_of_squares(numbers):
    return sum([num ** 2 for num in numbers])

As you can see, ChatGPT has generated a Python function that meets the requirements specified in the prompt. This simple example demonstrates how ChatGPT can be used to generate code snippets and save you time.

Documentation

ChatGPT can help software engineers quickly generate technical documentation for their projects. Whether it’s a readme file, API reference, or design document, ChatGPT can produce accurate and well-structured documents, saving engineers time and allowing them to focus on more critical tasks.

Suppose you have a Java class and you want to document its functionality using ChatGPT.

 public ClassicsLibrary() {
        books = new TreeMap<>();
        Book nineteenEightyFour = new Book("Nineteen Eighty-Four", "George Orwell",
                "It was a bright cold day in April, and the clocks were striking thirteen.");
        Book theLordOfTheRings = new Book("The Lord of the Rings", "J. R. R. Tolkien",
                "When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating");
        Book cleanCode = new Book("Clean Code", "Robert C. Martin",
                "ClassicsEven bad code can function. But if code isn’t clean, it can bring a development organization to its knees");

        books.put("Nineteen Eighty-Four", nineteenEightyFour);
        books.put("The Lord of the Rings", theLordOfTheRings);
        books.put("Clean Code", cleanCode);
    }

You can provide the following prompt:

"create documentation for this function in Java" 

 public ClassicsLibrary() {
        books = new TreeMap<>();
        Book nineteenEightyFour = new Book("Nineteen Eighty-Four", "George Orwell",
                "It was a bright cold day in April, and the clocks were striking thirteen.");
        Book theLordOfTheRings = new Book("The Lord of the Rings", "J. R. R. Tolkien",
                "When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating");
        Book cleanCode = new Book("Clean Code", "Robert C. Martin",
                "ClassicsEven bad code can function. But if code isn’t clean, it can bring a development organization to its knees");

        books.put("Nineteen Eighty-Four", nineteenEightyFour);
        books.put("The Lord of the Rings", theLordOfTheRings);
        books.put("Clean Code", cleanCode);
    }

ChatGPT may respond with the following code:

/**
 * ClassicsLibrary is a class that represents a library of classical books.
 * It contains a collection of books stored in a TreeMap.
 */
public class ClassicsLibrary {
    private TreeMap<String, Book> books;

    /**
     * Constructor that initializes the books collection by adding three books to it.
     */
    public ClassicsLibrary() {
        books = new TreeMap<>();
        Book nineteenEightyFour = new Book("Nineteen Eighty-Four", "George Orwell",
                "It was a bright cold day in April, and the clocks were striking thirteen.");
        Book theLordOfTheRings = new Book("The Lord of the Rings", "J. R. R. Tolkien",
                "When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating");
        Book cleanCode = new Book("Clean Code", "Robert C. Martin",
                "ClassicsEven bad code can function. But if code isn’t clean, it can bring a development organization to its knees");

        books.put("Nineteen Eighty-Four", nineteenEightyFour);
        books.put("The Lord of the Rings", theLordOfTheRings);
        books.put("Clean Code", cleanCode);
    }
}

Problem-solving

ChatGPT can assist software engineers with problem-solving, whether it’s a technical challenge or a bug in their code. Simply ask ChatGPT to analyze the problem and provide possible solutions, and it will generate a list of suggestions and recommended next steps.

Let’s use this sample code that we know has a known issue related to variable initialization and finds the highest number in a list of numbers. When the function is called with the list [1, 2, 3, 4, 5] as an argument, it returns 5, which is the maximum number in the list:

def find_max_value(numbers):
    max_value = 0
    for num in numbers:
        if num > max_value:
            max_value = num
    return max_value

print(find_max_value([1, 2, 3, 4, 5])) # Output: 5
print(find_max_value([5, 4, 3, 2, 1])) # Output: 5 (incorrect, expected output is 5)

You can provide the following prompt:

"Analize the problem in this code and provide solutions"

def find_max_value(numbers):
    max_value = 0
    for num in numbers:
        if num > max_value:
            max_value = num
    return max_value

print(find_max_value([1, 2, 3, 4, 5])) # Output: 5
print(find_max_value([5, 4, 3, 2, 1])) # Output: 5 (incorrect, expected output is 5)

ChatGPT may respond with the following:

"The problem with the code is that the initial value of 'max_value' is set to '0', which can be a problem if the input list 'numbers' contains negative numbers.

"Here are two solutions to solve this issue:

"1. Initialize 'max_value' with the first element of the input list 'numbers':

    def find_max_value(numbers):
        max_value = numbers[0]
        for num in numbers:
            if num > max_value:
                max_value = num
        return max_value
      
"2. Use the built-in 'max' function to find the maximum value in the input list 'numbers':

    def find_max_value(numbers):
        return max(numbers)

Create Unit Testing

ChatGPT can help developers with generating unit tests by providing code snippets and examples, along with suggestions on how to effectively write unit tests for specific programming languages or frameworks. However, the actual implementation of unit tests is a crucial task that requires in-depth knowledge of the code and the system being tested. ChatGPT can provide suggestions and guidance, but it cannot replace a developer’s understanding of the code and their ability to write effective and meaningful tests. Ultimately, the responsibility of generating unit tests lies with the developer and their understanding of the requirements and expected behaviors of the code being tested.

Suppose you have a Java class like this and want to create some unit testing.

public class LibraryClient {
    public static void main(String[] args) {
        LibraryService libraryService = LibraryService.getInstance();
        requestBook("Clean Code", libraryService);
        requestBook("The Lord of the Rings", libraryService);
        requestBook("The Lord of the Rings", "COMPUTER_SCIENCE", libraryService);
    }

    private static void requestBook(String bookName, LibraryService library) {
        library.getBook(bookName)
                .ifPresentOrElse(book -> System.out.println("The book '" + bookName + "' was found, here are the details:" + book),
                        () -> System.out.println("The library doesn't have the book '" + bookName + "' that you need."));
    } 
}

You can provide the following prompt:

"can you provide sample unit testing for this function:"

public class LibraryClient {
    public static void main(String[] args) {
        LibraryService libraryService = LibraryService.getInstance();
        requestBook("Clean Code", libraryService);
        requestBook("The Lord of the Rings", libraryService);
        requestBook("The Lord of the Rings", "COMPUTER_SCIENCE", libraryService);
    }

    private static void requestBook(String bookName, LibraryService library) {
        library.getBook(bookName)
                .ifPresentOrElse(book -> System.out.println("The book '" + bookName + "' was found, here are the details:" + book),
                        () -> System.out.println("The library doesn't have the book '" + bookName + "' that you need."));
    } 
}

ChatGPT may respond with the following:


@RunWith(MockitoJUnitRunner.class)
public class LibraryClientTest {
    @Mock
    private LibraryService libraryService;

    @InjectMocks
    private LibraryClient libraryClient;

    @Test
    public void testRequestBook_bookFound() {
        when(libraryService.getBook(anyString()))
                .thenReturn(Optional.of(new Book("Clean Code", "PROGRAMMING")));

        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));

        libraryClient.requestBook("Clean Code", libraryService);

        assertEquals("The book 'Clean Code' was found, here are the details:" + new Book("Clean Code", "PROGRAMMING").toString(), outContent.toString());
    }

    @Test
    public void testRequestBook_bookNotFound() {
        when(libraryService.getBook(anyString()))
                .thenReturn(Optional.empty());

        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));

        libraryClient.requestBook("The Lord of the Rings", libraryService);

        assertEquals("The library doesn't have the book 'The Lord of the Rings' that you need.", outContent.toString());
    }
}

"In this example, the LibraryClientTest class uses JUnit and Mockito to test the requestBook method. The libraryService object is mocked using Mockito, so we can control the behavior of the getBook method for testing purposes.

The test case testRequestBook_bookFound verifies that the expected message is printed when the getBook method returns a Book object. The test case testRequestBook_bookNotFound verifies that the expected message is printed when the getBook method returns an empty Optional.

By writing these unit tests, we can validate that the requestBook method behaves as expected and provides the correct output in different scenarios.

The future

It is important to note that ChatGPT was trained on a vast amount of text, including code snippets from various programming languages. It has some understanding of many programming languages including Java, Python, JavaScript, C++, and more. However, it is not an expert in any language, and its ability to understand code may vary based on the specific syntax or concept.

The development team at FusionHit recognizes the potential of ChatGPT as a tool to enhance software engineers’ efficiency and productivity. To fully leverage its capabilities, our programming team is dedicated to mastering the art of crafting effective prompts.

At FusionHit, we believe that the core value of software engineers lies in their ability to think critically and creatively about a project, devise a strategic approach, and execute a proven process. The use of ChatGPT simply amplifies this expertise.

Share Pin it