Wicket Loose Coupling

We can do it in two ways,

  1. Using Model : Partially loosely coupled , it is really worth considering
  2. Using Custom Events : Fully loosely coupled

Loose Coupling Using Model

public class ChangeSensitiveModel extends Model {

	private static final long serialVersionUID = 1L;

	public ChangeSensitiveModel() {

	}

	public ChangeSensitiveModel(final T object) {
		super(object);
	}

	final public void setObject(final T newObject) {
		if (onBeforeSet(newObject)) {
			super.setObject(newObject);
			onAfterSet();
		}
	}

	protected boolean onBeforeSet(T newObject) {
		return true;
	}

	protected void onAfterSet() {

	}
}

ChangeSensitiveModel In Action

Set object to the ChangeSensitiveModel Model

private Button newButton(final ChangeSensitiveModel<ValueMap> model, final Form<ValueMap> searchForm) {
		return new AjaxButton(("sayHello")) {

			private static final long serialVersionUID = 1L;
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				model.setObject(searchForm.getModelObject());
			}
		};
	}

Override the call back

public LooseCouplingUsingModelPage() {

		changeSensitiveModel = new ChangeSensitiveModel(){

			private static final long serialVersionUID = 1L;

			@Override
			protected void onAfterSet() {
				LooseCouplingUsingModelPage.this.addOrReplace(new ResultPanel(RESULT_PANEL_ID, changeSensitiveModel));
				AjaxRequestTarget ajaxRequestTarget = AjaxRequestTarget.get();
				if (ajaxRequestTarget != null) {
					ajaxRequestTarget.addComponent(LooseCouplingUsingModelPage.this.get(RESULT_PANEL_ID));
				}
			}
		};
		add(new SearchPanel(SEARCH_PANEL_ID, changeSensitiveModel));
		add(newEmptyPanel());
	}

Loose Coupling Using Custom Event

Firing an Event

new RefreashFeedBackPanelEvent(SearchPanel.this, target, searchForm.getModelObject()).fire();

Responding to Event : Any component wanting to respond to Customer event should implement AjaxUpdateListener

public void handleAjaxUpdate(final AjaxUpdateEvent event) {
		if (event instanceof RefreashFeedBackPanelEvent) {
			info("You said : " + ((RefreashFeedBackPanelEvent)event).getSourcData().getString("name"));
			event.getTarget().addComponent(feedbackPanel);
		}
	}

Go Grab the source code from  GitHub

do mvn jetty:run and see the example in Action.

Good Vs. Bad Programmer

Good vs. Bad Programmer

Good Programmer is one who

  1. Always strives for excellence;
  2. follows good design principles;
  3. Always keep in mind CHANGE and MAINTAINIBILITY;
  4. At least refactors his/her code thrice
  5. Always try to improve the code base.
  6.  While working with Object oriented language always thinks of Object Oriented Design and Principles, so on…

In short a good programmer tries to stick to:

“Don’t Comment the Bad code rewrite it”

On the contrary a bad programmer (not so good programmer to be politically correct ;-)) is one who

  1. Don’t care about any rule
  2. Don’t follow any principle
  3. Never think about MAINTAINABILITY and the cost involved.
  4. Always try to find an excuse to support their bad code, for example they will say “Man!! I am not doing anything new or wrong that sort of code is already there in the code base.” Duh!!! We can’t expect them to improve the existing code base (in small steps of course) because they are not even willing to write the new code well.

The fundamental question nowadays is “Why do people write Bad Code?”

Let me share with you my experience before I answer that question!!

I have been mentoring wide varieties of people, from college graduate level to 8years programming experience doods. Here is my experience with them.

  1. College graduates learn things (good programming techniques) faster (given correct guidance, of course candidate should be willing to learn)
  2. There are three categories of  experienced people.
    1. People who do good programming. [Very rare]
    2. People who do not know what is clean code but they are willing to learn (I consider my self here). [masses]
    3. People who neither know about clean code nor they are willing to learn. This kind of people are very dangerous for the project, get rid of this kind of people at the very first place (obviously after giving them some warnings)  [rare]

So we have masses who don’t know anything about good programming, so obviously they will write Bad Code, because they are not exposed to Clean Coding (I see people write sequential Code With Object Oriented Languages [really frustrating])

Bad Code don’t Do this

public List<XyzCondition> getConditions() {
   List<XyzCondition> conditions = // Build or Get Conditions
   return conditions;
}

List List<XyzCondition>  list = getConditions();
list.remove(conditionName);

Simplified Version

 public Conditions getConditions() {
   List<XyzCondition> conditions = // Build or Get Conditions
   return new Conditions(conditions);
}
Conditions  conditions = getConditions();
conditions.removeCondition(conditionName);

There are lots of problems with the first version of the code, but most people would argue that there is not much difference. In fact some people would go to extend that they would say first code is simple than the later one. Duhhhh !!!!

People wont even think twice before initiating such kind of discussion, They enjoy writing sequential code and they can’t tolerate a change, there is a bit of emotional baggage that they don’t want to overcome

People (99% of them, as I said there will always be some exceptions) who work with me always think in terms of Object Oriented Design and principles, even in tight schedules they produce high quality code. I am  aware that some people would ask for more time the moment we demand clean code. Because for them writing clean code is like rocket science engineering.