viernes, 6 de agosto de 2010

JavaScript Class using JSON prototype


/// <summary>
/// Class that represents a customer of our store.
/// The constructor receives the name of the customer
/// </summary>
/// <param name="firstName"> First name of the customer.
/// <param name="lastName"> Last name of the customer.
function Customer(firstName, lastName)
{
// Properties
this.FirstName = firstName;
this.LastName = lastName;
}

Customer.prototype = {

/// <summary>
/// Returns the first name of the customer
/// </summary>
GetFirstName: function()
{
return this.FirstName;
},

/// <summary>
/// Returns the last name of the customer
/// </summary>
GetLastName: function()
{
return this.LastName;
},
...
}


Usage:


var aCustomer = new Customer("Pablo","Bustos");
alert("Hello " + aCustomer.GetFirstName());

Good programmers

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Refactoring: Improving the Design of Existing Code by Martin Fowler

jueves, 8 de julio de 2010

miércoles, 7 de julio de 2010

Buenos videos de Uncle Bob (Robert Martin, autor de Clean Code)

http://www.objectmentor.com/videos/video_index.html

lunes, 5 de julio de 2010

Exceptions de jetty para retornar http status codes

Usando jetty, puedes arrojar un status code, con mensaje incluido, de la sgte manera:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
throw new HttpException(404, "not found jeje");
}

jueves, 1 de julio de 2010

Wrap your Exception as a RuntimeException when it is not of your method's business

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.


package exceptions;

public class RuntimeExceptionTest {

public class TestException extends Exception {

}

public static void main(String[] args) {
RuntimeExceptionTest test = new RuntimeExceptionTest();
try {
test.throwWrappedTestExceptionAsRuntimeException();
} catch (Exception e) {
System.out.println("exception catched:\n");
e.printStackTrace();
}
}

private void throwTestException() throws TestException {
throw new TestException();
}


public void throwWrappedTestExceptionAsRuntimeException() {
try {
throwTestException();
} catch (TestException e) {
throw new RuntimeException(e);
}
}
}

Notification rather than logging

¿Te ha pasado que el log ensucia?:

Location location = tracker.getCurrentLocation();
for (Filter filter : filters) {
filter. selectFor(location);
if (logger. isInfoEnabled()) {
logger. info("Filter " + filter.getName() + ", " + filter.getDate()
+ " selected for " + location.getName()
+ ", is current: " + tracker.isCurrent(location));
}
}


Solución:

Location location = tracker.getCurrentLocation();
for (Filter filter : filters) {
filter. selectFor(location);
support.notifyFiltering(tracker, location, filter);
}
Otros beneficios:
  • Mientras programas, no te preocupas de escribir un string concatenando objetos, déjalo para el final.
  • El traspaso del software al área de soporte de tu empresa se complementará dándoles la clase Support, en ella están todos los monitoreos o alarmas que deben capturar.
  • No debes inicializar un Logger de log4j en cada clase que creas (sino sólo en caso de que quieras hacer un log.debug mientras programas)
  • Los logs dejan de ser un string que se escribe "a la rápida", pues deben ser pensados realmente como solución a un requerimiento implícito del departamento de operaciones de tu empresa.
  • En cada método de support no sólo puedes poner un logger en disco, sino también un registro en BD o cualquier otra cosa, usa la encapsulación.

Ref: