AI editors like Cursor can generate code with outstanding velocity utilizing LLMs,
dealing with boilerplate and offering purposeful snippets for varied duties.
Nonetheless, when constructing sturdy programs, purposeful correctness is barely the
start line. Code should even be secure, predictable, maintainable, and free
from refined negative effects.
This text demonstrates, by means of a dialogue between a developer and an
LLM, how professional steering is essential to remodel an preliminary, doubtlessly
unsafe code snippet into a strong, system-ready element. This improvement
session was finished as half of a bigger improvement effort to put in writing a BoltDB
like storage engine for an inner workshop. This additionally demonstrates how
breaking the bigger improvement efforts into smaller programming classes
may be more practical with LLM primarily based improvement. We’ll use the widespread activity
of studying and writing a fixed-structure header inside a Java ByteBuffer.
The Dialog Begins
Preliminary Immediate to Begin the Dialog
Are you able to present me the way to write and skim a easy web page header
construction to a Java ByteBuffer? The header ought to comprise: Web page Sort (byte),
Web page Stage (byte), Variety of Keys (brief), Free Area Offset (int), Proper
Sibling Web page ID (int).
LLM-Generated Code (Utilizing place())
Definitely. This is a fundamental instance utilizing ByteBuffer’s place()
technique to handle reads and writes:”
import java.nio.ByteBuffer; public class PageHeaderV1 { // Offsets (implicitly managed by place) non-public static closing int PAGE_TYPE_OFFSET = 0; non-public static closing int LEVEL_OFFSET = 1; // pageType is 1 byte non-public static closing int NUM_KEYS_OFFSET = 2; // degree is 1 byte non-public static closing int FREE_SPACE_OFFSET_OFFSET = 4; // numKeys is 2 bytes non-public static closing int RIGHT_SIBLING_OFFSET = 8; // freeSpaceOffset is 4 bytes public static closing int HEADER_SIZE = 12; // rightSibling is 4 bytes public static void write(ByteBuffer buffer, byte pageType, byte degree, brief numKeys, int freeSpaceOffset, int rightSiblingPageId) { buffer.clear(); // Reset place to 0, restrict to capability buffer.place(PAGE_TYPE_OFFSET); buffer.put(pageType); buffer.place(LEVEL_OFFSET); buffer.put(degree); buffer.place(NUM_KEYS_OFFSET); buffer.putShort(numKeys); buffer.place(FREE_SPACE_OFFSET_OFFSET); buffer.putInt(freeSpaceOffset); buffer.place(RIGHT_SIBLING_OFFSET); buffer.putInt(rightSiblingPageId); } public static byte getPageType(ByteBuffer buffer) { buffer.place(PAGE_TYPE_OFFSET); return buffer.get(); } public static byte getLevel(ByteBuffer buffer) { buffer.place(LEVEL_OFFSET); return buffer.get(); } public static brief getNumKeys(ByteBuffer buffer) { buffer.place(NUM_KEYS_OFFSET); return buffer.getShort(); } public static int getFreeSpaceOffset(ByteBuffer buffer) { buffer.place(FREE_SPACE_OFFSET_OFFSET); return buffer.getInt(); } public static int getRightSiblingPageId(ByteBuffer buffer) { buffer.place(RIGHT_SIBLING_OFFSET); return buffer.getInt(); } }
This code makes use of place() to maneuver to the proper offset earlier than every learn
or write.
Developer Perception: The LLM produced functionally appropriate code for a
easy case. Nonetheless, the professional developer instantly acknowledges a possible
drawback: using buffer.place()
. This technique modifies the state of the
ByteBuffer object itself.
Developer Suggests Absolute Offsets
Thanks. Nonetheless, always altering the buffer’s place can
trigger points, particularly with shared buffers like MappedByteBuffer or in
concurrent code. It introduces negative effects. Can we rewrite this utilizing the
absolute get(index) and put(index, worth) strategies as a substitute to make it stateless
concerning the buffer’s place?
Developer Perception: The developer identifies the precise situation (aspect
results through state mutation) and understands the context the place it turns into
harmful (shared buffers, concurrency). They information the LLM in the direction of a safer,
stateless sample.
You might be completely proper. Utilizing absolute offset strategies avoids
modifying the buffer’s place state, which is way safer for shared or
concurrent use circumstances. This is how one can learn the numKeys and freeSpaceOffset
utilizing absolutely the strategies:
import java.nio.ByteBuffer; // Instance correction for a few strategies: public class PageHeaderCorrection { non-public static closing int NUM_KEYS_OFFSET = 2; non-public static closing int FREE_SPACE_OFFSET_OFFSET = 4; // Corrected getter utilizing absolute offset public static brief getNumKeys(ByteBuffer buffer) { return buffer.getShort(NUM_KEYS_OFFSET); // Reads at index 2, does not change place } // Corrected getter utilizing absolute offset public static int getFreeSpaceOffset(ByteBuffer buffer) { return buffer.getInt(FREE_SPACE_OFFSET_OFFSET); // Reads at index 4, does not change place } // Equally, writing would use put(index, worth): public static void writeNumKeys(ByteBuffer buffer, brief numKeys) { buffer.putShort(NUM_KEYS_OFFSET, numKeys); // Writes at index 2, does not change place } }
Utilizing get(index) and put(index, worth) ensures the operations are
side-effect-free regarding the buffer’s place.
Developer Perception: The LLM accurately implements the urged sample.
The developer now pushes for higher software program design, shifting from static
utility strategies to an object-oriented strategy.
Developer Requests Refactoring to Occasion-Primarily based Class
Developer: “Wonderful, that is precisely the sample wanted. Please present
the whole PageHeader class utilizing solely absolutely the offset strategies
(get(index), put(index, worth), getShort(index), putShort(index, worth), and so on.)
for all fields. Additionally, let’s refactor this. As an alternative of static strategies, make
PageHeader a category that takes the ByteBuffer in its constructor. It ought to act
as a view or wrapper round that buffer part. Take away the static write
technique that takes all attributes. As an alternative, present occasion strategies for
getting and setting every subject straight on the buffer offered within the
constructor, nonetheless utilizing the secure absolute offset strategies.”
Developer Perception: The developer applies software program design rules:
Encapsulation (wrapping the buffer) and Object-Oriented Design (occasion
strategies appearing on object state – the buffer reference). This improves
usability and maintainability.
LLM: “Understood. Refactoring the PageHeader into a category that wraps a
ByteBuffer offered at development is an efficient solution to encapsulate header entry
for a selected web page buffer. This is the refactored class with occasion strategies
for getting and setting fields utilizing absolute offsets:”
import java.nio.ByteBuffer; public class PageHeader { // Outline constants for offsets and dimension non-public static closing int OFFSET_PAGE_TYPE = 0; non-public static closing int OFFSET_LEVEL = 1; non-public static closing int OFFSET_NUM_KEYS = 2; non-public static closing int OFFSET_FREE_SPACE = 4; non-public static closing int OFFSET_RIGHT_SIBLING = 8; public static closing int HEADER_SIZE = 12; // Whole dimension of the header non-public closing ByteBuffer buffer; // Retailer the buffer occasion /** * Creates a PageHeader view wrapping the given ByteBuffer. * Assumes the buffer's related part begins at place 0 * or that the buffer slice/view is accurately positioned. * @param buffer The ByteBuffer containing the web page knowledge. */ public PageHeader(ByteBuffer buffer) { // Think about including checks: buffer != null, buffer.capability() >= HEADER_SIZE this.buffer = buffer; } // --- Getters utilizing absolute offsets --- public byte getPageType() { return buffer.get(OFFSET_PAGE_TYPE); } public byte getLevel() { return buffer.get(OFFSET_LEVEL); } public brief getNumKeys() { return buffer.getShort(OFFSET_NUM_KEYS); } public int getFreeSpaceOffset() { return buffer.getInt(OFFSET_FREE_SPACE); } public int getRightSiblingPageId() { return buffer.getInt(OFFSET_RIGHT_SIBLING); } // --- Setters utilizing absolute offsets --- public void setPageType(byte pageType) { buffer.put(OFFSET_PAGE_TYPE, pageType); } public void setLevel(byte degree) { buffer.put(OFFSET_LEVEL, degree); } public void setNumKeys(brief numKeys) { buffer.putShort(OFFSET_NUM_KEYS, numKeys); } public void setFreeSpaceOffset(int freeSpaceOffset) { buffer.putInt(OFFSET_FREE_SPACE, freeSpaceOffset); } public void setRightSiblingPageId(int rightSiblingPageId) { buffer.putInt(OFFSET_RIGHT_SIBLING, rightSiblingPageId); } /** * Initializes a brand new header construction with default/zero values * within the underlying buffer. */ public void initializeNewHeader() { setPageType((byte) 0); setLevel((byte) 0); setNumKeys((brief) 0); setFreeSpaceOffset(HEADER_SIZE); // Default free house begins after header setRightSiblingPageId(0); // Assuming 0 represents no sibling initially // Or use particular constants for null/invalid IDs if obtainable } }
This model encapsulates entry to the header fields inside an occasion
tied to a selected ByteBuffer. All learn and write operations use absolute
offsets, making certain security and avoiding negative effects on the buffer’s place.
I’ve additionally added an instance initializeNewHeader technique for comfort.
Takeaways: The significance of the developer position
There are extra enhancements doable. However this dialogue highlights important areas the place developer experience is
indispensable when working with LLMs:
- Figuring out Hidden Dangers: The preliminary code labored, however the developer
acknowledged the latent hazard of negative effects fromplace()
in broader system
contexts. - Contextual Consciousness: Understanding implications for
MappedByteBuffer, concurrency, and library design was key to steering away from
the stateful strategy. - Guiding In direction of Strong Patterns: The developer explicitly prompted
for stateless, absolute offset strategies, leveraging deep data of secure
low-level programming practices. - Making use of Software program Design Ideas: The ultimate refactoring wasn’t
nearly correctness however about higher design – encapsulation, instance-based
logic, maintainability (constants), and usefulness. - Vital Analysis: All through the method, the developer critically
evaluated the LLM’s output in opposition to not simply purposeful necessities but in addition
non-functional necessities like security, stability, and maintainability.
Conclusion
LLMs are extremely highly effective coding assistants, accelerating improvement
and dealing with advanced duties. Nonetheless, as this case research reveals, they’re instruments
that reply to steering. Constructing sturdy, dependable, and performant programs,
requires the important pondering, contextual understanding, and deep programs
data of an skilled developer. The professional does not simply immediate for
code; they consider, information, refine, and combine, making certain the ultimate product
meets the rigorous calls for of real-world software program engineering.