My Dev weblog

Just another development weblog

10 Ways to Improve Your Code

with one comment

I’ve just finished   watching a very interesing presentation recorded during QCon SF 2008. it was delivered by Neal Fred. a senior application architect at ThoughtWorks. He is the author of a lot of development  books i wanted to share it on the form of points in case someone doesn’t have time for watching the presentation.

So, let’s begin
**************
**************

10 ways to improve your code:

1. Use Composite Methods: divide the program into SMALL general methods, each of them carries on ONE identifiable task. This will result in a program of small methods with few (6 to 7) lines each
the Benefits:
a. Refactoring will be much easier, imagine refactoring one method that connects to a DB, performs a query against the DB  and fetches the  data. Nightmare isn’t it!!    So, to apply the composite method concept with the previous case, we will create 3 small methods each of them doing ONE task, one for opening the DB connection, one for querying the DbB and the last one is for handling the ResultSet, then you call these 3 methods on your original method that used to do everything on by itself.
b. Making the methods as abstract as possible helps in reusing them.
c. Unit Testing Small dummy methods with one task to do is easy.
d. Method names will become documentation.

2. Do TDD (Test Driven Design/Development): Test Driven Development means writing Test before writing the code ( the test Class is the first consumer of the class).
the Benefits:
a. building the test affects the Design of the actual application. it tells you wah u need exactly before doing it.
b. Better interaction/integration between the app components. assume you want to create a customer class that uses an Order class, So, when you test-first, You are forced to think how customer will interact with order AT design time before you write any code for both classes. that kind of thinking eliminates alot of the integration problems that occur when you write the 2 separate components and try to use them in the real world.
c. encourages you to write Composed methods: by writing a simple small method to pass the test and evolving it till it becomes ready.
d. TDD code tends to be cleaner and has a better complixity.

3. Do Static Analysis: there exist some tools to do some analysis of code written in almost every programming language. for example “FindBugs” does Byte-code- level analysis of java code. It  is able to find bug patterns that might be in your code.

4. Assure Good Citizenship between classes: make sure that Classes interact with each others in a civilized manner. Don’t Mix static and state methods in one place. Singleton Pattern is evil !! it is the OOP alias of the Global variable we used to use back in the days. the problem with singleton is that it mixes the states and the static methods together. it is hard to test/debug. mixes responsibilities.
the alternative to using singleton:
create a Plain object POJO that holds the state, Logic ..etc
and then create a factory to create one an only one object of that POJO and insure that.

5. YAGNI : You Ain’t Gonna Need It!. Write only and only write theings that you actualy need. this minimizes the Complixity of the application – it will grow by time as specs change, so don’t make it even more complex in advance! – . Don’t create your OWN FRAMEWORK or reinvent the wheel. the Best frameworks are the ones Harvested from existing code ! because at least they proved to work once, the concept of Composite method is one case of that harvesting.

6. Question Authority: alot of things are taken as they appear. without raising questions,Why is that done that way. IT’S THE COMPANY POLICY/CULTURE. they tend not to break that things so-called culture without even knowing why are they culture.
ex: managers think pair programming produces code half as fast. But studies show that although it is 50% slower, it is also 50% less in defects and much higher quality.
THE POINT is: dont take everything for granted. DO explore and make expierments. find the goog points and push them to their extreme.

7. SLAP (Single Level Of Abstraction Principle): every thing should be in the same level of abstraction.

8. There are other Languages/frameworks that can solve this particular problem better: it is a common mistake to think that one programming language can do everything with ease. writing MAssive multithreaded code is hard in Java/.NET. instead, use a functional language that runs on the same platform – JRE as in Jaskel or the .NET as in F#-.
the point is: Be smart and use the most appropriate tool to give you the best solution to yout problem.

9. Explore the Backalleys/hidden doors: learn the special features of the language. it will make you solve unusual problems sharper. things like Reflection/regular expressions… etc can save the day. then spread your knowlege to others in the project.

10. Anti-objects: change your perspective, they way you look at the problem and don’t get stuck in one methodology/way of thinking to solve all your problems. it might not give you the optimal solution to the given problem. Sometimes unusual solutions that dont stick to a uniform way of thinking work best.

******************************
*****************************

the presentation : http://www.infoq.com/presentations/10-Ways-to-Better-Code-Neal-Ford

Advertisement

Written by topquarck

April 17, 2009 at 8:32 pm

Posted in soft skills

One Response

Subscribe to comments with RSS.

  1. I haven’t read the post yet …. but the comic gamda gdaaaaaan :D :D :D

    OsAmA

    June 7, 2009 at 10:49 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.