Code: Review of Java Fundamentals: Object-oriented Design from PluralSight.com

Review of Java Fundamentals: Object-oriented Design

When I picked up the Java programming language a few years back, I focused on understanding the syntax, API and tooling of the language. Yet one area that I quickly glazed over was object oriented design patterns. Over the years, I’ve been itching to research this topic since I’ve heard that good architecture and the right design patterns could increase the maintainability of my source code.
For that reason, I decided last week that I would level up my design skills and take a course over Java object oriented design patterns on pluralsight.com.

The pluralsight course covered the popular SOLID principles mentioned in most Java OO design textbooks but with the authors spin on things.

After taking the course over a 3 day time span, I feel like it was time well spent! I had a lot of misconceptions about object oriented programming that were debunked in the first hour of the modules. Here are a few things I absorbed from the course.

1. The object with the most data should do the heavy lifting

2. Ask for help from objects, not their data

We can illustrate the first two points with a simple use case.
In this example we want to find the number of books with less than 100 pages in a shelf.

Ok Code

// ES6
const MAX_PAGES_FOR_SHORT_BOOK = 100
const numOfShortBooks = shelf.getBooks().filter(book => book.getTotalPages() < MAX_PAGES_FOR_SHORT_BOOK).length


Figure: Code 1.1

The problem here is that the shelf object is exposing too much information about itself, which could make refactoring hard.
In the course, the author states that objects should communicate and accomplish tasks using public methods, while keeping their inner data structures private and locked down. Avoid getter and setters of internal data like the plague. Not doing so will produce more run-time exceptions because of the unexpected way a external class is using your code.
Check this out.

Improved Code

// ES6
const MAX_PAGES_FOR_SHORT_BOOK = 100
const numOfShortBooks = shelf.countBooksByTotalPages(MAX_PAGES_FOR_SHORT_BOOK)


Figure: Code 1.2

As you can see, the method calls were wrapped inside .countBooksByTotalPages() to hide the complexity. Less code, less bugs.

3. Prefer interfaces over concrete classes.

In OO design, your objects should care about getting the job done regardless of the class. That's why you should prefer interacting with interfaces rather than classes. Interfaces will give you a greater number of objects that can fulfill a role because it's based on abilities. However, class must have the correct super class in it's class hierarchy to meet a requirement.

// in groovy language
interface Talkative {
  void speak()
}
class Person {
  speak(){
    println("Where's my money?")
  }
}
class Dog {
  speak(){
    println("woof! woof!")
  }
}

def saySomething(Talkative thing){
  thing.speak()
}

// :-( Doesn't work with Dog
def personSaySomething(Person person){
  person.speak()
}

saySomething(new Person()) // prints where's my money!
saySomething(new Dog()) // prints woof! woof!

personSaySomething(new Person()) // prints where's my money!
personSaySomething(new Dog()) // Error! Dog isn't of class Person


Figure: Code 1.3

In the above code, you can see that the saySomething() method is more flexible then personSaySomething().

Review: 5/5 - Recommended
Pros:
+ Short and concise
+ Covers the popular SOLID principles
+ Straight forward and complete examples

Cons:
- It not about designing objects. For that you'll have to find a course over Object-oriented analysis and design (OOAD)

- More info:
Java Fundamentals: Object-oriented Design

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

4 Notepad++ Plugins for Javascript Developers

Editor’s Note: This post is from 2012. The hottest editors in 2018 are vscode and editor.

Here are some Notepad++ plugins for JavaScript Developers that I found useful.

Tip:
You can install all the plugins using Notepad++ Plugins Manager located in the “Plugins” Menu.

  1. JSLint


    Description:
    JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules.


    Tip:
    If jslint turns out to be strict for you, then I recommend that you try jshint.com.
    Does it make any sense to use JSLint and follow it?

  2. JSMin


    Description:
    JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files.


    Tip:
    The great thing about this plugin is that it formats and minimizes.
    Link:
    JavaScript Compressor and Comparison Utility.

  3. JSON Viewer


    Description:
    A JSON viewer plugin for Notepad++. Displays the selected JSON string in a tree view.


    Tip:
    If you get “Could not parse!!” error message, then reformat your code with JSON.stringify().

  4. RegEx Helper


    Description:
    A Notepad++ plugin that allows users to develop regular expressions and test them against their open documents.

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Book review for “Become Your Own Boss in 12 Months”

I'm my own boss
I'm my own boss

