0

Currently ,Managing few algorithms which perform set of normal operations on set of Student object.

I wanted to make these algorithm generic , so that we can perform those operations on others Object like Student.

I see in my legacy code base, redundant code can be replaced by Generic-Based Code.

What is best way / good practice for converting Non-Generic code to make it Generic ?

Sanjiv
  • 1,795
  • 1
  • 29
  • 45

3 Answers3

0

Your question is a unclear. If you meant generics as in Set<Object>, then if you have a certain operation that can be done on a Set<Object> you can use something like:

public <T> void doSomethingOnSet(Set<T> set) {
    for (T item : set) {
       //do something fancy: let's print the first item:
       System.out.println(item);
       return;
    }
}

Your method can now be called on a set of any type of objects, not just students.

assylias
  • 321,522
  • 82
  • 660
  • 783
0

In Java Tutorials there are examples how to convert legacy code to use generics:

http://docs.oracle.com/javase/tutorial/extra/generics/convert.html

Java Generics FAQ also has the subsection about dealing with legacy code:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Coping%20With%20Legacy

Without examples it's hard to give you something more specific.

luke657
  • 826
  • 2
  • 11
  • 28
-1

Your question is very generic :). One very simple though crude example of making your code generic is

public void updateStudent(Student student)
{}

to

public void updateObject(Object genericObject)
   {

    if( genericObject instanceof Student) 
        // do student specific operation


    if( genericObject instanceof School) 
        // do schoolspecific operation

  }

    }

It is just an example though it depends on your actual requirement what you want to achieve.

M Sach
  • 33,416
  • 76
  • 221
  • 314
  • I don't see anything generic with your proposal (as in `List` for example). – assylias Jun 29 '13 at 13:36
  • I think his question in not about using java generics but how we can made our code more generic. At least this what i understood from his question. – M Sach Jun 29 '13 at 13:38
  • I see what you mean - the question is quite unclear... – assylias Jun 29 '13 at 13:39
  • 2
    In this situation, you should use two separate methods (`update(Student)` and `update(School)`) instead of one large method with `instanceof` checks. You can throw any shared code into a private method where you can ensure the only objects being passed in are `Student`s or `School`s, the way you have it now, I could pass in a `String`. – Jeffrey Jun 29 '13 at 13:40
  • 1
    It would be helpful to know the downvote reason. Its always welcome but without any reason it hurts :) – M Sach Jun 29 '13 at 13:41
  • 1
    @MSach I explained my reasoning. – Jeffrey Jun 29 '13 at 13:41
  • jeffrey i totally agree with your point but as i said in the end "It is just an example though it depends on your actual requirement what you want to achieve.". I would definitely go with the way you suggested but this example is just an example to move towards more generic code. It can also be appropriate in some of the scenarios. – M Sach Jun 29 '13 at 13:43
  • 1
    @MSach Creating more generic code shouldn't sacrifice type safety. I don't see a single reason why using a single method with `instaceof` checks is more appropriate than overloading methods. If you can explain why it is, I will gladly flip my vote. – Jeffrey Jun 29 '13 at 13:53
  • @MSach Rather lots of if..else for each Object , I wanted to use Generic .... – Sanjiv Jun 29 '13 at 14:07
  • @jeffrey Just forget student, school object here. Say its URL connection object where i do not want to entertain HTTPURLConnection(from some third party) but only HttpsURLConnection. Then probably i will check type of object and handle the logic. Again my point here is not make your vote flip but to just understand the reason. – M Sach Jun 29 '13 at 14:12