自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何讓ChatGPT充當(dāng)細(xì)致入微的Java代碼優(yōu)化工?

人工智能
ChatGPT為代表的LLM模型,在充當(dāng)我們無所不知的老師、充當(dāng)不知疲倦的通用Util代碼編寫者這些角色時能極大的提高我們的開發(fā)效率,尤其在數(shù)據(jù)分析、前端、單測、重構(gòu)等領(lǐng)域。

注:本文使用New Bing(GPT4.0)演示

1、讓他扮演一個Java軟件開發(fā)者

第一步:我們讓ChatGPT扮演一個Java軟件開發(fā)者的角色

  • 提示詞插件:地址:ChatGPT BingChat GPT3 Prompt Generator App (Streamlit) - a Hugging Face Space by Kaludi

Java Software Developer  Java軟件開發(fā)者

I want you to act as a Java software developer.
I will provide you with a list of commands and you will implement them.
My first request is "I need help creating a Java application."

圖片

2、準(zhǔn)備一個不太好的Java代碼示例

第二步:我們準(zhǔn)備一個寫得不太好的Java代碼示例

  • Bad Java Example:
public int calculateUsingIfElse(int a, int b, String operator) {
int result = 0;
if (operator.equals("add")) {
result = a + b;
} else if (operator.equals("subtract")) {
result = a - b;
} else if (operator.equals("multiply")) {
result = a * b;
} else if (operator.equals("divide")) {
if (b == 0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
result = a / b;
} else if (operator.equals("modulus")) {
if (b == 0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
result = a % b;
} else if (operator.equals("power")) {
result = (int) Math.pow(a, b);
} else if (operator.equals("square root")) {
if (a < 0) {
throw new IllegalArgumentException("Cannot take square root of negative number");
}
result = (int) Math.sqrt(a);
} else if (operator.equals("absolute value")) {
result = Math.abs(a);
} else if (operator.equals("negate")) {
result = -a;
} else {
throw new IllegalArgumentException("Invalid operator");
}
return result;
}

圖片

3、讓他優(yōu)化這段代碼

第三步:讓newBing(ChatGPT)優(yōu)化這段代碼

  • 并且要求「整理成比較優(yōu)雅的代碼結(jié)構(gòu),比如自動拆分子函數(shù)、降低if和循環(huán)嵌套,甚至實現(xiàn)部分設(shè)計模式?!?/span>

圖片

// 定義一個接口,表示不同的運算策略
public interface OperationStrategy {
int apply(int a, int b);
}


// 定義一個枚舉,表示不同的運算符和對應(yīng)的策略
public enum Operator {
ADD((a, b) -> a + b),
SUBTRACT((a, b) -> a - b),
MULTIPLY((a, b) -> a * b),
DIVIDE((a, b) -> {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a / b;
}),
MODULUS((a, b) -> {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a % b;
}),
POWER((a, b) -> (int) Math.pow(a, b)),
SQUARE_ROOT((a, b) -> {
if (a < 0) {
throw new ArithmeticException("Cannot take square root of negative number");
}
return (int) Math.sqrt(a);
}),
ABSOLUTE_VALUE(Math::abs),
NEGATE(a -> -a);


private final OperationStrategy strategy;


Operator(OperationStrategy strategy) {
this.strategy = strategy;
}


public int apply(int a, int b) {
return strategy.apply(a, b);
}
}


// 定義一個方法,根據(jù)運算符和操作數(shù)來計算結(jié)果
public int calculateUsingStrategy(int a, int b, String operatorName) {


// 根據(jù)運算符名稱找到對應(yīng)的枚舉常量,如果沒有找到則拋出異常
Operator operator = Operator.valueOf(operatorName.toUpperCase());
if (operator == null) {
throw new IllegalArgumentException("Invalid operator");
}


// 調(diào)用枚舉常量的apply方法來執(zhí)行相應(yīng)的策略
return operator.apply(a,b);
}

4、讓他寫個單測

第四步:我們最后讓他收下尾——寫個單測吧

圖片

import org.junit.Test;
import static org.junit.Assert.*;


// 定義一個測試類,用@Test注解標(biāo)記要執(zhí)行的測試方法
public class CalculatorTest {


// 定義一個測試方法,用assertEquals方法來驗證計算結(jié)果是否正確
@Test
public void testAdd() {
int result = calculateUsingStrategy(3, 5, "add");
assertEquals(8, result);
}


// 定義其他的測試方法,分別測試不同的運算符和操作數(shù)
@Test
public void testSubtract() {
int result = calculateUsingStrategy(10, 4, "subtract");
assertEquals(6, result);
}


@Test
public void testMultiply() {
int result = calculateUsingStrategy(7, 9, "multiply");
assertEquals(63, result);
}


@Test
public void testDivide() {
int result = calculateUsingStrategy(12, 3, "divide");
assertEquals(4, result);
}


// 定義一個測試方法,用assertThrows方法來驗證拋出了預(yù)期的異常
@Test
public void testDivideByZero() {
assertThrows(ArithmeticException.class,
() -> calculateUsingStrategy(10,0,"divide"));
}


// 定義其他的測試方法,分別測試不同的異常情況


@Test
public void testModulusByZero() {
assertThrows(ArithmeticException.class,
() -> calculateUsingStrategy(10,0,"modulus"));
}


@Test
public void testSquareRootOfNegativeNumber() {
assertThrows(ArithmeticException.class,
() -> calculateUsingStrategy(-4,-2,"square root"));
}


// 定義一個測試方法,用assertThrows方法來驗證拋出了預(yù)期的異常
@Test
public void testInvalidOperator() {
assertThrows(IllegalArgumentException.class,
() -> calculateUsingStrategy(10,-2,"foo"));
}
}

最后,數(shù)據(jù)安全是最大的問題,不要亂貼數(shù)據(jù)到 ChatGPT,尤其是涉及后端核心存儲賬戶密碼、公司核心業(yè)務(wù)數(shù)據(jù)、部門核心戰(zhàn)略規(guī)劃等。因為首先,ChatGPT會使用你的問答語料進行訓(xùn)練;其次,你無法預(yù)料在什么Prompt提示詞下,LLM模型會講你無意中泄露的信息回答出去。

瑕不掩瑜,ChatGPT為代表的LLM模型,在充當(dāng)我們無所不知的老師、充當(dāng)不知疲倦的通用Util代碼編寫者這些角色時能極大的提高我們的開發(fā)效率,尤其在數(shù)據(jù)分析、前端、單測、重構(gòu)等領(lǐng)域。

就像文章第一步寫的一樣,ChatGPT就像是一個百變身份,你可以讓他扮演任何角色,而每一個角色都能在這個角色范圍內(nèi)幫助我們獲得更美好的生活。

更有意思的用法期待大家的發(fā)掘。


責(zé)任編輯:武曉燕 來源: 得物技術(shù)
相關(guān)推薦

2024-03-21 11:34:40

2018-03-19 11:54:43

2021-12-02 06:14:50

數(shù)據(jù)勒索勒索軟件攻擊

2021-08-10 09:38:50

elementaryLinux

2019-08-22 08:53:57

IT現(xiàn)代化數(shù)字化轉(zhuǎn)型

2010-03-22 17:08:00

交換機端口

2024-09-26 00:10:00

Agent大模型AI

2023-10-10 08:00:00

2023-09-07 16:18:50

網(wǎng)絡(luò)方案

2023-03-15 15:56:09

新華三

2021-01-05 11:41:47

5G4G網(wǎng)速

2010-07-26 15:29:42

Telnet會話

2022-12-12 12:04:59

ChatGPT代碼軟件

2023-03-20 11:29:49

2024-07-18 08:26:09

2023-06-09 00:11:57

ChatGPT寫作風(fēng)格GPT

2012-06-18 15:18:32

JS

2022-12-15 19:27:33

多線程代碼性能

2014-12-19 09:54:03

Java

2009-04-08 10:22:06

IT系統(tǒng)運維管理北塔
點贊
收藏

51CTO技術(shù)棧公眾號