Category Archives: code you’re paying for

.equals(), the other way

The new year bring with it a new project for me.
The new project came new colleagues.
And the new colleagues write funny stuff.
One colleague has his own way to check if two strings are the the same:

if (nodeId.compareTo("HEADER") == 0) {
	// ...
}
if (nodeId.compareTo("SHEET") == 0) {
	// ...
}
if (nodeId.compareTo("FOOTER") == 0) {
	// ...
}


Problem created, problem solved

Many thanks to Emanuele for submitting the code.

This is how you create the problem:

   Long.parseLong(Long.toString(id.longValue()));



And this is how you’re going to solve it:

   try {
           mId = Long.parseLong(Long.toString(id.longValue()));
           log.debug("id: " + id.longValue());
       } catch (NullPointerException npEx) {
           log.debug("NullPointerException npEx :: " + npEx.getMessage());
           return ActionSupport.ERROR;
       } catch (NumberFormatException nfEx) {
           log.debug("NumberFormatException nfEx :: " + nfEx.getMessage());
           return ActionSupport.ERROR;
       } catch (Exception gnEx) {
           log.debug("Exception gnEx :: " + gnEx.getMessage());
           return ActionSupport.ERROR;
       }

Someone said “If you are not part of the solution then you are part of the problem”.
The guy who has wrote this chunk of code, eventually is part of both.


The hard way

If you have to initialize a boolean, you can use ‘true’ or ‘false’. It’s easy.

But if you feel a little more fancy you can use something like that:

	boolean result = (IU.getId_profile()+"-"+IU.getId_project()+"-"+IU.getId_LEV())
		  .equals(IU.getId_profile()+"-"+IU.getId_project()+"-"+IU.getId_LEV());


Date Parsing Perversion /4

		public static long parseDate(String month,String day,String time_year)
			throws Exception
		{
			int pos_2p;
			try 
			{
				GregorianCalendar calendar = new GregorianCalendar();
				if((pos_2p=time_year.indexOf(":"))!=-1)
					calendar.set(calendar.get(Calendar.YEAR),
					((Month_STR.indexOf(month)+1+4)/4)-1,
					Integer.parseInt(day),
					Integer.parseInt(time_year.substring(0,pos_2p)),
					Integer.parseInt(time_year.substring(pos_2p+1)),
					0);
				else
					calendar.set(Integer.parseInt(time_year),
					((Month_STR.indexOf(month)+1+4)/4)-1,
					Integer.parseInt(day),
					0,0);
					
				return calendar.getTime().getTime();
				
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			return 0;
		}

.getTime().getTime()


Cycle

function clearValueTab()
{
	var ITD="";
	for(var i1 = 0; i1 < 3; i1++)
		for(var i2 = 0; i2 < 2; i2++)
			for(var i3 = 0; i3 < 2; i3++)
				for(var i4 = 0; i4 < 2; i4++)
				{
					ITD=(i1==0?'N':(i1==1?'S':'F'))
						+(i2==0?'N':'S')
						+(i3==0?'N':'S')
						+(i4==0?'N':'S');
					if(document.getElementById(ITD+"_T"))
						document.getElementById(ITD+"_T")
							.parentNode.style.display="none";	
				}
}

Hope that this stuff works. I’m not going to touch it.

Just found in the codebase of the big TLC company


Log

INFO |02 mag 2011 17:05:19| ZIdentityAsserter -  messageeeeeeeeeeeeeeee

Got it.

Found digging in a production logfile


With or without CRM

	var ccNoCRM=[
		 [0,-1,-1,-1,-1],
		 [1,1,1,0,0],
		 [2,1,1,0,0],
		 [3,1,1,0,0],
		 [4,1,1,0,0],
		 [5,0,0,0,0],
		 [6,0,0,0,0],
		 [7,0,0,0,0]
		];

	var ccYesCRM=[
		   [0,-1,-1,-1,-1],
		   [1,1,1,0,0],
		   [2,1,1,0,0],
		   [3,1,1,0,0],
		   [4,1,1,0,0],
		   [5,0,0,0,0],
		   [6,0,0,0,0],
		   [7,0,0,0,0]
		  ];

I think, it sunk my cruiser :/

Just found in the codebase of the big TLC Company