Saturday, June 21, 2025
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy
T3llam
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
T3llam
No Result
View All Result
Home Services & Software

Guiding an LLM for Strong Java ByteBuffer Code

admin by admin
June 9, 2025
in Services & Software
0
Guiding an LLM for Strong Java ByteBuffer Code
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


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:

  1. Figuring out Hidden Dangers: The preliminary code labored, however the developer
    acknowledged the latent hazard of negative effects from place() in broader system
    contexts.
  2. Contextual Consciousness: Understanding implications for
    MappedByteBuffer, concurrency, and library design was key to steering away from
    the stateful strategy.
  3. Guiding In direction of Strong Patterns: The developer explicitly prompted
    for stateless, absolute offset strategies, leveraging deep data of secure
    low-level programming practices.
  4. 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.
  5. 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.


RelatedPosts

The state of strategic portfolio administration

The state of strategic portfolio administration

June 11, 2025
You should utilize PSVR 2 controllers together with your Apple Imaginative and prescient Professional – however you’ll want to purchase a PSVR 2 headset as properly

You should utilize PSVR 2 controllers together with your Apple Imaginative and prescient Professional – however you’ll want to purchase a PSVR 2 headset as properly

June 11, 2025
Consumer Information For Magento 2 Market Limit Vendor Product

Consumer Information For Magento 2 Market Limit Vendor Product

June 11, 2025
Previous Post

The Seven Lethal Sins: Origin Is An Open World RPG Based mostly On The Hit Anime

Next Post

Apple Says macOS Tahoe is Appropriate With These Macs

Next Post
Apple Says macOS Tahoe is Appropriate With These Macs

Apple Says macOS Tahoe is Appropriate With These Macs

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • App (3,061)
  • Computing (4,401)
  • Gaming (9,599)
  • Home entertainment (633)
  • IOS (9,534)
  • Mobile (11,881)
  • Services & Software (4,006)
  • Tech (5,315)
  • Uncategorized (4)

Recent Posts

  • WWDC 2025 Rumor Report Card: Which Leaks Had been Proper or Unsuitable?
  • The state of strategic portfolio administration
  • 51 of the Greatest TV Exhibits on Netflix That Will Maintain You Entertained
  • ‘We’re previous the occasion horizon’: Sam Altman thinks superintelligence is inside our grasp and makes 3 daring predictions for the way forward for AI and robotics
  • Snap will launch its AR glasses known as Specs subsequent 12 months, and these can be commercially accessible
  • App
  • Computing
  • Gaming
  • Home entertainment
  • IOS
  • Mobile
  • Services & Software
  • Tech
  • Uncategorized
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. However you may visit Cookie Settings to provide a controlled consent.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analyticsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functionalThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessaryThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-othersThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performanceThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policyThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Save & Accept