This post originated from an RSS feed registered with Agile Buzz
by Steven E. Newton.
Original Post: Objects for "Newbies"
Feed Title: Crater Moon Buzz
Feed URL: http://www.cmdev.com/buzz/blosxom.cgi?flav=rss
Feed Description: Views and experiences from the software world.
Simple coding tips for junior to intermediate programmers new to objects.
It's not a big deal to create a new class or method -- especially not a new method.
Control structures don't appear as often in good OO code as in good procedural good.
A method can be thought of as a function that retains state across invocations and can be called over and over again without having to re-initialize and pass in every parameter.
Whenever a chunk of code becomes a good candidate for moving into a function in procedural terms, consider creating a class instead. This is especially true if it appears that the function would take many parameters and be called from many places.
Whenever it appears that code might benefit from adding a comment, add a new method instead, and move the code that the comment would have applied to into the new method. Name the method something that summarizes the intent of the comment.
If code is calling a method on another class and then calling a method on the returned value, consider creating a method on the other class that does the work.
Consider this exercise -- take a substantial piece of good procedural
code and turn every function into a class. Not necessarily with the goal
to produce an ideal implementation, but to explore the nature of objects.