Any ideas how to abstract that?

  • 16
  • 1
    What IDE is this? I like the editor color scheme. Can you share that :D?
  • 8
    Yeah. Rewrite it all in a language that is not PHP.
  • 5
    Even if it's not user input data, please never write your sql statements using string interpolation
  • 2
    @Kyu96 it’s dracula theme for intellij (but they have it for all popular ide’s) + firacode font with ligatures enabled, 14pt size with 1.2 line spacing. I also use same setup for python, though its slightly different (more cyan, and less pink)
  • 0
    @AlexDeLarge I don’t see how changing the language affects OOP principles. Let’s say it’s written in Java, so I am stuck with the same shit.
  • 0
    @julkali it’s ok, this is just abstraction for reusable expression. And this is actually DQL (doctrine dsl for sql), so I always get error if interpolated values are invalid.
  • 2
    Could extract the logic into objects that expose some methods like

    supports($prediction, $actual): bool
    result(): int

    Then in a loop return the first supported result()
  • 2
    "switch(true)" yeah, fuck this
  • 4
    @gintko I didn't say anything about OOP. PHP is just a shit language, in my opinion.
  • 0
  • 0
    I don't understand. In the switch you are matching a case:

    $predictedHomeGoals == $actualHomeGoals && $predictedAwayGoals == $actualAwayGoals

    But then in same case to are trying to look for < and > return 6

    Those will never parse unless the goals are == so they cannot be < or > after the case it will skip and go to next case not equal.

    So for case one it will only ever return 3 and that is when they are equal.

    The if statements should never be true or false.
  • 1
    @Bitwise first case is checking if prediction and actual result is the same:
    actualHome = predictedHome.

    Then ifs below are checking which team was the winner:
    predictedHome > predictedAway
    Same as:
    actualHome > actualAway

    @AlexDeLarge something I can agree with, but language after all is just a tool

    @jallman112 https://draculatheme.com
  • 0

    Oh fuck I read those wrong
  • 1

    Way too small of text

    I read those as the same
  • 0

    So the only thing the switch statement is being used for is tidiness and readability?

    I don't think there is a need to abstract it out.
  • 1
    @Bitwise oh, come on:

    aH = 3 | aA = 5
    pH = 3 | pA = 5

    If aH == pH && aA == pA: # true
    if aH > aA: 6 # false
    elseif aH < aA: 6 # true
    else: 3

    PS: u figured it out already I see, well its devrant ing compression not much I can do.
  • 4
    @gintko True, it's both a matter of taste and pragmatism. I'm also not seriously trying to keep you from working with PHP. As long as you like it or the advantages of using it overweigh for you, I'd be the last one to piss in your cereal for that. I just don't like PHP myself, that's all.
  • 0
    Yes, switch was for readability and also for easier reasoning compared to SQL.

    Abstraction is needed, though, to separate SQL and PHP implementations and reduce duplication for easier future maintenance
  • 1

    I just won't try to read code on my phone anymore.

    I will go to a computer. getting way too old to read tiny print anymore.
  • 1
    @gintko Cool! I'm just used to people getting autocorrected on that.

    I guess everyone can use regular TIL moments 😀
  • 0

    Well then I've completely not understood, I thought you just translated the SQL into PHP.

    If you are already using SQL to do the logic there is no need for the php.

    I guess your doing the logic in php now? So all you would need to do is grab the objects with a select statement to populate your parameters. Or am I still missing the point?
  • 2
    What exactly do you mean by abstracting a SQL and a PHP script?

    One abstraction for a script is a function. Just wrap it in a function and it's abstracted to one function from multiple lines of code.

    That wasn't probably what you meant.
  • 1

    This is what I thought too, which is why I am confused. I am not sure what is going on, or what he means by abstracting.
  • 0
    @joas @Bitwise what I meant, was refactoring code so it can comply with solid and dry, which in this case can be only approached by abstracting the code or in other words, splitting code into higher level class(-es) which then can be reused to implement actual logic.

    Anyway, I kind of solved it in the not the best way by defining template methods for specific instructions, that really does nothing besides documenting the code. I can post link here on tuesday if someone is interested.

    I figured, that probably the best way to achieve reusability here, is to implement builder pattern which abstracts both SQL and PHP DSLs, sth like following:

    Anyway, thats overengineering for my case.
Your Job Suck?
Get a Better Job
Add Comment