# Design/Code choice : Method chaining vs Reflection

**URL:** <https://forum.golangbridge.org/t/design-code-choice-method-chaining-vs-reflection/38910>\
**Category:** Code Review\
**Created:** [May 29, 2025, 9:51am UTC](https://forum.golangbridge.org/t/design-code-choice-method-chaining-vs-reflection/38910 "2025-05-29T09:51:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Stratton](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/andrew_stratton/32/12684_2.png) [@Andrew\_Stratton](https://forum.golangbridge.org/u/Andrew_Stratton)\
**Post date:** [May 29, 2025, 9:51am UTC](https://forum.golangbridge.org/t/design-code-choice-method-chaining-vs-reflection/38910/1 "2025-05-29T09:51:18Z")

</div>

I have a need to create a UI (output as html eventually) and I am trying to decide between using method chaining for creating the UI, or using structs with struct tag aliases (like the standard JSON which I have used)

Method chaining looks like this (this works):

```auto
  // create block
server_log := library.NewBlockType("server.log", "misc", setup)
  // add a text output showing 'Log '
server_log.Add(text.New("Log "))
  // below can generate an html text/string input
  // with an empty message with name 'greeting'
server_log.Add(stringinput.New("greeting").Empty("greeting"))

```

Struct aliasing would look like this (not yet written) :

```auto
type UI struct {
 	_ widget.Text `Log `
 	Greeting widget.String `empty:"greeting"`
}
...
server_log := library.NewBlockType("server.log", "misc", setup, UI{})

```

It seems to me that a struct approach :

- + automatically adds the name of the ‘field’ including compilation error if duplicated in the struct
- + looks tidier
- _reflection could be a performance issue (unlikely for my use)_
- – there is no compile time checking of the struct tag/alias which sets up the UI structure and html names/keys
  - _this is a likely candidate for runtime bugs_

The method chaining :

- + includes compile time checking of the UI creation methods
- _has more power if needed (it’s just code)_
- – looks less tidy and includes more code/text
- – has to specific the name which isn’t compile time checked for duplicates
  - _this is a likely candidate for run time bugs - there is a run time check which can warn/error/panic_

I will need to create 100s (but not 1000s) of these in their own packages with attached custom Go func() (through setup) for my application and **I am trying to reduce technical debt here**.

I don’t think performance is significant since any reflection would be a run once only on server startup (of \<1000 instances).

Any suggestions or personal experience would be welcome - thank you 🙂

---

<div class="post-metadata">

**Author:** ![Andrew\_Stratton](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/andrew_stratton/32/12684_2.png) [@Andrew\_Stratton](https://forum.golangbridge.org/u/Andrew_Stratton)\
**Post date:** [May 29, 2025, 10:08am UTC](https://forum.golangbridge.org/t/design-code-choice-method-chaining-vs-reflection/38910/3 "2025-05-29T10:08:40Z")

</div>

Another issue is that the field order **might not** be guaranteed. 😑 This would break my application completely…

See [https://stackoverflow.com/questions/32392311/reflect-type-field-order](https://stackoverflow.com/questions/32392311/reflect-type-field-order)

---

<div class="post-metadata">

**Author:** ![Andrew\_Stratton](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/andrew_stratton/32/12684_2.png) [@Andrew\_Stratton](https://forum.golangbridge.org/u/Andrew_Stratton)\
**Post date:** [May 30, 2025, 12:48pm UTC](https://forum.golangbridge.org/t/design-code-choice-method-chaining-vs-reflection/38910/5 "2025-05-30T12:48:18Z")

</div>

~~One issue I have just discovered - wasm (through tinygo) doesn’t support reflect?!~~

Actually - tinygo doesn’t FULLY support reflect, e.g. below works in tinygo playground

```auto
package main

import (
        "fmt"
        "reflect"
)

type Idb struct {
	txt string `example`
	greeting string `empty:greeting`
}

func main() {
	r := reflect.TypeOf(Id{})
	fmt.Println(r.Name())
	for i := 0; i < r.NumField(); i++ {
		fmt.Println(r.Field(i).Name, r.Field(i).Tag)
	}
}

```

Outputs

```auto
​Id
​txt example
​greeting empty:greeting

```
