homedark

Foundations of Programming 2 - Chapter 1 - Quality and Efficiency

Dec 20, 2010

The book is being made available on github at the same time as here (for your contributing convenience). Please view for how you can help

Chapter 1 - Quality and Efficiency

"Quality is never an accident; it is always the result of intelligent effort." - John Ruskin

Pragmatically, the reason I, and I assume most programmers, are keen to learn is to improve the quality of the code we write as well as our efficiency. I don't know any programmer who spends time learning new approaches and technologies with the explicit goal of being able to write worse code, or find ways to slow his or her pace. How do we measure efficiency and quality though? What, as creators, do we consider to be our goals?

Management-type might solely align quality with client/user satisfaction. While that's certainly important, good programmers are almost always the stakeholders with the highest quality expectation as well as being the most critical. I find it difficult to put quality, from a programmers perspective, into words; yet, I'm sure most of us not only have a similar definition, we can all easily recognize it (or the lack of it). Quality is about coming up with an elegant solution to a problem - both at the micro (an individual behavior) and macro levels (the system as a whole). What's elegant, then? It varies on the context of the problem, but generally, an elegant solution is both the simplest possible one as well as the most explicit and easy to understand.

The more I've programmed, the more I've noticed something truly startling. Not only is the simplest solution obviously the easiest to implement, its almost certainly the easiest to understand, maintain and change. Simple solutions also tend to have better performance characteristics than more complex approaches. This is particularly astounding when you find yourself knee deep in an over-engineered mess where the original intention for flexibility, performance and efficiency are sabotaged by artificial complexity. That doesn't mean I advocate, what is amusingly termed, duct-tape programming (though I am more forgiving of it since duct tape programmers just don't know any better, whereas architecture astronauts fail precisely for the opposite reason, they think they know better). How then do we know whether a given solution is simple enough? How do we avoid being duct tape programmers while not turning into architecture astronauts?

I wish I could tell you the answer. It isn't that I don't know. The problem is that I have to preface it with a word of warning - else I risk having you roll your eyes and give up on this book. I know a lot of you are sick of hearing about it, but I beg that you hear me out and recognize that I'm probably not saying what you think I'm saying. Deal? So the secret to writing quality code is to make sure your code is testable. WAIT...don't roll those eyes, I'm not saying that you have to test your code (and I'm certainly not saying that you shouldn't), I'm just saying that if, theoretically, you were to test your code, it shouldn't be hard to do. Code can be testable without actually testing it - and ultimately, at the most fundamental level, that's what you should be aiming for.

Much of this book is going to be devoted to testability. However, we largely do so as a measure of quality. We'll certainly cover the other benefits of writing tests, but do not mix testability as a quality metrics with writing tests. Its a lot like writing maintainable code that doesn't really require maintenance - its a goal, not an actual activity. Now its true that the best way to discover whether a solution is testable is to actually write tests. I do hope that I can convince you to give it a try; in the last three years, becoming an effective test writer has been one of the two things which has most helped me elevate my craft. Nonetheless, its entirely possible to look at a method, or a system, and gauge testability, and thus quality, without actually writing a test.

For a long time I thought the solution really was that simple - write tests, and the quality of your code will greatly improve. More recently though I've realized that not all tests are created equally and its entirely possible, if not common, to have really poor tests. Understanding what makes a good test, and thus what makes code testable, comes with experience, and hopefully, through future chapters, I can help you avoid some of the pitfalls I ran into.

Testability as a Quality Metric

I don't blame you if you're skeptical. All I can do is promise that, while we might spend some time on writing effective tests, I won't try to make you feel like you are doing something wrong if you choose not to test. Also, I can show you a simple example of what I mean:

//An example of hard to test, and thus low-quality, code
private void LoginButton_Click(object sender, EventArgs e)
{
	User user = SqlServerDataStore.FindUser(UserName.Text, Password.Text);
	if (user != null)
	{
		Session["userId"] = user.Id;
		Response.Redirect("~/members/index.aspx");
	}
	else
	{
		ErrorMessage.Text = "Invalid UserName or Password";
	}
}

(If this was a short-lived/throwaway app, we might forgive it. Although I must say that in my experience, skilled programmers write clean and thoughtful code regardless of the expected life expectancy of our code. This is likely due to the fact that we all know prototype code can end up having an embarrassingly long shelf-life.)

