As promised

… in Condition, here the code contained by the if statement.

It’s a little bit convoluted, but it’s so well commented.

	/**/
	if (cod_tipo_uo.equals("21") || !cod_tipo_uo.equals("21")){
		if (itemComboTip.equals("")){
			ptfType = "";
			ptfSubTtypel = "";
			if (cod_tipo_uo.equals("A3")|| cod_tipo_uo.equals("94")){
				if (!ContextUtil.consumeArea(request, "CodItem").isEmpty() && 
						ContextUtil.consumeArea(request, "CodItem")!= null	){
					String val = (String) ContextUtil.consumeArea(request, "CodItem")
						.toArray() [0];
					val = val.substring(3,val.length());
					ContextUtil.informArea(request, val, "searchType");
	/***/ 
					if (!val.equals("ALL")){					
						ptfType = val.substring(0,2);
						if (val.length()== 5){
							ptfSubTtypel = val.substring(3,5);
						}else if (val.length()== 2){
							ptfSubTtypel = "";
						}	

						if (ptfSubTtypel.equals("")){
							ContextUtil.informArea(request,cod_tipo_uo 
								+'-'+ ptfType, "searchType");					
						}else ContextUtil.informArea(request,cod_tipo_uo 
							+'-'+ ptfType+'-'+ptfSubTtypel, "searchType");
					}				
	/****/							
				}	
			}else ContextUtil.informArea(request, "ALL", "searchType");
		}
	}

Still no clue of what it was about.

(I think) Actually in the production code of the Big Bank, I did not dare touch it


There’re not going to be…

… more than one thousand!

	var i;
	for (i = 0; i < 1000; i++){
		if (document.getElementById('row_' + i) != null){
			...
		}
	}

If you have to roll through a table, and you don’t know how many elements it has, choose an arbitrary high number and check for nulliness, all right.

In the codebase of the Big Bank


How to iterate

The problem is ‘remove a certain item from a collection’.
If you remove an object and the counter keep going, you’re going to jump the next element.

The solution my predecessor come to:

	private void removeData(String code){
		String sp914code;
			
		for (int i = 0; i < sp914Codes.size(); i++){
			sp914code = sp914Codes.get(i).toString();
			if(sp914code.equals(code)){
				sp914Codes.remove(i);
				i=-1;
			}
		}
	}

Restart everytime from the very beginning, obvious.

I doesn’t dare touch this, even if I’d worked on that class, in THAT Big Bank


Date parsing perversion /3

If you have to remove 15 days from a date, which way you’re going to take?

   	switch(endMonth) 
   	{
		case '01':
			startDay = startDay = 31 - (15 - endDay);
			startMonth = '12';
			startYear = endYear - 1;
			break;
		
		case '02':
			startDay = startDay = 31 - (15 - endDay);
			startMonth = '01';
			startYear = endYear;
			break;
			
		case '03':
			var isBisestile = startYear%4;
			var offset = (isBisestile == 0)? 29: 28;		
			
			startDay = offset - (15 - endDay);
			startMonth = '02';
			startYear = endYear;
			break;
			
		case '04':
			startDay = 31 - (15 - endDay);
			startMonth = '03';
			startYear = endYear;
			break;
			
		case '05':
			startDay = 30 - (15 - endDay);
			startMonth = '04';
			startYear = endYear;
			break;
			
		case '06':
			startDay = 31 - (15 - endDay);
			startMonth = '05';
			startYear = endYear;
			break;
			
		case '07':
			startDay = 30 - (15 - endDay);
			startMonth = '06';
			startYear = endYear;
			break;
			
		case '08':
			startDay = 31 - (15 - endDay);
			startMonth = '07';
			startYear = endYear;
			break;
			
		case '09':
			startDay = 31 - (15 - endDay);
			startMonth = '08';
			startYear = endYear;
			break;
			
		case '10':
			startDay = 30 - (15 - endDay);
			startMonth = '09';
			startYear = endYear;
			break;
			
		case '11':
			startDay = 31 - (15 - endDay);
			startMonth = '10';
			startYear = endYear;
			break;
			
		case '12':
			startDay = 30 - (15 - endDay);
			startMonth = '11';
			startYear = endYear;
			break;   				
    }

I hope not this one.

Many thanks to the Big Bank for providing me such gems


Selection

	private String executeSelect(int action, Form mform,
			HttpServletRequest request) {

		switch (action) {
		case 1: {
			return pageChooser(mform, request);
		}
		case 2: {
			return pageChooser(mform, request);
		}
		case 4: {
			return pageChooser(mform, request);
		}
		case 3: {
			return pageChooser(mform, request);
		}
		}
		return pageChooser(mform, request);
	}

There’s a need to comment?

Source can’t be disclosed


Inline /2

