Help to decipher this code!

Hi all,

Great to see so many passionate developers on here. I am new and would like some help. I am an operations manager so while I appreciate the great work you all do, I am illiterate in it.

Please could you give me an idea with // of what each of these statements mean? I am honestly confused. Thanks!

    pq = pq.Column(`
    ((ROUND((100 - (SELECT distance) + (LEAST(p.c_review_count, ?) * 2.5 +
     IF(p.professional_photo_graphic_id, 0, -5))),6) * 10000000000) + (c_review_count * 1000) +
     (IF(p.c_first_impression_count > 0, (((p.c_review_count * p.c_review_average_score_overall)+
     ((p.c_first_impression_count  /  2 )  *  p.c_first_impression_average_score_overall)) /
     ((p.c_review_count + p.c_first_impression_count) - (p.c_first_impression_count / 2))),
     p.c_review_average_score_overall) * 10)) as best_match
     `, params.ReviewCount)
    pq = pq.OrderBy("best_match desc, distance")
    return pq

It is a calculations that produces a score at the end. Just would like to know how it arrives at that.

Thanks,
Eric

Its hard to tell without knowing where pq comes from.

But at least I can guess, that the huge raw-string is processed alsewhere, and its result will be stored back into pq. After that pq will be ordered by the given keys and returned.

Thats it mostly from a go point of view. I’d guess the real works happen in that external system, which you did not gave any information about. Also I am not sure if we can help here to actually help to dive into the code in the string even if we knew what external system it is, since its obviously not go.

Thanks NobbZ, I will clarify with the team what language it is.

I think my query is around what different things mean seperated by +. For example, what does this mean → ((ROUND((100 - (SELECT distance) + (LEAST(p.c_review_count, ?) * 2.5

And what does it mean → IF(p.professional_photo_graphic_id, 0, -5))),6) * 10000000000)

and what doesthis mean → (c_review_count * 1000) +
(IF(p.c_first_impression_count > 0, (((p.c_review_count * p.c_review_average_score_overall)+
((p.c_first_impression_count / 2 ) * p.c_first_impression_average_score_overall)) /
((p.c_review_count + p.c_first_impression_count) - (p.c_first_impression_count / 2))),
p.c_review_average_score_overall) * 10))

Those snippets you are asking about right now, are those handled by the “external” system.

My first thought was that it could be Excel or something similar, but I never heard of one that allows to have qualified names.

So if you really need an explanation about how that piece of code works, ask the person who has written it. If (s)he can’t explain, get him/her fired… OK, thats a bit harsh :wink: Just take him/her to the next forum that has this language as topic and ask again there.

But be aware of the fact, that those guys in the other forum will need some additional context as well. There are a lots of names used as values (I suppose), and they might ask where those values come from.

pq is the pure Go postgresql driver. https://godoc.org/github.com/lib/pq

Everything within the ` quotes is a SQL query, and is hard to explain without knowing what all these variables are.

If this is something from work, you probably should not be posting it here but should ask your coworkers.

2 Likes

Its probably not in this case.

There is a method called on pq as receiver and then reassigned to pq, beware the =! Also in the documentation you linked, there is nothing that has a method Column() nor can I find it in sql.

Also even if it is SQL, then it is not a valid statement on its own. There is missing quite a lot then.

But you are right, and I am sorry not having considered this before, if this is really a piece of code from a project of your working place (or even bought code that you have source access to), it might violate NDAs to show it in public.

Given that the SQL-like stuff we see talks about a p I would guess that pq is the “p query” and this is an ORM or something that builds SQL using fragments like the column definition and the OrderBy etc… But yeah, none of that changes the big picture.

Either the expression makes sense as is, with additions, multiplications and ifs, or you better ask someone who knows the entire system.

It is not a Go question.

1 Like

You are correct. I got distracted mid-post and though I had done the search and found the right package.

What @calmh is saying sounds more plausible. :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.