1

I have this doubt many years. I am wring some code to achieve some functionality. For example I am writing 20 lines of code to achieve the functionality and my co worker writing the code for the same functionality with just 5 lines. Since he used some looping statement to achieve that, but that code will execute around 30 to 50 times.

So which is best way of coding? As per my knowledge I always try to reduce coding length as much I can.

gnat
  • 21,213
  • 29
  • 113
  • 291
  • 6
    I don't think this is answerable unless we see the actual code. – yannis Nov 07 '12 at 10:27
  • 1
    "more effective" in what context? performance, readability? How important is performance in your context? It's a difference if you rite a website where minor differences don't matter that much or have code that processes large amounts of data in real time. – thorsten müller Nov 07 '12 at 10:28
  • performance more important for me. – Ayyappan Anbalagan Nov 07 '12 at 10:31
  • @YannisRizos I don't have any actual code but i am just need generic answer. – Ayyappan Anbalagan Nov 07 '12 at 10:34
  • 3
    There is no generic answer, this isn't how programming works, there is no silver bullet. – yannis Nov 07 '12 at 10:34
  • You need your requirements clear. That's the start. If you run that code only once a day it might not be relevant that it performs not great (you only know after benchmarking). If it happens for every web request it might be bad. – Luc Franken Nov 07 '12 at 10:35
  • You can reduce number of lines by deleting ends of line. That is definitely not usefull. In less parodic style, reducing line count without actualy reducing complexity does not help, and it can hurt by forcing reader to comprehend too much at once. – user470365 Nov 07 '12 at 10:48
  • If performance is more important, what difference does it make? – JeffO Nov 07 '12 at 12:01
  • 1
    @Ayyappan.Anbalagan if you are looking for a generic answer then: "It depends..." – Songo Nov 07 '12 at 12:28

5 Answers5

10

This answer must be very general but:

The first priority must be clarity. Usually concise code is clearer than long winded code, but, often not. So favour five or six simple easy to understand lines of code over one brilliant but obscure hard to read one-liner.

The second priority may be speed of execution, so you may want to re-factor your code if (and only if!) your code is running too slowly for the given requirements. (The brilliant but obscure one-liner may be much faster; on the other hand hard coding twenty lines with subscripts 0 to 19 can be faster than a for (x=1,x < 20 ;x++) loop ).

The other rule of thumb is if you cannot see the whole function in one screenfull (say 60 lines!) then consider breaking it up. Being able to see all the code in one eyeful really helps comprehension.

James Anderson
  • 18,147
  • 1
  • 43
  • 72
  • Short functions are a nice-to-have, but it doesn't necessarily help all that much to decompose a complicated piece of functionality into a large number of short functions that join together in a complicated pattern. That's just turning code spaghetti into code ravioli; you're still stuck with the pasta… – Donal Fellows Nov 07 '12 at 14:46
  • @Donal Fellows -- I did use the weasel word "consider". :-) – James Anderson Nov 08 '12 at 01:58