re: Think Like a Programmer?? VIEW POST


That's exactly it. When people say they want to break the problem down into smaller pieces, that's not programmer thinking, that's architect's role.

Let's say you get a task that you need to write a function that returns a list of database records from start# to end# (e.g. one page of results).

This might be probably a oneliner (sorry, I think in Linq). How do you break it into smaller pieces?

It's more about being able to analyze the problem in your head and then write it in one take.

First ask yourselfg a question: Do I have all important inputs to complete the task? No? Go grab them!

The things important here are:

  • Initialize your variables
  • Sanitize your inputs (function parameters are integers > 0; end > start)
  • Handle all possible conditions (returns 0 results [do not return null but empty array/list], handle exceptions, return max. configured list of results for one page)
  • Handle your dependencies (is DB object provided by DI?)
  • Document your code (function result, parameters, throwning exceptions)

Another things you should do:

  • Test your function
  • Make sure the function integrates well (place and name is right, parameters are of correct type, has no extra dependencies if possible)
  • Make sure the function is optimal (if the function is executed tens of times per second it must be fast)
code of conduct - report abuse