In Java, would you sacrifice type safety for a nicer programming interface

When and why would you generally sacrifice typesafety for a nicer programming interface?

Let me give you an example: if you had the choice between two event aggregators, which one would you prefer and why?

Reflective Version:

SomeEvent.subscribe(instance, "nameOfAMethod"); //method called via reflection
SomeEvent.fire(arg1, arg2);  //the firing could actually even be statically typed

Statically typed version:

EventSystem.getEvent(SomeEvent.class).subscribe(new EventHandler<Payload>() {
   public void eventOccurred(Object sender, Payload payload) {
       //event handler code here
   }
});

EventSystem.getEvent(SomeEvent.class).fireEvent(payload);

Please note, that in Java, due to type erasure, you cannot implement a generic interface with different type parameters more than once and need to resort to anonymous or external classes for handlers.

Now the reflective event system has a nicer user interface, but you lose type safety. Which one would you prefer? Would you create empty event classes just for the sake of having a symbol, like Microsoft does it with PRISM in its event aggregator?

1

When and why would you generally sacrifice typesafety for a nicer programming interface?

Rarely if ever, and then only if the type un-safety was dealt with effectively in another manner.

Verbose/awkward programming interfaces yield a little fewer bugs, and far lower severity bugs than typeless interfaces in the middle of typed languages. Because let’s be honest, using string lookups or casting out of Object is its own horrible programming interface – now you have a horrible programming interface and typing issues.

3

I think your example does not really compare things at the same level of detail, whereas this would:

  1. stringly typed version:

    public void subscribeTo(Event event) {
         event.substribe(this, "handleEvent");
    }
    public void handleEvent(Object sender, Payload payload) {
        //do something
    }
    
  2. statically typed version:

    public void subscribeTo(Event event) {
        event.substribe(new EventHandler<Payload>() {
            public void eventOccurred(Object sender, Payload payload) {
                //do something
            }
        });
    }
    

So in essence the latter is not critically longer. But as others have pointed out, it is both faster and safer.

If you’re really that worried about token count, you shouldn’t be using Java to start with 😉

You would be sacrificing more than just type safety. Reflection is at least an order of magnitude slower in Java than using strong types.

It’s a false dichotomy anyway. It’s possible to have a nice programming API without sacrificing type safety. See http://boo.codehaus.org/

3

I actually implemented a strongly-typed event system similar to yours once (in C#). It was horribly over-engineered and horrible to use. By the time I had discovered how horrible it was, it had spread throughout my application like a virus. It took forever to refactor, and it felt good when it was gone.

Would you create empty event classes just for the sake of having a symbol, like Microsoft does it with PRISM in its event aggregator?

Like this?

applicationEvents.Foo.Subscribe(myHandler)

Yes. Definitely yes. Why not? It’s way more simple, easy to understand, and type safe to boot. Any new developer would have all the available events in one place and wouldn’t have to go searching your source code to figure out what events are available and how to use them.

No sense in sacrificing type safety when you don’t have to.

EDIT:

If I seem overly aggressive, I apologize. I don’t mean to criticize your design, I am just lamenting my own mistake which I made in the past, and it’s very similar to what you’re doing. Just be careful not to overcomplicate things.

if you have the time now then do it with the type checking. if its code that might last up to December 2013 you will regret method 1 later, unless its a small project and you will be the only one working on it … no even then if you have the time i would go with the typed.

Why? clearer, less mistakes, more verbose yes but will help keep some bugs at bay and that is a good thing

I am not 100% certain what those two examples are supposed to do, but I guess an alternative would be to use an enum. I imagine the number of allowed strings like "nameOfAMethod" is limited and you could put the anonymous class gory stuff in that enum.

enum Methods {
    NAME_OF_A_METHOD {
        EventHandler get(final Object instance) {
            return new EventHandler<Payload>() {
                public void eventOccurred(Object sender, Payload payload) {
                    //do something with instance
                }
            }
        }
    };

    abstract EventHandler get(Object instance);
}

And your example could become:

EventSystem.getEvent(SomeEvent.class).subscribe(NAME_OF_A_METHOD.get(instance));

Not sure if it is directly applicable to your use case but you get the idea.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật