Archive
6. Java vs C# (Extension Method)
kita bisa menambakan method seolah2 method tersebut terletak di kelas tersebut.
namespace ExtensionMethods { public static class MyExtensions { public static int WordCount(this String str) { return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } } }
kita bisa menggunakannya sebagai berikut
using ExtensionMethods; .... string s = "Hello Extension Methods"; int i = s.WordCount(); // seolah2 ada method WordCount pada string. ....
klo make java harus gini …
public class StringUtils { public static int WordCount(String str) { // something like this lah in java // return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } }
manggilanya seperti ini
String s = "Hello Extension Methods"; int i = StringUtils.WordCount(s);
jelas c# lebih mudah dibaca dengan extension method…..
5. Java vs C# Lamda Expression
Lamda expression membuat code delegate jadi lebih singkat
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int oddNumbers = numbers.Count(n => (n % 2 == 1)); // find the lambda expression here? and there's extension method too
mungkin ada yg bertanya2 darimana method Count yg ada di array integer? Itu extension method. Aku akan jelasin di postingan berikut
klo make java kita harus buat interface lagi dan melewatkannya sebagai strategy.
interface INumberSpesification { boolean isSatisfiedBy(int number); } class OddNumberSpesification extends INumberSpesification { public boolean isSatisfiedBy(int number) { return n % 2 == 1; } } class Numbers { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; public int count(INumberSpesification a) { // foreach numbers cek dengan a.isSatisfiedBy(_element). // jika benar maka increment count // return count } }
simple is beauty. simple is fast.
more code more bugs
4. Java vs C# (Head to Head) LINQ
klo di java mw cari elemen pada array yg lazim dilakukan adalah looping dan mengecek sampai kriteria yg diinginkan ketemu.
alangkah indahnya klo misalnya kita bisa query dari object….
LINQ is the absolute winner here…
public class Student { public string First { get; set; } public string Last { get; set; } public int ID { get; set; } public List<int> Scores; } // Create a data source by using a collection initializer. static List<Student> students = new List<Student> { new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}}, new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}}, new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}}, new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}}, new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}}, new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}}, new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}}, new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}}, new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}}, new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}}, new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}}, new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} } };
kita bisa melakukan query dari data pada list tersebut dengan LINQ.
caranya adalah sebagai berikut..
// Create the query. // studentQuery is an IEnumerable<Student> var smartStudentQuery = from student in students where student.Scores[0] > 90 select student;
sudah pasti lebih mudah dengan LINQ klo mw cari2 di tumpukan object….
agree?
3. Java vs C# (Delegates and Events)
Di c# jelas lebih mudah mengimplementasikan Observer pattern dengan adanya delegates dan event.
Klo di Java? harus buatin interface2 lagi. Konsep2 yg dipake sama dengan konsep listener yg ada di swing.
jelas c# lebih simple…
2. Java vs C# (Indexers)
java version dari wrapper untuk list mahasiswa.
public class MahasiwaBlackList { private List<Mahasiwa> _mahasiswaBlackList = new ArrayList<Mahasiwa>(); public void addBadMahasiswa(Mahasiwa mahasiswa) { _mahasiswaBlackList.add(mahasiswa); } public Mahasiwa getBadMahsiswa(int rating) { //if (_mahasiswaBlackList.size() == 0) throw ThereNoBadStudentException(); return _mahasiswaBlackList.get(rating); } public static void main(String[] args) { MahasiwaBlackList a = new MahasiwaBlackList(); a.addBadMahasiswa(new Mahasiwa()); System.out.println(a.getBadMahsiswa(0)); // using method }
di c# lebih cool dengan indexers
public class MahasiswaBlackList { // default private List<Mahasiswa> _mahasiswaBlackList = new List<Mahasiswa>(); public void AddBadMahasiswa(Mahasiswa mahasiswa) { _mahasiswaBlackList.Add(mahasiswa); } public Mahasiswa this[int rating] { get { return _mahasiswaBlackList[rating]; } } static void Main() { MahasiswaBlackList blackList = new MahasiswaBlackList(); blackList.AddBadMahasiswa(new Mahasiswa { GPA = 0.1, Name = "Longgur", NIM = "11111111" }); Console.WriteLine(blackList[0]); // using indexers => like an array isn't ? } }
yeah.. yeah…