1

I recently moved into web development using ASP.NET MVC. The language I use is C#. Having considerable experience in C makes me look for optimized coding standards (memory, efficient data structures and speed of the loops to name a few).

But my peers who have years of experience in this domain do not seem to take into account all these. So my question is, does the code need to be optimized when working with such languages as the servers we will be deploying these applications on has huge memory and processing speeds? Or is ignoring them a bad practice?

gvk
  • 335

2 Answers2

6

The culture of programming has changed considerably since the times that C was the language of choice and the hardware was so wimpy that C was actually a necessity.

Luckily, we do not have to worry about optimization so much nowadays. The general rule is to never optimize unless you have a good reason to do so. And in order to have a good reason, you have to:

  1. Have a pre-established performance requirement for your product, something like "server response time must be less than 200 milliseconds". (Vague requirements like "as fast as possible" are generally frowned upon.)

  2. Measure the actual response time of your server and actually witness that it is failing to meet the requirement.

  3. Exhaust all options of meeting the requirement by reconfiguring your system to make it work more optimally. (You would be surprised. Some people don't know the difference between running a web server in debug mode and in production mode.)

  4. Exhaust all options of meeting the requirement by buying better hardware and/or more hardware. Hardware nowadays generally costs far less than developers' salaries.

  5. Throw the profiler on your system and determine that the bottleneck is in fact in code that you are responsible for and have the power to change.

  6. Exhaust all options for algorithmic optimizations. (Introduction of a caching layer somewhere; restructuring code so that something happens asynchronously rather than synchronously; etc.)

Then, and only then, is it advisable to try your luck by tweaking code to make it work more optimally. And as you might imagine, we hardly ever reach this stage.

Mike Nakis
  • 32,803
3

In most business application development the focus is on delivering functionality and writing code as cleanly and maintainable as possible in order to avoid bugs and make it easier to react to business changes and to deliver new functionality in the future. Performance optimizations usually have a cost in development time and in more complex code, which is then harder to modify later. Therefore optimizations can be counterproductive.

The usual rule of thumb is to only optimize if there is an actual observable performance problem, rather than optimize "by default" as is more common in systems development.

JacquesB
  • 61,955
  • 21
  • 135
  • 189