Skip to main content
gamedev.net
🔒 Locked

[java] What is Metadata for?

Started by andrewk3652 3 replies 1k views
Original Post
andrewk3652
andrewk3652
What's the point of Metadata? Can someone provide a practical example? IF I understand it correctly, all it's doing is acting as a sort of preprocessor for including extra data in the objects. Or is this wrong?
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
DJHoy
DJHoy
Quote:
Original post by andrewk3652
What's the point of Metadata? Can someone provide a practical example? IF I understand it correctly, all it's doing is acting as a sort of preprocessor for including extra data in the objects. Or is this wrong?


At it's most basic form, metadata is "data that describes data". For example, say you have a video clip (mpeg / avi / whatever). The video clip itself is the data. Metadata associated with it would be things like the date and time of the clip, the length, the location / etc....

andrewk3652
andrewk3652
Okay, so if I get it right, metadata provides information about your class that helps identify the content in the object.

When is this useful? I'd really like to see a practical example, as I'm having a hard time figuring out what I'd use this for.

(edit:)

Okay, in the avi file example, why couldn't this all be described using regular member variables and methods? I don't see the need for this extra level of abstraction.
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
SamLowry
SamLowry
AFAIK, meta-data is completly redundant. You can always write extra methods which provide users with the necessary information, or use conventions, and so on. But is this a good idea?

A simple example would be to attach an "Programmer" metadata attribute to every class, to know who wrote it. You can of course just let the class implement an extra interface (the ... ProgrammedPropertyHolder interface or something like that) which declares a "String getProgrammer()" method.
Personally, I would find it horrible to do it that way. It just adds a bunch of extra methods to a class which have nothing to do with the functionality it provides.

You could say that metadata are a bit like exceptions: one of the goals of exceptions is to move away all error-handling code away from the actual logic. Metadata allows you to separate the actual class and the data about the class.

There are plenty of examples of how you could use metadata. The .NET library is a good example for what you can use it.

Security for example: when using a security manager you need special permissions for, say, reading files. Security is not logic itself, it's something transcendent, it gives information about the code: this code may only do this or that when it has such and such permissions.
At this point, java library code is littered with security checks:
if ( System.getSecurityManager() != null ){    System.getSecurityManager().checkPermission( somePermission );}

which I find incredibly ugly. The .NET library handles this differently: (from MSDN)
[MyPermission(SecurityAction.Demand, Unrestricted = true)]public class MyClass{   public MyClass()   {      //The constructor is protected by the security call.   }   public void MyMethod()   {      //This method is protected by the security call.   }   public void YourMethod()   {      //This method is protected by the security call.   }}

This is one example where metadata cleans up your code considerably.


Another example of using metadata would be unit testing. (I don't know the java syntax for metadata very well, it's the concept that counts). Examples mainly borrowed from NUnit:

// declare a test@Testpublic void testSomething() { ... }// declare a disabled test; better than commenting out, since// you could ask the testing framework to give a list of// all disabled tests@Test@Disabledpublic void testOtherThing() { ... }// Perform test 5 times (for example, multithreading issues)@Test(5)public void test() { ... }// This test must throw an exception@Test@Expect( NullPointerException.class )public void testInvalidUsage() { ... }


Metadata can also help making more powerful IDE's (again, look at .NET's VS.NET).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.