Sử dụng tính năng của chatGPT để tăng hiệu suất code

Sau 1 tháng sử dụng ChatGPT, mình thực sự cảm thấy nó rất hữu ích để hỗ trợ cho việc học tập và công việc hàng ngày. Nó giúp mình tăng tốc độ và chất lượng công việc của mình một cách đáng kể. Vì vậy, mình sẽ chia sẻ 4 cách sử dụng mà mình đã trải nghiệm ^^

Nếu bạn nào chưa có tài khoản chatGPT thì vào bài viết này nhé <3

# Tạo sườn code

Chỉ cần một vài câu hỏi, ChatGPT có thể tạo ra cấu trúc mã nguồn cần thiết cho bạn, hoàn thiện với các công nghệ, khung và phiên bản bạn chọn. Nó không chỉ giúp bạn tiết kiệm ít nhất một giờ hoặc vài giờ mỗi lần, mà còn giúp nhóm của bạn làm việc hiểu quả hơn. Nó giống như có một tay trợ giúp thêm trong nhóm!

Ví dụ: Mình muốn sử dụng một API từ bên ngoài sử dụng Python.

Generate boiler plate code that integrates an extern API using Python.

Hoặc là hỏi khung thu thập dữ liệu facebook bằng javascripts:

Generate boilerplate code for crawling Facebook using JavaScript

Bạn hãy thử với chatGPT bằng những dự án mà bạn nghĩ ra nhé!

# Nghiên cứu và so sánh

Khá nhiều việc mình cần chuẩn bị cho dự án mới. Ví dụ như sử dụng công nghệ nào là tốt nhất cho dự án, hoặc muốn nghiên cứu 1 cái gì đó mới xuất hiện trong bài học của bạn. Thì tự nghiên cứu bằng cách tra cứu google, tài liệu mất rất nhiều thời gian. Vì thế nên bạn hãy để chatGPT giúp bạn làm việc đó.

Ví dụ: Mình muốn tạo một con chatbot và mình phân vân giữa việc sử dung javascripts hay golang.

Hoặc khi tôi muốn nghiên cứu cách lấy dữ liệu, và cần biết một thư viện tốt để làm điều đó:

# Đọc hiểu code và giải thích lại cho bạn

Bạn hãy copy đoạn code mới không hiểu và yêu cầu giải thích và comment thì nó sẽ làm ngay lập tức:

What does the following code do? [insert code here]

Câu hỏi của mình
Câu trả lời!

Mình yêu cầu nó comment code:

// Annotation to specify that this class is a Spring REST controller
@RestController
// Annotation to specify the base endpoint for this controller
@RequestMapping("/api/users")
public class UserController {
  
  // Repository object to interact with the database
  private final UserRepository userRepository;
  
  // Constructor to inject the repository object
  public UserController(UserRepository userRepository) {
    this.userRepository = userRepository;
  }
  
  // Annotation to specify this method maps to a GET request to the /api/users endpoint
  @GetMapping
  public List<User> getAllUsers() {
    // Returns a list of all users stored in the database
    return userRepository.findAll();
  }
  
  // Annotation to specify this method maps to a GET request to the /api/users/{id} endpoint
  @GetMapping("/{id}")
  public User getUserById(@PathVariable String id) {
    // Returns a user with the specified id from the database
    return userRepository.findById(id).orElse(null);
  }
  
  // Annotation to specify this method maps to a POST request to the /api/users endpoint
  @PostMapping
  public User createUser(@RequestBody User user) {
    // Creates a new user in the database and returns the saved user
    return userRepository.save(user);
  }
  
  // Annotation to specify this method maps to a PUT request to the /api/users/{id} endpoint
  @PutMapping("/{id}")
  public User updateUser(@PathVariable String id, @RequestBody User user) {
    // Sets the id of the user object to the specified id
    user.setId(id);
    // Updates the user in the database and returns the saved user
    return userRepository.save(user);
  }
  
  // Annotation to specify this method maps to a DELETE request to the /api/users/{id} endpoint
  @DeleteMapping("/{id}")
  public void deleteUser(@PathVariable String id) {
    // Deletes a user with the specified id from the database
    userRepository.deleteById(id);
  }
}

# Viết lại code theo phong cách khác

Bạn có một đoạn code (có thể là tự code) và muốn nó clean hoặc theo chuẩn nào đó thì hãy sử dụng chatGPT để làm điều đó:

Can you rewrite the following code using ... [code]

Dưới đây là code của mình:

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required="">

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required="">

  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required="">

  <button type="submit">Submit</button>
</form>


form {
  width: 500px;
  margin: 0 auto;
  padding: 30px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
}

label {
  font-size: 18px;
  font-weight: bold;
  display: block;
  margin-bottom: 10px;
}

input[type="text"],
input[type="email"],
input[type="password"] {
  width: 100%;
  padding: 10px;
  font-size: 16px;
  margin-bottom: 20px;
  border: 1px solid #ddd;
}

button[type="submit"] {
  width: 100%;
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  font-size: 18px;
  cursor: pointer;
}

Và mình yêu cầu nó viết lại theo chuẩn BEM:

<form class="form">
  <label class="form__label" for="name">Name:</label>
  <input class="form__input" type="text" id="name" name="name" required>

  <label class="form__label" for="email">Email:</label>
  <input class="form__input" type="email" id="email" name="email" required>

  <label class="form__label" for="password">Password:</label>
  <input class="form__input" type="password" id="password" name="password" required>

  <button class="form__submit" type="submit">Submit</button>
</form>
.form {
  width: 500px;
  margin: 0 auto;
  padding: 30px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
}

.form__label {
  font-size: 18px;
  font-weight: bold;
  display: block;
  margin-bottom: 10px;
}

.form__input {
  width: 100%;
  padding: 10px;
  font-size: 16px;
  margin-bottom: 20px;
  border: 1px solid #ddd;
}

.form__submit {
  width: 100%;
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  font-size: 18px;
  cursor: pointer;
}

Về cơ bản thì mình thấy ChatGPT sẽ rất hữu ích nếu chúng ta biết cách sử dụng, và không nên lạm dụng nó quá mức. Hãy sử dụng nó đúng cách như mục đích nó được sinh ra.

5 1 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận