Flylib.com

Books Software

 
 
 

- page 29

for RuBoard

Summary

In this chapter, you learned about a number of formatting and coding conventions that can help you churn out better Transact-SQL. Many of these apply regardless of whether you're creating a stored procedure or some other type of SQL Server- related object. You shouldn't assume that the list presented here is comprehensive or even applicable to every development team in every situation. The point is this: Adopting and consistently following sound formatting and coding conventions can help you produce better code with less effort.

for RuBoard
for RuBoard

Chapter 3. Common Design Patterns

The harder it is to see the design in the code, the harder it is to preserve it.

Martin Fowler [1]

[1] Fowler, Martin. Refactoring:Improving the Design of Existing Code . Reading, MA:Addison-Wesley, 1999. Page 55.

In their book, Design Patterns , Erich Gamma and company (commonly known as the "Gang of Four" or GoF) propose that there are certain patterns in software design that expert developers regularly use and recognize in code written by others. Design Patterns seeks to formally catalog these patterns so that developers may benefit from them without spending years learning them the hard waythrough experience alone. The idea is to promote the discussion of and research into software design patterns as a distinct area of computer science so that, having been formalized , software design patterns can continue to evolve and proliferate as software engineering itself does.

The book The Practice of Programming espouses a similar philosophy. In it, author Brian Kernighan proposes that programming languages have idioms conventions that experienced developers use to build common coding elements. [2] These are similar to design patterns, but are finer in granularity. I like to think of them as mini patterns or pattern fragments . Idioms are more generic than design patterns. They have more to do with the language than with solving a particular type of problem.

[2] Kernighan, Brian. The Practice of Programming . Reading, MA:Addison-Wesley, 1999. Page 11.

Although both of these books discuss software design from the standpoint of object-oriented programming (OOP) languages, I have long believed that common design patterns and idioms also exist in query languages such as Transact-SQL. Expert developers regularly use certain common techniques to build the code they write. The purpose of this chapter is to catalog a few of these and to help jumpstart a discussion of Transact-SQL design patterns and idioms. For succinctness, idioms and design patterns are discussed together. Just keep in mind that, although closely related , they're really two different things.

for RuBoard
for RuBoard

The Law of Parsimony

In philosophy, the law of parsimony (a.k.a., Ockham's razor ) states that the simplest of two or more competing theories is preferred to more complex ones. To the software designer, this means that the simplest approach to providing the required functionality is usually the best. Creating unnecessary complexities and obfuscation does not make one a better coder . In fact, it's exactly the opposite . From my experience, the best programmers, regardless of language, are those with a gift for being able to create solutions to intricate problems from simple inventions and techniques. The best code has a sense of elegance to it that derives from its simplicity. It is better code because it is simpler code.

In his book Refactoring: Improving the Design of Existing Code , Martin Fowler had this to say on the subject: "You build the simplest thing that can possibly work." [3] I agree with this statement to a point. A concern I have is that one man's simple is another man's complex. Sizing up the complexity of a proposed solution can be a completely subjective task. What's obvious and intuitive to you may confuse me to no end. Another concern I have is that oversimplifying a problem or failing to consider the long- term ramifications of the design decisions you make can cause problems down the road. The law of parsimony should never be used as an excuse to abandon forethought and the grasp of the overall picture.

[3] Fowler, Martin. Refactoring:Improving the Design of Existing Code . Reading, MA:Addison-Wesley, 1999. Page 68.

You'll see the law of parsimony in evidence in many of the idioms and design patterns covered in this chapter. One of the most productive things you can do to write better code and to clean up existing code is to remove unnecessary complexity. Doing so helps you not just now, but six months from now when you or someone else must work on the code again. It is an investment in the future health of your code. If a loop to execute a block of code a given number of times runs forward in some places and backward in othersfor no apparent reasonthe code contains needless complexity. People reading through it are forced to note that, even though the code looks different, it actually performs the same function as another, more straightforward code block. A prime tenet of good software design is the elimination of differences that make no differencevariations without a purposeso that what remains are those worth noting.

And, although there may be many ways to do something, generally the simplest, most straightforward approach is best. We have enough complex software engineering problems to solve without creating them for ourselves through poor coding practices.

for RuBoard