views:

516

answers:

16

I recently wrote a small tool to generate a class for each tier I hand write for the boring "forms over data" work where I spend almost 90% of my time (depressing I know) ... more on this as the economy improves ;)

My question is this - will using this tool instead of hand typing all this code from day to day actually hurt me as a developer? I feel like I will always be making changes to this tool and thus I "should" stay on top of the patterns used/ choices made/ etc... but some small part of me feels like I might loose my edge ... am I wrong?

+5  A: 

You are most definitely not wrong. I use code generators anywhere I can - I currently use CodeSmith to create my DAO's by looking at the database.

What edge are you afraid of losing? In my mind going to code generation is actually giving you an edge.

Otávio Décio
+12  A: 

If the tool can spit the code out without thought, then it probably saves you lots of thoughtless typing.

Writing the tool in the first place requires thinking, so I'd guess you'd be more "on the edge" maintaining and writing the tool.

Gilad Naor
I Agree. I use code generators every day. It has dramatically reduced the time I need to write the boring/tedious part of the application. I can highly recommend using Ruby as a code generator tool. I use internal DSL's to generate both C++ and C# code.
MB
+2  A: 

Generate what you can. Code generation is one of the best tools I've picked up over the last 2 or 3 years. Typing the same code over and over (or copy and pasting it) is prone to error.

Jim Blizard
+1  A: 

No. Why do you think IDE's are so popular. Imagine if all the people who use Visual Studio had to programmatically create the GUI's without help from the IDE, it'd be terrible. I would be willing to bet most people who use VisualStudio won't know how to manualy create the forms they're creating in the IDE. But there's nothing wrong with that.

Malfist
Mauricio Scheffer
*sigh* I never said people used VS because of the GUI creation tools, I just state that people who used VS could use the GUI creation tools, and are therefor saved a lot of time.
Malfist
+1  A: 

I believe in code generation wherever possible to remove the rote tasks of programming. You will not lose your edge, you will probably become a better programmer because you will spend more time working on the important and interesting stuff.

BTW, your tool sounds interesting. Have you released it anywhere?

Rob Prouse
haha, I could email you the url as it's a public site ... or i could post it at the top of this thread but everyone might "help" find security bugs/etc :P
Toran Billups
Knowing that, it wasn't hard to find. It does look interesting, but what is that weird VB you are generating :P
Rob Prouse
wow, that just blew my mind ... you found this site already ;) I work for a state gov and sadly ... vb only shop so I'm stuck w/out all the shiny new stuff in C# 3.0+
Toran Billups
At least working for state gov, your job is probably secure through this downturn ;)
Rob Prouse
you are correct, but at times I would kill to work with really talented people who are excited about solving really hard problems. After a while the "can you make a form to save this data?" - gets boring...
Toran Billups
+2  A: 

Spending less time doing something by having something/someone else do it, and more time researching better ways to do it will generally lead to doing it in a better way.

This doesn't have to just apply to programming....

ck
+7  A: 

That's good! Of course writing a tool to do all the job for you is impossible and wrong.

But automating repeatable tasks is always good - and sometimes writing specific types of code is repeatable.

It is even encouraged in the "Pragmatic Programmer" book.

Make sure that in the source control you have checked in a code generator and not its output (unless you have to modify the code later by hand)!

phjr
+1  A: 

Code generation is fine as long as you understand what you are generating. Physicists use calculators because they understand the formulas they are automating and realize that their precious time is better spent on important tasks.

Andrew Hare
You can't really generate code without knowing what the code is doing :-)
MB
+4  A: 

Larry Wall (of Perl fame) describes the three cardinal virtues of programming as Laziness, Impatience, and Hubris.

Congratulations! You have shown good laziness, in that you have identified some work you can pass off to an automated process and done so. (Bad laziness leads to cutting corners, procrastination, and generally postponing rather than eliminating work.) If you can successfully palm off some work onto another program, you are spending less time on annoying triviality and more on accomplishing things and learning.

David Thornley
+1  A: 

Code generation is one of those invaluable DO:s that The Pragmatic Programmer advocates. I truly recommend that book. Here's a Pragmatic Programmer quick ref.

PEZ
+1  A: 

Its almost hypocritical not to code generate. Here we are automating all of these tasks that were traditionally done by hand... and yet many of us still hand crank all of our code, even if it can be easily generated.

Giovanni Galbo
+1  A: 

My only experience with code generation is the macros of Common Lisp. They are used all the time. Everything that automats repetitive tasks is beneficial; that is what programming is about.

Read the story of Mac.

Svante
+1  A: 

Imagine that each time you made a change to the tool and regenerated your code, that you made that design change by hand on all of your modules.

Since I've started generating code and gotten up to speed, I've found that I rarely get bugs in the generated code.

jrcs3
this is the biggest plus for me - less bugs + more consistent code
Toran Billups
Yea, this is one of the little things that make some developers who can do 10x what the guy in the next cube can do.
jrcs3
+1  A: 

I find that writing code gen does help me learn the nuances of good architecture. You start seeing common patterns as opposed to a narrow view of your design. That said, don't use code gen as a substitute for good object-oriented code, and don't love your code gen so much you ignore new technologies. For example, if you're in .NET and are writing code-gen for data access, you'd better have a good excuse for not using Linq to SQL or NHibernate. Similarly, Dynamic Data can help in many forms-on-data scenarios. So, my advice: spike new stuff and code gen as needed.

Daniel
+2  A: 

Your code generator (at least in principle - I haven't looked at it myself) is The Right Thing, at least as far as it goes.

The next step would be to see whether you can, instead of generating all this redundant code, create a base class whose functionality matches the generated code and then derive your application code from it. Using inheritance rather than generation will allow you to benefit from improvements without needing to re-run the generator on all your projects. Perhaps more importantly, if you customize the generated code, the customizations would be lost if you re-run the generator, but customizations in a derived class will be preserved when the base class is changed.

Dave Sherohman
+1  A: 

My 2cents on code gen is that it is also critical for use in refactoring. I have found that partial classes and a good file comparison utility (Araxis or BeyondCompare) are essential.

Keep your generated code in one file and the custom Tweaks you made for that class in another file.

This practice will allow you to make those comprehensive framework changes implemented quickly and will also help you move to a new paradigm while easily being able to save your custom logic.

CodeSmith FTW!

While build servers are great to make sure all your code compiles, it doesn't address the differences in signatures with your stored procs or the like. If you routinely run the code gen you can more easily identify when those changes occur. A unit test will tell you the SP is wrong, code gen will tell you how to make it right.

dustinson