• Post Reply Bookmark Topic Watch Topic
  • New Topic

Feedback on making the code readable and optimizing the code further  RSS feed

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the below piece of code which splits the eventparams for acat, an, av, ani. Here each of the inputs splitted by pipeseparator is a set ie. the first param of acat, ani, an, av forms a set.
Please help me with tips to optimize this code further.

* Logic goes as follows: ex: eventparamsMap({acat : mo|cl|video|video}, {an : img|link|play|rewind}, {av: imgname| linkclass| playtime| rewindid}, {ani: 0|0|0|0})
* Create a list with a string array. Populate the array with an, av, acat, ani params splitted by pipeseparator. So, now the list would have 4 string arrays.
* For instance, it would be list.get(0) has mo, cl, video, video; list.get(1) has img, link, play, rewind; list.get(2) has imgname,linkclass,playtime,rewindid; list.get(3) has 0,0,0,0
* now we check the total number of params after split ie. len = 20 and we initialize k= 0, inc = 0, length = 0 and we have tempBuffer of size 4.
* now we start the loop for j=0 to j < len i.e. we run the loop 20 times, in the loop, we start with
* 1. checking the length of a set, call it setlength:
* i) if it is greater than maxsize, ignore and continue as we cannot use this set
* ii) if the urllength + setlength < maxsize, then append the params to tempbuffer, reset tempLength to 0, if we have reached the end of loop, then formclickurl and break,
* iii) if the urllength + setlength = maxsize, then append the params to tempbuffer and form click urls and reset tempbuffer, urllength =0=templength
* iv) if the urllength + setlength > maxsize, then form click urls without the current set and append params to tempBuffer and set urllength= templength, if we have reached end of loop, form clickurl with tempBuffer
*/
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that springs to mind immediately: why are you using a while loop with an iterator (line 16 and 98)? These situations are perfect for using the for-each loop.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even before you get to D. Ogranos’ point: why have you got such long lines? If you have more than about 120 characters in a line, it becomes very difficult to read.
I shall take the initial comment out of the code tags because it probably shouldn’t be part of the code.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, use spaces for indentation, not tabs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!