Linebreakers have been added by me, it’ll mess formattation otherwise.

	            G2D.drawRoundRect(DrawArea.x+2*InfoDrawTask.space_internal,
	DrawArea.y+2*InfoDrawTask.space_internal,DrawArea.width-(4*InfoDrawTask.space_internal),
	DrawArea.height-(4*InfoDrawTask.space_internal),(
	getTM() instanceof WorkflowTask?stroke_LINE_DASH:stroke_LINE),
	InfoDrawTask.getColorST(getTM().getStatoTask()),false, dimarc,(FI==null?new GradientPaint(DrawArea.x,
	DrawArea.y,Color.white,DrawArea.x+DrawArea.width,DrawArea.y+DrawArea.height,COLOR_FILL_TASK):
	new TexturePaint(FI,DrawArea)),5);

Yes, that’s a 503 freaking scary characters long single line.

Actually in the production code of the well known italian TLC company


Date parsing perversion /2

Thanks Michele for sending in!

             public String formatDate (String value, String format)
             {
                       
                        String regexTrim = "[;:-]|[\\.]|[\\s]";
                        String[] splitted = value.split(regexTrim);
                       
                        int anno = 0;
                        int mese = 0;
                        int giorno = 0;
                        int ora = 0;
                        int minuti = 0;
                        int secondi = 0;
                        int millisecondi = 0;
                       
                       
                        for(int i=0; i<splitted.length; i++)
                        {
                                   switch (i) {
            case 0:  if(!splitted[0].trim().equals("")){anno = Integer.parseInt(splitted[0]);}; break;
            case 1:  if(!splitted[1].trim().equals("")){mese = Integer.parseInt(splitted[1]);}; break;
            case 2:  if(!splitted[2].trim().equals("")){giorno = Integer.parseInt(splitted[2]);}; break;
            case 3:  if(!splitted[3].trim().equals("")){ora = Integer.parseInt(splitted[3]);}; break;
            case 4:  if(!splitted[4].trim().equals("")){minuti = Integer.parseInt(splitted[4]);}; break;
            case 5:  if(!splitted[5].trim().equals("")){secondi = Integer.parseInt(splitted[5]);}; break;
            case 6:  if(!splitted[6].trim().equals("")){millisecondi = Integer.parseInt(splitted[6]);}; break;
            default: logger.error("FORMAT PLACEHOLDERS (FAC) - Error"); return value;
                                   }
                        }
                       
                        Calendar calendar = new GregorianCalendar(anno,mese,giorno,ora,minuti,secondi);
                        calendar.add(Calendar.MILLISECOND, millisecondi);
                       
                        Date date = calendar.getTime();
                       
                        SimpleDateFormat sdf = new SimpleDateFormat(format);
                       
                        value = sdf.format(date);
                       
                        return value;
                       
            }

At last, it works.

Michele found it in the codebase of our beloved major italian bank


You only have to choose

	private int chooseOperation(String n, String s, String c) {
		if(c != null && c.equals(ON)){
			if (n != null && !VOID.equals(n) && (s == null || s.equals(VOID))){
				return ADD;
			}
			if ((s != null || !"".equals(s)) && (n != null)){
					return UPDATE;
			}
		}else if (c == null || c.equals(FALSE)){
			if (!VOID.equals(s)){
				return DELETE;
			}
		}
		return NULL;
	}

Wait, what??

Found in the codebase of a italian public company


Perplex

Thanks Costa for the submission!

	if ( $clientid > 0 && $vas == '0') {
		if ($clientid == 0){
			...
		}
	}

I’m so perplexed that I’ve been in serious trouble about choosing a title.


Not If

	if(sp001.getDtrfrmms1() == null && !sp001.getDtrfrmms1().equalsIgnoreCase("")) {
		
		
		
	}
	else {
		date = df.format((String)sp001.getDtrfrmms1().get(0));
		
		--... cut, not interesting --
	}
	
	if(sp001.getDtrfrmms2() == null && !sp001.getDtrfrmms2().equalsIgnoreCase("")) {
		
		
		
	}
	else {
		date = df.format((String)sp001.getDtrfrmms2().get(0));
		
		--... cut, not interesting --
	}
	
	if(sp001.getDtrfrmms3() == null && !sp001.getDtrfrmms3().equalsIgnoreCase("")) {
		
		
		
	}
	else {
		date = df.format((String)sp001.getDtrfrmms3().get(0));
		
		--... cut, not interesting --
	}
	
	
	...
	
	
	if(sp001.getDtrfrmms12() == null && !sp001.getDtrfrmms12().equalsIgnoreCase("")) {
		
		
		
	}
	else {
		date = df.format((String)sp001.getDtrfrmms12().get(0));
		
		--... cut, not interesting --
	}

A true reversed logic

Found in the codebase of the big italian bank, but I couldn’t stand in front of that… I’d rewritten the class