Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Newfangled Or Classic Programming?

Discussion in 'Java Update' started by Joe, 5/5/19.

  1. Joe

    Joe Moderator

    Since JAVA 8 (and all other "newfangled" OOPL such as C#) appears on the stage everyone can fool the oldies (like me) with some Newfangled Programming Techniques or NPT for short ;) It always looks good and sophisticated. But is it good for the (end)users? I mean the users who have to live with the newfangled software. I am not sure, because they, the users, usually were told that "it's always so! Or you have to tell me where is the problem?". And that immediately silenced them. What is it? The newfangled Programming technique ?

    The youngsters, the so-called Smart-Phone Generation or "SPG", who are not only adept with icons, but also with buzzwords (like "DB", "SQL", etc.) usually dislike to write down something. Probably they couldn't write at all (don't throw stones at me). Hence they loves the "acronyms" (or abbreviations) and all kinds of shorthand-writing. I took time and analyzed the "NPT" (Buzzword again ! The youngsters fall in love...with me). Here are some results.

    LOOPING
    The shorthand for-loop is very popular. It looks neat and clean. But is it as neat and as clean as it should be? Let check it.
    Code:
    // Newfangled Programming Technique
    public class Test {
      public static void main(String... args) {
    	String[] lst = { "Joe Nartca1", "Joe Nartca2",
    					 "Joe Nartca3", "Joe Nartca4",
    					 "Joe Nartca5", "Joe Nartca6",
    					 "Joe Nartca7", "Joe Nartca8",
    					 "Joe Nartca9", "Joe Nartca10",
    					 "Joe Nartca11", "Joe Nartca12"
    				   };
    	long t = System.nanoTime();
    	for (String s:lst) System.out.println(s); // <- newfangled
    	System.out.println((System.nanoTime()-t)+" nSec.");
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  15:27			 1.203 Test.class
    ...
    C:\links\java\mapListArray>java Test
    Joe Nartca1
    Joe Nartca2
    Joe Nartca3
    Joe Nartca4
    Joe Nartca5
    Joe Nartca6
    Joe Nartca7
    Joe Nartca8
    Joe Nartca9
    Joe Nartca10
    Joe Nartca11
    Joe Nartca12
    7702058 nSec.
    
    Now with the "classic" way what the oldies like me usually code:
    Code:
    public class Test {
      public static void main(String... args) {
    	String[] lst = { "Joe Nartca1", "Joe Nartca2",
    					 "Joe Nartca3", "Joe Nartca4",
    					 "Joe Nartca5", "Joe Nartca6",
    					 "Joe Nartca7", "Joe Nartca8",
    					 "Joe Nartca9", "Joe Nartca10",
    					 "Joe Nartca11", "Joe Nartca12"
    				   };
    	long t = System.nanoTime();
    	//for (String s:lst) System.out.println(s); // <- newfangled
    	for (int i = 0; i < lst.length; ++i) System.out.println(lst[i]);
    	System.out.println((System.nanoTime()-t)+" nSec.");
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  15:29			 1.179 Test.class
    ...
    C:\links\java\mapListArray>java Test
    Joe Nartca1
    Joe Nartca2
    Joe Nartca3
    Joe Nartca4
    Joe Nartca5
    Joe Nartca6
    Joe Nartca7
    Joe Nartca8
    Joe Nartca9
    Joe Nartca10
    Joe Nartca11
    Joe Nartca12
    6942543 nSec.
    
    You may say "Old man! It isn't a big deal. Nowadays memory is cheaper than dirt." Well, but The Germans used to say "Kleinvieh macht auch Mist." (in fine English: Many a little makes a mickle.) It isn't about the saved memory. It's about the processing time. The simplest formula is:
    Code:
    More codes = more works = more time = longer response time = users get boring and frustrated.
    
    Where does the "time" come from? It's the immunity of String. The reason: the reference with index doesn't produce a new string while the newfangled way does.

    CHAINING
    The boss is usually absent himself from the daily programming, so he is easily stunned by the very very loooong chaining code line like this:
    Code:
    public class Test {
      public static void main(String... args) {
    	String s = "joenartcajoenartcajoenartcajoenartcajoenartcajoenartcajoenartcajoenartca";
    	long t = System.nanoTime();
    	s = s.replace("n", " N").replace("aj", "a J");
    	System.out.println((System.nanoTime()-t)+" nSec.:"+s);
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  15:06			 1.055 Test.class
    ...
    C:\links\java\mapListArray>java Test
    88952 nSec.:joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca
    
    and the oldie's way
    Code:
    public class Test {
      public static void main(String... args) {
    	StringBuilder sb = new StringBuilder("joenartcajoenartcajoenartcajoenartcajoenartcajoenartcajoenartcajoenartca");
    	long t = System.nanoTime();
    	while (true) {
    	  int a = sb.indexOf("n");
    	  sb.replace(a, a+1, " N");
    	  a = sb.indexOf("aj");
    	  if (a < 0) break;
    	  sb.replace(a, a+2, "a J");
    	}
    	System.out.println((System.nanoTime()-t)+" nSec.:"+sb.toString());
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  15:04			 1.224 Test.class
    ...
    C:\links\java\mapListArray>java Test
    88096 nSec.:joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca Joe Nartca
    
    The youngsters laugh loudly and point their finger at the "big" figure of generated codes "1224 bytes versus 1055" and proudly say "Old man, the time difference is due to system-unpredictable activities in the background". True, but superficially. The time difference comes from the fact that String is immutable. And in a chaining action each replace() creates a new string, copies the new content to the new one and discards the old string. Garbage Collector has to work itself to "death". That is the true reason why StringBuilder works faster than String-Chaining.


    CHILD-RICH
    Asian families are usually "child-rich". Especially in Vietnam. And the trend of "child-richness" is back in Europe and the US. Youngsters who come from such a "child-rich" family are accustomed with "lots of things". They "habitually" produces hundreds of minute modules. Even for a tiny app -really child-rich, indeed: more children = more mouths for food = more works to support them. Exactly like that, more modules = more codes = more loading time.

    Code:
    public class Test {
      public static void main(String... args) {
    	Test_Child tc = new Test_Child("Joe Nartca");
    	String s = tc.getName();
    	System.out.println(s);
      }
    }
    // 1st little module
    interface child {
      String getName();
    }
    // 2nd little module
    class Test_Child implements child {
      public Test_Child(String name) {
    	this.name = name;
      }
      private String name;
      public String getName() {
    	return name;
      }
    }
    
    and the output:
    Code:
    04.05.2019  19:19				98 child.class
    04.05.2019  19:19			   416 Test.class
    04.05.2019  19:19			   277 Test_Child.class
    ...
    C:\JFX\Fuzzy\Test>java Test
    Joe Nartca
    
    If the "interface child" is "reduced" the outcome is unchanged, but like the "LOOPING" the generated codes are reduced "dramatically" -meaning: better response time (less loading time, less garbage collection)

    Code:
    public class Test {
      public static void main(String... args) {
    	Test_Child tc = new Test_Child("Joe Nartca");
    	String s = tc.getName();
    	System.out.println(s);
      }
    }
    //interface child {
    //  String getName();
    //}
    //class Test_Child implements child {
    class Test_Child {
      public Test_Child(String name) {
    	this.name = name;
      }
      private String name;
      public String getName() {
    	return name;
      }
    }
    
    and the output:
    Code:
    04.05.2019  19:24			   416 Test.class
    04.05.2019  19:24			   264 Test_Child.class
    ...
    C:\JFX\Fuzzy\Test>java Test
    Joe Nartca
    
    SHORTHAND

    This case appears "peanut" to you. However, as the Germans used to say "Kleinvieh macht auch Mist." Piece by piece it grows to a huge heap of obstacles for the performance. Amazon and Alibaba sell "peanuts", but they are much richer than BMW and Mercedes-Benz together with their "noble and outrageously expensive" cars.
    Code:
    public class Test {
      public static void main(String... args) {
    	String a = "Hello Joe";
    	String b = "Bonjour Joe";
    	String c = null;
    	c = a.charAt(0) == 'H'? a:b;
    	System.out.println(c);
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  14:29			   460 Test.class
    ...
    C:\links\java\mapListArray>java Test
    Hello Joe
    
    Old way:
    Code:
    public class Test {
    	String a = "Hello Joe";
    	String b = "Bonjour Joe";
    	String c = null;
    	if (a.charAt(0) == 'H') c = a;
    	else c = b;
    	System.out.println(c);
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  14:33			   458 Test.class
    ...
    C:\links\java\mapListArray>java Test
    Hello Joe
    
    NOW WHAT?

    In general the newfangled technique is not bad at all, it depends on case to case. If the processing time is crucial it's better to go with the old ways (e.g. huge data processing). Otherwise the newfangled ways are clear and "extremely" valuable. The Lambda-Expression makes life, for example, much easier than earlier. Especially with GUI application. Example:

    Code:
    import javax.swing.*;
    // author Joe Nartca
    public class Test {
      public static void main(String... args) {
    	JFrame jf = new JFrame("Newfangled");
    	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JButton but = new JButton("ClickMe");
    	but.addActionListener(e ->{
    	  System.out.println("You've 'KISSED' me :o)");
    	});
    	jf.add(but);
    	jf.setSize(100, 100);
    	jf.setVisible(true);
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  15:40			 1.310 Test.class
    ...
    C:\links\java\mapListArray>java Test
    You've 'KISSED' me :o)
    
    And the old way is extremly awkward
    Code:
    import javax.swing.*;
    // author Joe Nartca
    public class Test extends JFrame {
      public static void main(String... args) {
    	new Test();
      }
      public Test() {
    	setTitle("Newfangled");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JButton but = new JButton("ClickMe");
    	but.addActionListener(e ->go());
    	add(but);
    	setSize(100, 100);
    	setVisible(true);
      }
      private void go() {
    	System.out.println("You've 'KISSED' me :o)");
      }
    }
    
    and the generated codes
    Code:
    05.05.2019  15:42			 1.375 Test.class
    ...
    C:\links\java\mapListArray>java Test
    You've 'KISSED' me :o)
    
     
    Last edited: 5/5/19

Chia sẻ trang này

Loading...