Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 380201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:57:58+00:00 2026-05-12T14:57:58+00:00

Is it OK to generate code like this on the fly? Or is this

  • 0

Is it OK to generate code like this on the fly? Or is this a major code smell? How can this be made better?

I’m new to web but I’m stumbling across this all the time and I don’t really understand why.

// Create a js function that applies foo to each group of controls
foreach (KeyValuePair<string, Dictionary<Control, string>> pair in maps)
{
    js.Append(pair.Key);
    js.Append("=function(grid){Prm.remove_endRequest(");
    js.Append(pair.Key);
    js.Append(");if(grid && grid._element)"); //   ... blah blah blah

}
page.ClientScript.RegisterClientScriptBlock(page.GetType(), key + "Ajax", 
    js.ToString(), true);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:57:58+00:00Added an answer on May 12, 2026 at 2:57 pm

    I don’t see it as a smell until you start doing it all over the place.

    Consider these changes, however, that will help out in the future:

    1. Write data-driven JS functions, and only dynamically generate the data that they need. This way, all your JS can be tucked away in a fast static file, and your server only sends the data. This is a better design change than (2) and (3) – and it really isn’t that hard. Just think of the data that your current code generator needs, serve that data instead of the JS code, then wrap your JS code in “factory functions” that accept that data as input.

    2. Use templates for the JS code just as you use templates for HTML. This way you don’t have to munge around in C# flow control / data control code when you really just want to change some variable names. I would suggest naming template files with the name of the view that it assists. If you have Home.aspx then perhaps you will have JS code templates Home_DoCrazyGridThing.js, Home_DoOtherCrazyThing.js. You can write a simple template engine or use one of many existing such.

    3. Create a thin layer over generating code so that it’s obvious what you’re doing to the maintainer. That is, have a JSCodeGenerator class with varying levels of intelligence (understands the language OR just allows you to dump string in it OR interfaces with the emplate engine from (2)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 339k
  • Answers 339k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer At its core, real-time computing is about predictability -- the… May 14, 2026 at 4:33 am
  • Editorial Team
    Editorial Team added an answer try adding your own fixed size bitmapimage to the map… May 14, 2026 at 4:33 am
  • Editorial Team
    Editorial Team added an answer # Ensure we deal with `datetime`s. date = datetime.datetime.fromordinal(date.toordinal()) (That's… May 14, 2026 at 4:33 am

Related Questions

This thing is driving me crazy, and the error is quite meaningless to me:
I have a data warehouse containing typical star schemas, and a whole bunch of
I have a simple form that has a list (dropdown list generated from a
The following code will generate a link to the page I want to get

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.