Here’s a book review I did on amazon.com

I’ve started a few small businesses that failed because of poor planning & structure.
Seeking for a change, I picked up this highly rated book on entrepreneurship to find out the essential skills and techniques needed to build a successful company.
Having finished this book, I’ve found what I’ve been searching for and realized my mistakes.
For example, I’m now aware that before I start my next company I need to develop a solid business plan, eliminate my debt, save up a year worth of operational cost and read this book again.
I really enjoyed reading this book since it’s simple, straight forwards and offers “Emerson’s Action Steps”, a list summarizing each chapter.

This book covers all the aspects of a start-up, from forming a life and financial plan to marketing and hiring employees.
I recommend “Become your own boss in 12 months” for anyone starting up a business, whether on-line or off-line.

Thanks Melinda Emerson for sharing your insightful knowledge to the world.

Become Your Own Boss in 12 Months

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Siebel eScript headaches.


Here are a few problems that I’ve encountered while spending a week programming in Siebel eScript 7.8.

  1. Siebel eScript is ECMAScript compliant.
    ECMAScript is the standard implementation of JavaScript as defined by the ECMA-262 standard.
    Beginning with Siebel Business Applications version 7.8, the script engine supports ECMAScript Edition 4.
    You can opt to use the updated functionality of this engine, including the Siebel ScriptAssist tool, or you can opt to leave this updated functionality inactive.
    http://docs.oracle.com/cd/B31104_02/books/eScript/eScript_JSLOverview.html

    Here’s a link to ECMAScript Version 4.0. proposed draft.
    Example code for ES4.

    interface I { function f() } 
    interface J { function g(n) } 
    class C implements I, J { 
    	function f() { return "foo" } 
    	function g(n) { return n+2 } 
    }


    It should be noted that most major web browsers abandoned ES4 due it the complexity of feature implementations.
    So even if your scripts pass jshint.com and jslint.com 100%, and works on all major browsers you still have the possibility of your code throwing an during execution.
    Siebel eScript says it’s ECMA-262 compliant but I’ve found a few bugs in the engine that require some work around.

  2. Developing a simple way to unit test your code is essential.
    Here’s a easy way to add test function to your library.

    function logThis( name, result ){
    	// Log this values to a Property Set Object that get's converted to XML.
    	AddParameterPS( logObjs.Outputs, name, result, "logThis");
    }
    function testThis( name, func ){
    	try{
    		logThis( "Passed: "+ name, func() );
    	}
    	catch(e){
    		logThis( "Failed: "+ name, e );
    	}
    }


    Test example

    function runTest(Input, Output){
     
    	testThis( "Test strict equivalence support", function(){
    		return "1" !== 1;
    	});
    	// This will fail since Siebel doesn't support the apply and call Object function calls.
    	testThis( "Test isArray using call", function(){
    		// Siebel eScript: FAIL, Browser: Pass
    		function isArray( arr ){
    			return Object.prototype.toString.call( obj ) == "[Object Array]";
    		}
    	});
    	testThis( "Test isArray using instanceof", function(){
    		// Siebel eScript: Pass, Browser: Pass
    		function isArray( arr ){
    			return arr instanceof Array;
    		}
    	});
    }

  3. Siebel Business Applications in high-interactivity mode requires an ActiveX plugin, which forces you to use Internet Explorer on Windows.
  4. Siebel eScript has errors with it’s engine and it’s hard to debug eScripts for run-time business services.
    Here’s a strange bug in Siebel eScript.
    You can’t create return objects when they are generated from a function attached to an object.
    Example Code:

    	var x = {};
    	x.getObj = function( a, b ){
    		var obj = function(){
    			this.x = a;
    			this.y = b;
    		};
    		return new obj();
    	};
    	var getObj = function( a, b ){
    		var obj = function(){
    			this.x = a;
    			this.y = b;
    		};
    		return new obj();
    	};
    	var arr = [];
    	arr.push( getObj() );	// successful
    	arr.push( x.getObj() );	// Run-time Error.

  5. Siebel eScript engine is pretty damn slow. So avoid writing these two performance killers.
    1: Wrapping all your code within a closure.
    2: Defining an object inside closure with the same name as an object in the outer scope.
    Example:

    	var x = (function(){
    		var x = {
    			a:1
    		};
    		return x;
    	}());

Siebel eScript Reference from Oracle.com:
eScript Lanague Reference (2004 edition)

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube