setXxxIfNotNull, getXxxOrNull or something along the lines

2 views
Skip to first unread message

Joern Huxhorn

unread,
Dec 26, 2009, 8:05:39 AM12/26/09
to Protocol Buffers
Hi everyone.

My current code is cluttered with repeated

String str=x.getFoo();
if(str != null)
{
builder.setFoo(str);
}

since the setFoo method of builder throws an NPE if one is trying to set null.

How about adding additional generated setFooIfNotNull (or something briefer with the same intention) methods to the builder that contains exactly that code?

The parsing has a similar problem. It returns "" instead of null if a String has not been set.

This creates repeated

if(x.hasFoo())
{
result.setFoo(x.getFoo());
}

Perhaps methods like getFooOrNull() could solve this.

These two issues are the only ones that keep hitting me while writing encoders and decoders using Protobuf.
I keep forgetting either one or the other which only dawns on me while implementing the tests for the persistence code.

What do you think?

Regards,
Joern.

p.s.: As always: Thank you very much for protobuf!

Kenton Varda

unread,
Dec 26, 2009, 9:38:37 AM12/26/09
to Joern Huxhorn, Protocol Buffers
This has been suggested before.  The critical question is:  how much will this increase the size of the resulting class files?  If someone could implement this and tell me how much it affects unittest.proto, for example, then we could make a decision.  The generated code is too big already so we can't do anything that would increase it by very much.

With 2.3.0 you could also write a protoc plugin which adds these accessors.


--

You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.



Reply all
Reply to author
Forward
0 new messages