wiki:codekata/refactor

Version 3 (modified by zhangyihui, 12 years ago) (diff)

--

2013年9月27日代码道场活动纪实
代码道场的参与者:秦鸿源,陈阳,王安宁,丁健勇,李炳岳,黄志强,李剑文,张艺辉,江毅超, 刘必晓, 李峰
地点:4G会议室
本次代码道场的题目,希望与实际工作有一定的联系,从而能激发大家的热情,所以这期代码道场的题目为论坛系统其中一个方法的重构
题目如下:

重构如下代码:

public static String htmlUrl(Object object, int type) {
		boolean isIt = false; 
		if (isIt && type != SystemConstant.TYPE_WAP ) {
			return ""; 
		} else {

	        switch (type) {
	            case SystemConstant.TYPE_WEB:
	                rootUrl = rootUrl;
	                break;
	            case SystemConstant.TYPE_WAP:
	                rootUrl = rootUrl; 
	                break;
	            default:
	                break;
	        }

	        if ("".equals(rootUrl) || rootUrl == null) {
	            return "";
	        }

	        if (object instanceof Forum) {
	            Forum forum = (Forum) object;
	            if (!isEmpty(forum.getRedirectUrl()) && SystemConstant.TYPE_WEB == type) {
	                return forum.getRedirectUrl();
	            } else {
	                return rootUrl + "/forum-" + forum.getFid() + ".html";
	            }
	        } else if (object instanceof Topic) {
	        	//销售配合,此帖子跳转到指定链接
	        	if(SystemConstant.AUTO_APP_NAME.equals(appName) && ((Topic) object).getTid() == 4018911){
	        		return "http://ad.doubleclick.net/clk;275446626;102403463;a?http://bbs.pcauto.com.cn/topic-4018911.html";
	        	}
	        	if(SystemConstant.AUTO_APP_NAME.equals(appName) && ((Topic) object).getTid() == 4027940){
	        		return "http://ad.doubleclick.net/clk;275446638;102403466;g?http://bbs.pcauto.com.cn/topic-4027940.html";
	        	}
	        	if(SystemConstant.AUTO_APP_NAME.equals(appName) && ((Topic) object).getTid() == 4040395){
	        		return "http://ad.doubleclick.net/clk;275446642;102403468;d?http://bbs.pcauto.com.cn/topic-4040395.html";
	        	}
	            return rootUrl + "/topic-" + ((Topic) object).getTid() + ".html";
	        } else if (object instanceof Post) {
	            return rootUrl + "/post-" + ((Post) object).getTid() + '_' + ((Post) object).getPid() + ".html";
	        } else if (object instanceof String) {
	            return (String) object;
	        } else if (object instanceof User) {
	            return  "/" + ((User) object).getUid();
	        } else if (object instanceof ftree.Forum) {
	            ftree.Forum forum = (ftree.Forum) object;
	            return rootUrl + "/forum-" + forum.getFid() + ".html";
	        } else {
	            return "";
	        }
		}
    }
    
    public static boolean isEmpty(String text){
    	 return (null == text || "".equals(text));
    }
}}}[[BR]]


活动开始前,咱们组的陈阳童鞋一马当先,只管抢占键盘,是的,他只用键盘,就已经将需要重构的方法提炼出来,完全忽略鼠标,看得我眼睛一眨一眨的,[[BR]]
虽然用时比较长,但还是暗下决定要学好vi的使用。[[BR]]
题目一出来,只见鸿源同志如风一般坐在了陈阳童鞋刚刚坐的位置,立马听到一连串的键盘音,声音过后,一个重构的新方法名就出来了,还带着测试类出来,[[BR]]
跟着,与他组队的炳岳同志讨论下一步该怎么做。当然,这一过程是非常激烈的,但慢慢地,他们的意见终于统一了,想法达成一致,这时,鸿源同志二话不说[[BR]]
马上又敲起键盘,将想法转变成测试代码。[[BR]]
时间是无情的,正当键盘敲得起劲的时候,每人7分钟的演练时间到了,这时咱们只有将想写的,但不够时间写的代码,换成语言,去跟下一位进行代码演示的童鞋交流[[BR]]
非常像头脑风暴,大量信息量同时在大脑出现,并且需要快速思考,学到的不仅仅是一段代码,还有思考问题的方法。[[BR]]
现在,咱们来看下测试代码[[BR]]

