Re: best practice for structure of code
Cory Foy <[email protected]>
| Newsgroups | gmane.org.user-groups.trijug.juglist |
|---|---|
| Message-ID | <[email protected]> |
Douglas Ivers wrote:
> If I need to execute a section of code more than once (not in a loop),
> then I of course create a named method so that I don't have multiple
> copies of the code. On the other hand, I sometimes create methods
> that exist solely for readability and are only called once, see below
> for example. Is there any drawback to this style of code? Are there
> differences in opinion, or is there a widely accepted best practice?
>
> public void init() {
> parseConfigFile()
> loadImages()
> loadData()
> buildGui()
> }
This is exactly what I do. The code may not initially be written that
way, but I often refactor to this.
> This often leads to class fields that could otherwise be local
> variables, which I see as a slight drawback.
It may also be a smell that your class is trying to do too much. The
Single Responsibility Principle
(http://www.objectmentor.com/resources/articles/srp.pdf) comes to mind.
> I've also experimented with unnecessary braces to improve readability,
> but I suspect this is very unconventional. For example,
I never would have thought about that, but I would tend to be against
that style.
Bob Martin just wrote a book called "Clean Code" that I just got my copy
of. It addresses many of these issues. The important thing is that your
team agree on the standards and be consistent about them.
--
Cory Foy
http://www.cornetdesign.com
http://www.agileflorida.com