This snippet is far from perfect. If we specifically look at it from a testing point of view, we'll see a number of dependencies which'll make this hard to test, such as `SqlServerDataStore.FindUser` and `Response.Redirect`, to name a few. Sadly, many of those are baked into the framework - which is a one of the reasons many .NET developers feel so strongly against WebForms. In this particular case, the near impossibility of testing tells me that the code will be difficult to change and maintain.

A lot of the best practices you hear thrown around, like YAGNI (you aren't going to need it), low coupling and high cohesion, or SOLID, can be measured by looking at your code and thinking of how you'd test it. A method that does too much, potentially violating both YAGNI and high cohesion, will require a painful amount of setup code to test (as well as prove easy to break).

Efficiency: Not As Simple As It Seems

I might not have sold you on the testability as a quality metric (yet), but I'm confident you agree that quality is a supremely important aspect. Our other pragmatic-driven goal is efficiency, because great code can't wait forever. You'd hope that efficiency, with respect to learning and improving, would be easy to measure: can I achieve XYZ now faster than I could before? This isn't the case.

First, and most dangerously, programmers don't always realize that a better way might exist. Let's be honest, a lot of programmers refuse to accept that very possibility. Its hard for people, especially those not passionate about what they do, to accept that a better way exists - it not only threatens their comfort zone, but potentially their careers. I was in school when Java started gaining popularity and was impressed by the close-mindedness of programmers who refused to accept that automatic garbage collection might, after working out some quirks, be a useful (let alone standard) general purpose solution. I find it particularly ironic now when Java developers refuse to accept that that dynamic languages might become the new Java (especially since they often raise the same concerns which C++ developers once had about Java).

Second, regardless of how much more efficient a new technology or approach is, you'll be more efficient in what you know versus what you're learning. My strategy to surmount this is to take my time and recognize the process as a long term investment. Also if you are clever about it, you can probably fit some R&D into your day to day work. By clever, I don't mean deceitful. I mean finding secondary, none critical systems. That monitoring system you've talked about with colleagues for a while but never have the time to do, or that prototype you've been asked to write. The exact approach you take will be personalized to your preferred way to learn. All I can say is that you shouldn't expect an increase in efficiency over night.

In This Chapter

That was a lot of text with too little code. This chapter was the groundwork for following chapters, which rely heavily on code and examples. We defined what quality means to us as well as the metric we'll use to measure it. We looked at efficiency and discovered that while it might seem easy to gauge, it really isn't. Take a break and consider some of the code you've recently written. If you did test it what could have made your tests simpler and less likely to fail as your system changes? If you didn't, can you think of any pain points you might have run into if you decided to test it now?

"But the grooms, or 'bettos,' as the Japanese call them, are not the only ones who indulge in tattooing. You will see many of the 'sendos,' or boat-coolies, thus marked, but in a less degree than the bettos. Perhaps it is because the grooms are obliged to run so much, and consequently wish to lay aside all garments. As they must wear something, they have their skins decorated in this way, and thus have a suit of clothing always about them. Joe Johnston's shattered army was at Jackson, about forty-five miles to northward; beleaguered Vicksburg was in the Northwest, a trifle farther away; Natchez lay southwest, still more distant; and nearly twice as far in the south was our heartbroken New Orleans. We had paused to recuperate our animals, and there was a rumor that we were to get new clothing. Anyhow we had rags with honor, and a right to make as much noise as we chose. "Nonsense, my dear fellow. Now let me open your eyes. Behold the great force of a man who is gifted with second sight. Where did you get those notes? Was it not on the same evening as the murder?" "Oh, nothing," came the reply. "Only I was so silly as to place the wrong end of my cigarette in my mouth and burnt my lips. What's tuberose?" Reviewing these mechanical conditions, we may at once see sufficient reasons for the platen movement of planing machines; and that it would be objectionable, if not impossible, to add a traversing or cutting action to tools already supported through the medium of eight joints. To traverse for cutting would require a moving gib joint in place of the bolted one, between the standards and main frame, leading to a complication of joints and movements quite impracticable. And disadvantaged aye begins the strife. We have already seen how this fundamental division is applied to the universe as a whole. But our philosopher is not content with classifying the phenomena as he finds360 them; he attempts to demonstrate the necessity of their dual existence; and in so doing is guilty of something very like a vicious circle. For, after proving from the terrestrial movements that there must be an eternal movement to keep them going, he now assumes the revolving aether, and argues that there must be a motionless solid centre for it to revolve round, although a geometrical axis would have served the purpose equally well. By a still more palpable fallacy, he proceeds to show that a body whose tendency is towards the centre, must, in the nature of things, be opposed by another body whose tendency is towards the circumference. In order to fill up the interval created by this opposition, two intermediate bodies are required, and thus we get the four elementsearth, water, air, and fire. These, again, are resolved into the antithetical couples, dry and wet, hot and cold, the possible combinations of which, by twos, give us the four elements once more. Earth is dry and cold, water cold and wet, air wet and hot, fire hot and dry; each adjacent pair having a quality in common, and each element being characterized by the excess of a particular quality; earth is especially dry, water cold, air wet, and fire hot. The common centre of each antithesis is what Aristotle calls the First Matter, the mere abstract unformed possibility of existence. This matter always combines two qualities, and has the power of oscillating from one quality to another, but it cannot, as a rule, simultaneously exchange both for their opposites. Earth may pass into water, exchanging dry for wet, but not so readily into air, which would necessitate a double exchange at the same moment. 192 He helped her out. "I have drifted in a way," he went on to explain. "I left home when I was a mere boy, and the spirit of savagery and unrest laid hold of me. I can't break away. And I'm not even sure that I want to. You, I dare say, can't understand." Yet he felt so sure, for some reason, that she could that he[Pg 71] merely nodded his head when she said briefly, "I can." "Then, too," he went on, "there is something in the Indian character that strikes a responsive chord in me. I come of lawless stock myself. I was born in Sidney." Then he stopped short. What business was it of hers where he had been born? He had never seen fit to speak of it before. Nevertheless he intended that she should understand now. So he made it quite plain. "Sidney was a convict settlement, you know," he said deliberately, "and marriages were promiscuous. My grandfather was an officer who was best away from England. My grandmother poisoned her first husband. That is on my mother's side. On my father's side it was about as mixed." He leaned back, crossing his booted legs and running his fingers into his cartridge belt. His manner asked with a certain defiance, what she was going to do about it, or to think. "Naw; git out. Don't bother me with no questions, I tell you," impatiently said a man in citizen's clothes, who with arms outspread was signalling the switching engines. "'Tain't my business to give information to people. Got all I kin do to furnish brains for them bull-headed engineers. Go to that Quartermaster you see over there in uniform. The Government pays him for knowin' things. It don't me." As little Pete dropped to the ground, his nervous finger touched the trigger and his gun went off up in the air. The others took this as a cue, and banged away as rapidly as they could get their muskets off. "I am no master," Cadnan said wearily. "I am a slave." "The people know," Dodd said. "It's out. It's all out. About the slavery. Is that what you mean?" Cadnan peered at him, half-fearfully. "You are a master." One did not give orders to masters, or argue with them. "Why not? There's naun shameful in it. Munds's brother did it for twenty years. And think of the difference it'll m?ake to usthirty pound or so a year, instead of the dead loss of Harry's keep and the wages of an extra man beside. I tell you, mother, I wur fair sick about the farm till I thought of this." "Ben, I swear I'm your true wife." Reuben started, and Pete awoke noisily. Harry was frightened and dropped his string, crying because he could not find it. The knock came again, and this time Pete crossed the room yawning, and opened the door. c0c0.com.cn
eunay.com.cn
rz-tennis.com.cn
boyu2000.com.cn
tjbjgs.com.cn
www.szzbzx.org.cn
www.jhykt.com.cn
tlqi.com.cn
www.jaak.com.cn
www.lpxo.com.cn
中韩欧美一级一中文字暮 _黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 _毛片机地男男性行为免费视频播放九九欧美一级毛欧美片 _啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中韩欧美一级一中文字暮 中文字幕第十九页 中央游月中文字幕 中文字幕 第9页 中文字幕午夜福利 黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 中文字幕欧美日韩 中村知惠中文字幕 啪啪啪在线视频 中文字幕亚洲综合 中文字幕先锋资源 中文字幕 青青草 中文字幕资源网站 中韩欧美一级一中文字暮 中文字幕卡通动漫 中文字幕理论电影 免费视频播放 中文字幕资源在线 毛片免费观看 黄色一级电影片 中国美女一级看片 中文字幕先锋影音 黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 欧美在线不卡 中文字幕巨乳有码 啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中文字幕第86页 中文字幕91在线 黄色一级录像片 中文字幕久荜在线 黄色一级欧美片 毛片视频在线 中文字幕 第7页 中文字幕每日更新 毛片免费试看 中文字幕巨乱亚洲 中韩欧美一级一中文字暮 啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中韩欧美一级一中文字暮 毛片机地男男性行为免费视频播放九九欧美一级毛欧美片 黄色一级录像带 中文在线视频观看 中文字幕Av电影 啪啪啪男女视频 中文字幕 新妈妈 中国一级特黄大片 中文字幕久久视频 中文字幕黄色视频 中国黄色一级大片 中文字幕手机看片 中文字幕伦理在线 欧美一级毛 中韩欧美一级一中文字暮 免费视频播 啪啪免费视频网站 中文字幕男人天堂 黄色一级伦理片 中文字幕亚洲在线 中文字幕视频不卡 毛片免费网址 中国一级黄色大片 中文字幕亚洲情字 啪啪视频在线播放 啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中韩欧美一级一中文字暮 啪啪免费在线视频 亚洲AV无码天堂 中文字幕丝袜美腿 毛片免费视频 毛片在线电影 毛片在线不卡 啪啪啪在线播放 中文字幕伦理电影 中文字幕中文字幕 中文字幕一级在线 毛片免费基地 中文字幕综合影院 中文字幕手机在线 中文字幕视频在线 毛片机地男男性行为免费视频播放九九欧美一级毛欧美片 中国一级特大黄片 中文字幕乱码视频 中国成人在线视频 啪啪啪视频欧美 中国一级黄色电影 啪啪啪性爱动态图 啪啪啪欧美视频 黄色一级片播放 中文字幕乱码免费 黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 黄色一级毛毛片 中文娱乐在线视频 黄色一级黄色片 中文字幕乱伦电影 黄色一级片aa 中文字幕第十七页 男男性行为免费视频播放 黄色一级片观看 毛片免费电影
кŷһһĺ _ƬBBAVwww޹ŷ߲һ _ëƬΪƵžžŷһëŷƬ _žĿƬɫۺѸƵ60ŷϸһë кŷһһĺ Ļʮҳ Ļ Ļ 9ҳ Ļҹ ƬBBAVwww޹ŷ߲һ Ļŷպ д֪Ļ žžžƵ Ļۺ ĻȷԴ Ļ ĻԴվ кŷһһĺ Ļͨ Ļ۵Ӱ Ƶ ĻԴ ëƬѹۿ ɫһӰƬ йŮһƬ ĻȷӰ ƬBBAVwww޹ŷ߲һ ŷ߲ Ļ žĿƬɫۺѸƵ60ŷϸһë Ļ86ҳ Ļ91 ɫһ¼Ƭ Ļ ɫһŷƬ ëƬƵ Ļ 7ҳ Ļÿո ëƬԿ Ļ кŷһһĺ žĿƬɫۺѸƵ60ŷϸһë кŷһһĺ ëƬΪƵžžŷһëŷƬ ɫһ¼ Ƶۿ ĻAvӰ žžžŮƵ Ļ йһػƴƬ ĻþƵ ĻɫƵ йɫһƬ ĻֻƬ Ļ ŷһë кŷһһĺ Ƶ žžƵվ Ļ ɫһƬ Ļ ĻƵ ëƬַ йһɫƬ Ļ žžƵ߲ žĿƬɫۺѸƵ60ŷϸһë кŷһһĺ žžƵ AV Ļ˿ ëƬƵ ëƬߵӰ ëƬ߲ žžž߲ ĻӰ ĻĻ Ļһ ëƬѻ ĻۺӰԺ Ļֻ ĻƵ ëƬΪƵžžŷһëŷƬ йһشƬ ĻƵ йƵ žžžƵŷ йһɫӰ žžž԰̬ͼ žžžŷƵ ɫһƬ Ļ ƬBBAVwww޹ŷ߲һ ɫһëëƬ Ƶ ɫһɫƬ Ļ׵Ӱ ɫһƬaa Ļʮҳ ΪƵ ɫһƬۿ ëƬѵӰ
ENTER NUMBET 0018