this post was submitted on 08 Jun 2026
10 points (69.2% liked)

Programming

27223 readers
276 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 3 years ago
MODERATORS
 

Delimited flat-file parsing often leads to brittle index-based code. In this post, I show how enums make field positions easier to read and maintain.

In the examples below, we assume the input has already been split:

String[] split = delimitedData.split("\\|");

There are caveats of using the split method this way, but they are outside the scope of this post.

Direct indexing

DbData existingData = dbHandler.getExistingData(
    split[2],
    split[7],
    split[8],
    split[9]
);

Direct indexing is compact, but brittle and hard to scan.

Local variables

String id = split[2];
String date = split[7];
String time = split[8];
String reason = split[9];

DbData existingData = dbHandler.getExistingData(
    id, 
    date, 
    time, 
    reason
);

Local variables improves readability at the call site, but field mappings are still scattered across the code base.

Enum mapping

public enum FlatFileField {
    ID(2),
    DATE(7),
    TIME(8),
    REASON(9);

    private final int index;

    FlatFileField(int index) {
        this.index = index;
    }

    public int index() {
        return index;
    }
}

DbData existingData = dbHandler.getExistingData(
    split[FlatFileField.ID.index()],
    split[FlatFileField.DATE.index()],
    split[FlatFileField.TIME.index()],
    split[FlatFileField.REASON.index()]
);

Comparison

Approach         Pros                                         Cons                                                        
Direct indexing Concise                                     Uses magic numbers, hard to maintain, higher cognitive load
Local variables Readable at call site                       Field mapping still scattered                              
Enum mapping     Centralized field positions, clearer intent Require an additional enum                                  

Takeaway

Enums are a simple way to replace magic numbers with meaningful names when working with delimited data. They improve readability and centralize field positions. When parsing logic grows beyond simple positional access, a dedicated parser or DTO is usually a better choice.

you are viewing a single comment's thread
view the rest of the comments
[–] frustdev@programming.dev 1 points 2 days ago (1 children)

Not written, but it has been used to edit the original article to reduce length and make it more concise.

This code would parse pipe-separated data such as:

"foo|bar|id2|foo|bar|foo|bar|2026-06-03|23:59:00|random reason"

[–] litchralee@sh.itjust.works -1 points 2 days ago

If the data is already delimited, why does the implementation need to have fixed offsets? Why not just count the fields, as 0, 1, 2, etc?

Your post speaks of brittle code but using fixed offsets is almost always guaranteed to be brittle.