程序員最恨兩件事情:一是別人代碼不寫文檔,二是要讓自己寫文檔。隨著 GPT-4 的到來這些都不是問題了,順帶可能連程序員都解決了。。。
之前一直覺得 AI 生成的代碼也就寫個(gè)面試題的水平,小打小鬧,現(xiàn)在時(shí)代可變了。Cursor 編輯器是 GPT-4 驅(qū)動(dòng),對接了 GitHub 的 Copilot。本文列舉了幾個(gè)他能完全掌控的場景,就看你慌不慌
官網(wǎng): https://www.cursor.so/

一、代碼審查
他能看懂你的代碼,并能指出錯(cuò)誤?。。∫簿褪钦f他的編程水平是高于你的!畢竟基于 GitHub 的海量代碼訓(xùn)練出來的,Code Review 從此有了個(gè)好老師
- 源碼:我們從網(wǎng)上找了個(gè)常見的金額處理函數(shù),并刪掉一些邊界條件看看能不能發(fā)現(xiàn)問題
const formatMoney = (money, symbol = "", decimals = 2) => {
let arr = money.toFixed(decimals).toString().split(".");
let first = parseInt(arr[0]).toLocaleString();
let result = [first, arr[1]].join(".");
return `${symbol} ${money.toFixed(decimals)}`;
};
formatMoney(12341234.246); // 12,341,234.25
formatMoney(12341234.246, "¥", 1); // ¥ 12,341,234.2
- 輸入指令:分析 formatMoney 函數(shù)的入?yún)L(fēng)險(xiǎn),中文回答
- 效果:詢問被修改的函數(shù)風(fēng)險(xiǎn)能準(zhǔn)確回答,并給出改進(jìn)方法
- 輸入指令:分析這個(gè)文件并給出優(yōu)化建議
- 效果:能清晰的識(shí)別 3 個(gè)函數(shù)的作用,并能給出建議
二、補(bǔ)充文檔
程序員最恨兩件事情:一是別人代碼不寫文檔,二是要讓自己寫文檔。現(xiàn)在這都不是問題了,在不寫一行中文注釋的情況下,生成完整的文檔不在話下
- 輸入指令:補(bǔ)充這個(gè)文件的中文markdown,包含使用說明、場景說明、注意事項(xiàng)、代碼示范等
- 效果:markdown 渲染效果如下
三、補(bǔ)充單測
- 輸入指令:給上面函數(shù)補(bǔ)充完整的單元測試,考慮各種邊界條件
- 效果:瞬間生成單測,并能覆蓋各種邊界和極端情況,極大的提高代碼質(zhì)量
test("formatMoney should format money correctly", () => {
expect(formatMoney(12341234.246)).toBe(" 12341234.25");
expect(formatMoney(12341234.246, "¥", 1)).toBe("¥ 12341234.2");
expect(formatMoney(0)).toBe(" 0.00");
expect(formatMoney(0.1)).toBe(" 0.10");
expect(formatMoney(0.005)).toBe(" 0.01");
expect(formatMoney(9999999999.999)).toBe(" 9999999999.99");
});
test("fenToYuan should convert fen to yuan correctly", () => {
expect(fenToYuan(123456)).toBe("1234.56");
expect(fenToYuan(0)).toBe("0.00");
expect(fenToYuan("123456")).toBe("1234.56");
expect(fenToYuan("0")).toBe("0.00");
});
test("yuanToFen should convert yuan to fen correctly", () => {
expect(yuanToFen(12.34)).toBe("1234");
expect(yuanToFen(0)).toBe("0");
expect(yuanToFen("12.34")).toBe("1234");
expect(yuanToFen("0")).toBe("0");
});
test("formatMoney should handle negative numbers correctly", () => {
expect(formatMoney(-1234.5678)).toBe("- 1234.57");
expect(formatMoney(-1234.5678, "¥", 1)).toBe("-¥ 1234.6");
expect(formatMoney(-0.1)).toBe("- 0.10");
expect(formatMoney(-0.005)).toBe("- 0.01");
expect(formatMoney(-9999999999.999)).toBe("- 9999999999.99");
});
test("fenToYuan should handle negative numbers correctly", () => {
expect(fenToYuan(-123456)).toBe("-1234.56");
expect(fenToYuan("-123456")).toBe("-1234.56");
});
test("yuanToFen should handle negative numbers correctly", () => {
expect(yuanToFen(-12.34)).toBe("-1234");
expect(yuanToFen("-12.34")).toBe("-1234");
});
四、補(bǔ)充定義
- 指令:補(bǔ)充formatMoney函數(shù)的interface
- 效果:可快速生成 interface
五、生成通用代碼
- 目標(biāo):我們提前定義一個(gè)樹狀的菜單結(jié)構(gòu)
tree
,然后讓 AI 根據(jù)字段和屬性自動(dòng)生成可投產(chǎn)面包屑組件 - 指令:生成一個(gè)面包屑組件,入?yún)⑹莟ree和當(dāng)前路由
- 效果:可迅速生成高質(zhì)量可運(yùn)行的面包屑組件,自動(dòng)分析 tree 的數(shù)據(jù)格式,并生成遞歸查找路徑的函數(shù)
const tree = [
{
name: '首頁',
route: '/',
},
{
name: '個(gè)人中心',
route: '/home',
children: [
{
name: '我的訂單',
route: '/order',
children: [
{
name: '未收貨',
route: '/undo',
},
{
name: '已收貨',
route: '/done',
}
]
},
{
name: '我的地址',
route: '/address',
children: [
{
name: '首頁',
route: '/',
}
]
}
]
}
]
const Breadcrumb = ({ tree, currentRoute }) => {
const breadcrumbList = [];
const findRoute = (tree, currentRoute) => {
for (let i = 0; i < tree.length; i++) {
const node = tree[i];
if (node.route === currentRoute) {
breadcrumbList.push(node.name);
return true;
} else if (node.children) {
if (findRoute(node.children, currentRoute)) {
breadcrumbList.push(node.name);
return true;
}
}
}
return false;
};
findRoute(tree, currentRoute);
return (
<div>
{breadcrumbList.reverse().map((item, index) => (
<span key={index}>
{item}
{index !== breadcrumbList.length - 1 && ' > '}
</span>
))}
</div>
);
};
<Breadcrumb tree={tree} currentRoute={'/home/order/undo'} />
六、總結(jié)
-
最后來一段視頻完整的回顧一下效果
使用基于GPT-4的Cursor編輯器自動(dòng)生成代碼
-
本文給出的例子其實(shí)都比較簡單,真正打開生產(chǎn)環(huán)境的復(fù)雜業(yè)務(wù)邏輯代碼照樣都能分析的頭頭是道,這里主要是紅線問題不能使用業(yè)務(wù)代碼作為示范。
-
現(xiàn)階段看起輔助作用是完全沒問題的,真正實(shí)現(xiàn)了人機(jī)結(jié)對編程,目測已經(jīng)可以取代 30% 左右的重復(fù)低水平勞動(dòng)。這還是在沒有完整閱讀業(yè)務(wù)倉庫代碼和 PRD 的基礎(chǔ)上做到的,如果以公司粒度投喂更多的業(yè)務(wù)知識(shí)和已有代碼,以及后面的技術(shù)升級(jí),那能干的活的比例肯定是越來越高的。文章來源:http://www.zghlxwxcb.cn/news/detail-404668.html
-
AI 的奇點(diǎn)真的快來了,有生之年肯定是能看到這樣的盛況了,是福是禍只能擁抱變化了吧!文章來源地址http://www.zghlxwxcb.cn/news/detail-404668.html
到了這里,關(guān)于如何使用基于GPT-4的Cursor編輯器提升開發(fā)效率的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!