{{{
import junit.framework.Assert;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class FunctionsTest {

	private String appName = null;
	private String itAppName = null;
	private String rootUrl = null;
	private String ucRoot = null;
	private Topic topic = null;
	private Forum forum = null;
	private Post post = null;
	private User user = null;

	@Before
	public void setUp() {
		appName = "pcauto";
		itAppName = "itbbs";
		rootUrl = "http://bbs.pcauto.com.cn";
		ucRoot = "http://my.pcauto.com.cn";
		topic = new Topic();
		forum = new Forum();
		post = new Post();
		user = new User();
		forum.setFid(123L);
	}

	@Ignore
	public void testHtmlUrl() {
		// 变化的对象 Object,rootUrl,appName,
		// Object是变化的,rootUrl,appName=auto 是固定的
		Assert.assertEquals(rootUrl + "/topic-1342.html",
				Functions.htmlUrl(topic));
		Assert.assertEquals(rootUrl + "/404.html",
				Functions.htmlUrl((Topic) null));
		Assert.assertEquals(rootUrl + "/forum-123.html",
				Functions.htmlUrl(forum));
		Assert.assertEquals(rootUrl + "/post-789789_2424242.html",
				Functions.htmlUrl(post));
		Assert.assertEquals(ucRoot + "/2424",
				Functions.htmlUrl(user));
		Assert.assertEquals("1234567",
				Functions.htmlUrl("1234567"));

	}

	@Test
	public void testHtmlUrlForum() {
		Assert.assertEquals(rootUrl + "/forum-123.html",
				Functions.htmlUrl(forum));
		
//		Assert.assertEquals(rootUrl + "/f123.html",
//				Functions.htmlUrl(forum, rootUrl, itAppName));

		Assert.assertEquals(rootUrl + "/404.html",
				Functions.htmlUrl((Forum) null));

	}

	@Test
	public void testHtmlUrlTopic() {
		Assert.assertEquals(rootUrl + "/topic-1342.html",
				Functions.htmlUrl(topic));

//		Assert.assertEquals(rootUrl + "/1342.html",
//				Functions.htmlUrl(topic, rootUrl, itAppName));
		
		Assert.assertEquals(rootUrl + "/404.html",
				Functions.htmlUrl((Topic) null));
	}

	@Test
	public void testHtmlUrlUser() {
		Assert.assertEquals(ucRoot + "/2424/",
				Functions.htmlUrl(user));
		
//		Assert.assertEquals(rootUrl + "/123.html",
//				Functions.htmlUrl(user, ucRoot, itAppName));

		Assert.assertEquals(rootUrl + "/404.html",
				Functions.htmlUrl((User) null));
	}

	@Test
	public void testHtmlUrlPost() {
		Assert.assertEquals(rootUrl + "/post-789789_2424242.html",
				Functions.htmlUrl(post));
		
//		Assert.assertEquals(rootUrl + "/p789789_2424242.html",
//				Functions.htmlUrl(post, rootUrl, itAppName));

		Assert.assertEquals(rootUrl + "/404.html",
				Functions.htmlUrl((Post) null));
	}

	@Test
	public void testHtmlUrlString() {
		Assert.assertEquals("123456",
				Functions.htmlUrl("123456"));
		
//		Assert.assertEquals("123456",
//				Functions.htmlUrl("123456", rootUrl, itAppName));

		Assert.assertEquals(rootUrl + "/404.html",
				Functions.htmlUrl((String) null));
	}
	
}
}}}[[BR]]


















}}}