Avoiding Glue-Code by using Tags
Posted: Fri Feb 29, 2008 5:25 pm
Cobra sounds very interesting and I would like to give my 5 cent on creating a new OO language. I am developing software since 27 years, so I have some experience. What I always stumble over during software development is object serialization and object communication - which is basically the same. You often write glue-code to copy the values of members of class A to class B or write code to store or read values of members in/from streams.
This is an annoying and stupid task, ending in many lines of code like:
stream.Write(m_Name)
stream.Write(m_FirstName)
stream.Write(m_Street)
stream.Write(m_Town)
etc.
It is also error-prone if you add a new member and forget to write and/or read it.
To solve this issue, one solution could be to "tag" members of classes. For instance, if I could tag all members which shall be serialized to a stream as "streamable", then an interface could enumerate all streamable members and write/read them to/from streams automatically without writing specialized code. XML streams would be a good choice, but it should not be limited to this.
The same could be done to exchange data between two classes, for example an Address class (with a person's address information) and a generic record class of a database driver, which provides fields of a table as an array. In this case the tag could be "db" for database. Maybe it could be even solved by the same "streamable" tag, using a different underlying stream class.
It would also make sense that multiple tags can be assigned to a single member or property and when a tag could hold a value, e.g. tag "size = 80" to specify the maximum allowed size of a string which would instruct a GUI class that the edit control associated with a member may have an input of max. 80 characters.
Of course such generic things wouldn't always work, for example if streaming would involve that multiple members are written as a single date. In this case it should be possible to overload the method which does the "automagic", so the members which can not be handled automatically are not tagged and handled by extra code.
Please let me know if this is something which could be implemented into Cobra.
Regards
Thorsten
This is an annoying and stupid task, ending in many lines of code like:
stream.Write(m_Name)
stream.Write(m_FirstName)
stream.Write(m_Street)
stream.Write(m_Town)
etc.
It is also error-prone if you add a new member and forget to write and/or read it.
To solve this issue, one solution could be to "tag" members of classes. For instance, if I could tag all members which shall be serialized to a stream as "streamable", then an interface could enumerate all streamable members and write/read them to/from streams automatically without writing specialized code. XML streams would be a good choice, but it should not be limited to this.
The same could be done to exchange data between two classes, for example an Address class (with a person's address information) and a generic record class of a database driver, which provides fields of a table as an array. In this case the tag could be "db" for database. Maybe it could be even solved by the same "streamable" tag, using a different underlying stream class.
It would also make sense that multiple tags can be assigned to a single member or property and when a tag could hold a value, e.g. tag "size = 80" to specify the maximum allowed size of a string which would instruct a GUI class that the edit control associated with a member may have an input of max. 80 characters.
Of course such generic things wouldn't always work, for example if streaming would involve that multiple members are written as a single date. In this case it should be possible to overload the method which does the "automagic", so the members which can not be handled automatically are not tagged and handled by extra code.
Please let me know if this is something which could be implemented into Cobra.
Regards
Thorsten