注:本文使用NewBing(GPT4.0)演示
让他扮演一个Java软件开发者
第一步:咱们让ChatGPT扮演一个Java软件开发者的人物
- 提示词插件:地址:ChatGPTBingChatGPT3PromptGeneratorApp(Streamlit)-aHuggingFaceSpacebyKaludi
JavaSoftwareDeveloperJava软件开发者
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."
预备一个不太好的Java代码示例
第二步:咱们预备一个写得不太好的Java代码示例
- BadJavaExample:
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;}
让他优化这段代码
第三步:让newBing(ChatGPT)优化这段代码
- 而且要求「整理成比较高雅的代码结构,比方自动拆分子函数、降低if和循环嵌套,乃至实现部分规划形式。」****
// 界说一个接口,表明不同的运算战略public interface OperationStrategy { int apply(int a, int b);}
// 界说一个枚举,表明不同的运算符和对应的战略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); }}
// 界说一个办法,依据运算符和操作数来计算结果public int calculateUsingStrategy(int a, int b, String operatorName) {
// 依据运算符称号找到对应的枚举常量,假如没有找到则抛出反常 Operator operator = Operator.valueOf(operatorName.toUpperCase()); if (operator == null) { throw new IllegalArgumentException("Invalid operator"); }
// 调用枚举常量的apply办法来执行相应的战略 return operator.apply(a,b);}
让他写个单测
第四步:咱们最终让他收下尾——写个单测吧
import org.junit.Test;import static org.junit.Assert.*;
// 界说一个测验类,用@Test注解标记要执行的测验办法public class CalculatorTest {
// 界说一个测验办法,用assertEquals办法来验证计算结果是否正确 @Test public void testAdd() { int result = calculateUsingStrategy(3, 5, "add"); assertEquals(8, result); }
// 界说其他的测验办法,别离测验不同的运算符和操作数 @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办法来验证抛出了预期的反常 @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办法来验证抛出了预期的反常 @Test public void testInvalidOperator() { assertThrows(IllegalArgumentException.class, () -> calculateUsingStrategy(10,-2,"foo")); }}
最终,数据安满是最大的问题,不要乱贴数据到ChatGPT,尤其是触及后端中心存储账户密码、公司中心事务数据、部门中心战略规划等。因为首要,ChatGPT会使用你的问答语料进行练习;其次,你无法意料在什么Prompt提示词下,LLM模型会讲你无意中泄露的信息回答出去。 瑕不掩瑜,ChatGPT为代表的LLM模型,在充任咱们无所不知的教师、充任不知疲倦的通用Util代码编写者这些人物时能极大的进步咱们的开发效率,尤其在数据分析、前端、单测、重构等范畴。就像文章第一步写的一样,ChatGPT就像是一个百变身份,你能够让他扮演任何人物,而每一个人物都能在这个人物范围内帮助咱们获得更夸姣的生活。更有意思的用法等待我们的开掘。