serial-builder icon indicating copy to clipboard operation
serial-builder copied to clipboard

Non-fluent builder API

Open Marcono1234 opened this issue 3 years ago • 1 comments

Currently the builder only provides a fluent API. While this has some advantages it also have some disadvantages:

  • Bad IDE support:
    • Auto-formatting would remove proper indentation
    • Easy to make indentation mistakes, no help from the IDE
    • IDE type hints can make code very verbose (and possibly also decrease IDE reaction speed)
  • More difficult to break creation appart into separate steps

Therefore it might be useful to also offer a non-fluent builder API, for example similar to this:

byte[] serialData = serializableObject(
    classData(
        SerializableClass.class,
        List.of(
            intField("i", 6),
            objectField(
                "array",
                int[].class,
                array(new int[] {1, 2, 3})
            ),
            objectField(
                "s",
                String.class,
                string("nested-test")
            )
        )
    )
).createSerialData();

This could be realized in a rather user friendly way by exposing these static factory methods all on one class for which the user can then use a wildcard import (e.g. import static marcono1234...FactoryMethods.*;).

As part of this, it might be reasonable to rename the existing builders (and their packages) to:

  • SerialBuilder: FluentSerialBuilder / LowLevelFluentSerialBuilder (possibly a bit too verbose)
  • SimpleSerialBuilder: SimpleFluentSerialBuilder (too verbose?) / FluentSerialBuilder

The new builder API could be for now mostly be implemented on top of the existing fluent API. But eventually, especially for handle support, it might be easier to implement the fluent API on top of that new API. That would however also require a low level non-fluent API.

Marcono1234 avatar Oct 17 '22 21:10 Marcono1234

The new API could for example be named CompositeSerialBuilder (?).

Marcono1234 avatar Feb 08 '24 20:02 Marcono1234