Coding confession/brag/rfc on something i just wrote



  • code & pre tags are bullshit because they ignore tabs, so screenshot. all the calls used are just composition of framework-provided functions, no needless scruff, really the best and most concise way how i am able to do what i need it to do. please comment on whether and how large a wtf it is, whether the comments make it more or less understandable, anything, nitpick it to bits any way you want if you imagine you came across it or something like it in your codebase.
    0_1497491165790_7e151bdc-fa82-4311-87cd-6a897e4eb518-image.png

    btw there was a general small coding confessions threat that i can't find right now. my confession for there would be "compositing. i'm a compositing and nesting fetishist sometimes."



  • @sh_code
    FYI, in general, the three backticks (`) method for markdown is supported here. You can click the vertical "..." to the right of this post and choose "View raw" to see what I did here.

    Code goes here
    

    It also supports the markdown hinting for specific languages (though it often gets the right language without too much help) so you can do things like:

    SELECT izzion
    FROM dbo.Users
    WHERE UserType = 'Awesome'
    

  • Winner of the 2016 Presidential Election

    @izzion said in Coding confession/brag/rfc on something i just wrote:

    @sh_code
    FYI, in general, the three backticks (`) method for markdown is supported here. You can click the vertical "..." to the right of this post and choose "View raw" to see what I did here.

    Code goes here
    

    It also supports the markdown hinting for specific languages (though it often gets the right language without too much help) so you can do things like:

    SELECT izzion
    FROM dbo.Users
    WHERE UserType = 'Awesome'
    
    Rows returned: 0
    

    :trolleybus:


  • Winner of the 2016 Presidential Election

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    code & pre tags are bullshit because they ignore tabs

    Since @izzion didn't make it clear: the three backticks method respects tabs.

    Example
    	of tabbed
    		input
    


  • @Dreikin
    Psh, why should I actually be helpful in my response posts? What is this, Discourse? :trolleybus:


  • Winner of the 2016 Presidential Election

    @izzion said in Coding confession/brag/rfc on something i just wrote:

    What is this, Discourse?

    No,
    0_1497492424444_1qwv12.jpg


  • Winner of the 2016 Presidential Election

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    code & pre tags are bullshit because they ignore tabs, so screenshot. all the calls used are just composition of framework-provided functions, no needless scruff, really the best and most concise way how i am able to do what i need it to do. please comment on whether and how large a wtf it is, whether the comments make it more or less understandable, anything, nitpick it to bits any way you want if you imagine you came across it or something like it in your codebase.
    0_1497491165790_7e151bdc-fa82-4311-87cd-6a897e4eb518-image.png

    btw there was a general small coding confessions threat that i can't find right now. my confession for there would be "compositing. i'm a compositing and nesting fetishist sometimes."

    So, uh, why aren't you doing this in a functional language?



  • @Dreikin said in Coding confession/brag/rfc on something i just wrote:

    So, uh, why aren't you doing this in a functional language?

    C# is a functional language, if you use LINQ. This might actually benefit from LINQ's deferred execution, depending on what you do with the resulting collection.

    Why aren't you using var? Specifically-typing Collider is going to make refactoring more difficult later on. Odds are you don't care if the lookCollector contains Colliders or not, you probably just care that the thing it contains contains a .ClosestPoint() and a .transform. Also your casing is weird, but that might be framework stuff.


  • Winner of the 2016 Presidential Election

    @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    C# is a functional language, if you use LINQ. This might actually benefit from LINQ's deferred execution, depending on what you do with the resulting collection.

    Yes, but I was thinking it would look cleaner in a functional language (like F# in this case) if they always try to code this way*, even given LINQ query syntax.


    *: @sh_code said in Coding confession/brag/rfc on something i just wrote:

    btw there was a general small coding confessions threat that i can't find right now. my confession for there would be "compositing. i'm a compositing and nesting fetishist sometimes."


  • sockdevs

    Maybe this is Unity, which doesn't speak F#...


  • Dupa

    Static properties are bad. Someone is gonna hate you for that.

    But before that, I'm gonna hate on you for that.

    Dawg.


    ETA

    I'm the stupid one, I somehow missed your caveat that these are all standard framework classes.

    Move along, nothing to see here. Apart from the fact, that if you put this stuff into variables, you wouldn't have needed comments.



  • @sh_code Without knowing more about the code and just a quick glance, I'd be wondering about why you use the y-component for the Z value, and why you add .5 to it.

    Also, from a quick glance, I'm also wondering if you can't simplify that a bit. I.e., you go viewspace to worldspace, and then back. And both times there's the offset by a half-unity vector. Though that's hard to determine without knowing more about what a Collider is and how ClosestPoint() works. (Again, would need to know more about the Collider in order to be 100% sure, but it almost seems like you want to compute the distance in a certain plane.)

    Regarding the comments. I'd be happier if there was an overarching comment about your what you're going to do at the beginning (i.e., what distance are you actually computing, not how you compute it). The current comments are somewhat meh (but OK), they help in some cases ("center of viewport", since that also depends on your setup), but they also repeat the code in some cases ("point on the collider that is clostest to"). But that's IMO.



  • @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    Specifically-typing Collider is going to make refactoring more difficult later on. Odds are you don't care if the lookCollector contains Colliders or not, you probably just care that the thing it contains contains a .ClosestPoint() and a .transform. Also your casing is weird, but that might be framework stuff.

    only thing that contains .ClosestPoint is a collider which is a superclass for every "ray-castable" component, so odds are I do not care for anything else except colliders. but in general I have aversion against the var keyword, when reading a variable declaration, i like to know what type it is without having to hunt and parse for it in the value assignment.

    also, yes, the casing in unity is weird.



  • @kt_ these static properties are surprisingly useful. having the ability to go Vector3.zero or Vector3.one or Vector3.left/right/up/down instead of new Vector3(x, y, z) feels nice.

    Why Distance and similar operations are as static methods instead of normal ones i have no idea, vectorA.Distance(vectorB) seems better, less wordy, more direct, similarly to Dot, Cross, etc.



  • @cvi said in Coding confession/brag/rfc on something i just wrote:

    Without knowing more about the code and just a quick glance, I'd be wondering about why you use the y-component for the Z value, and why you add .5 to it.

    that would be a good question. the answer is "it was late at night, this snippet went through some corrections after posting (but stayed in principle the same), I'm not entirely sure anymore". i think it was supposed to be correction for y coord to account for character's height (therefore camera is higher than his 0,0 coord), because the viewportToWorld seemed to... not correctly account for local coords, but... the more i think about it now the less sure I am if it was actually the case.

    @cvi said in Coding confession/brag/rfc on something i just wrote:

    And both times there's the offset by a half-unity vector. Though that's hard to determine without knowing more about what a Collider is and how ClosestPoint() works.

    unity's screen space (on some places at least, not sure if all, yes, a bit of framework :wtf: ) is 0,0 from bottom left, the only check i need to do on it is "distance from screen center", so i do distance from 0.5, 0.5 (screen center) to the closest point on the collider...
    ...oh wow. it was really late at night. to be honest, i'm not entirely sure why and how I arrived at this exact form...

    therefore conclusion: this is a confession and that is a horrible piece of code regardless of how cool it looks :-D
    thanks for helping me realize that by forcing me to look at it again when I'm sober



  • @sh_code said in Coding confession/brag/rfc on something i just wrote:

    but in general I have aversion against the var keyword, when reading a variable declaration, i like to know what type it is without having to hunt and parse for it in the value assignment.

    1. if that matters, your program's written stupid. (Unity might be written stupid, I honestly don't know.) C#'s compiler will yell if you do anything really wrong, until then relax and write code.
    2. Your IDE knows the type that'll be assigned if you really need to know, so just mouseover. Unless you're using a crappy IDE, in which case: stop doing that.

    You asked for a code review, there you got one.


  • sockdevs

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    in general I have aversion against the var keyword, when reading a variable declaration, i like to know what type it is without having to hunt and parse for it in the value assignment

    If you're having to hunt and parse the assignment to work out the type, you've done a very bad job of naming it



  • @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    if that matters, your program's written stupid. (Unity might be written stupid, I honestly don't know.) C#'s compiler will yell if you do anything really wrong, until then relax and write code.

    that's how i write code, i imagine puzzle pieces/connector shapes, know which outputs need to go into which input in what shape, write code to transform data from one "connector shape" to the other. that's all that programming is, after all, transforming data, no? how do I transform data in a relaxed state when it's not obvious from one look what i'm transforming into?

    @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    You asked for a code review, there you got one.

    yes, thank you, now I've moved to the discussion section. Not saying you're wrong, just throwing out my aruments seeing what counterones I get.



  • @RaceProUK said in Coding confession/brag/rfc on something i just wrote:

    If you're having to hunt and parse the assignment to work out the type, you've done a very bad job of naming it

    var element;
    var elementWasFound = (myList.First(i => i == "element we're looking for") = element) != null;

    maybe.



  • @sh_code said in Coding confession/brag/rfc on something i just wrote:

    that's how i write code, i imagine puzzle pieces/connector shapes, know which outputs need to go into which input in what shape, write code to transform data from one "connector shape" to the other. that's all that programming is, after all, transforming data, no? how do I transform data in a relaxed state when it's not obvious from one look what i'm transforming into?

    You must embrace the zen of C#. Everything in your code is ethereal, your types float in the mists of vaguery. There are no concrete types until the last possible millisecond when you need them to be concrete, they solidify out of the mists and drop into the mundane world of "output". The compiler is your guide through this world, you can trust it. Those types that are not output stay in the mists until, long after they are no longer needed, they waft peaceably away.

    Your loop cares not for Collider, nor List, it cares perhaps for ICollideable and IEnumerable. Do not sabotage the compiler by trying to second-guess what types it needs, it knows more than you, embrace its wisdom. Concrete types are the mind-killer. Concrete types are the little death that bring total obliteration.



  • @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    Your loop cares not for Collider, nor List, it cares perhaps for ICollideable and IEnunmerable

    incorrect, sadly. unity no likey interfaces, mostly, mainly in the older parts of the codebase, and Colliders, being one of the oldest, don't have any. Two versions ago, the finally made the unified Collider parent class which all colliders now inherit from, before that you had all separate BoxCollider, SphereCollider, MeshCollider, CapsuleCollider, BoxCollider2D, all the other *Collider2D variants, all basically sharing the same interface without... actually sharing anything or even having any interface. so you couldn't even switch out BoxCollider for CapsuleCollider from under your code without everything coming crashing down.
    Then, the godsend of parent Collider which all the flavors of Colliders inherit from was brought upon us :-D

    (i love Unity, but it's got some crazyparts and some stupidparts, in the most unexpected places, as everything.)

    @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    You must embrace the zen of C#. Everything in your code is ethereal, your types float in the mists of vaguery. There are no concrete types until the last possible millisecond when you need them to be concrete, they solidify out of the mists and drop into the mundane world of "output". The compiler is your guide through this world, you can trust it. Those types that are not output stay in the mists until, long after they are no longer needed, they waft peaceably away.

    i kinda like this mindset and at the same time i know it would be suffering for me to adopt it, and c# never seemed to me to be of this mind, with it being the first language where i met <Type Parentheses>... very counterintuitive for me, but will try, maybe


  • sockdevs

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    unity no likey interfaces, mostly, mainly in the older parts of the codebase, and Colliders, being one of the oldest, don't have any. Two versions ago, the finally made the unified Collider parent class which all colliders now inherit from, before that you had all separate BoxCollider, SphereCollider, MeshCollider, CapsuleCollider, BoxCollider2D, all the other *Collider2D variants, all basically sharing the same interface without... actually sharing anything or even having any interface. so you couldn't even switch out BoxCollider for CapsuleCollider from under your code without everything coming crashing down.

    Wait... the designers of Unity seriously sat down and created a whole family of Collider classes, and at no point did any of them think "Now, what do all these Colliders have in common?" until is was too late?



  • @RaceProUK said in Coding confession/brag/rfc on something i just wrote:

    Wait... the designers of Unity seriously sat down and created a whole family of Collider classes, and at no point did any of them think "Now, what do all these Colliders have in common?" until is was too late?

    looking at that piece of code I wrote and how I thought how awesome it was yesterday, anyone making this mistake few times in their lives is pretty understandable to me :-D



  • @RaceProUK they also implemented implicit meaningful conversions from most of the types ToString(), so you don't have to even cast numbers when concatenating, but they neglected to implement Inspector (in-editor view) for Dictionary variables. or for properties, so any parameter you want to set to a script from inside the editor? public variables for you and nothing else! or serializable privates, which i bet i don't have to say is even worse.



  • @sh_code said in Coding confession/brag/rfc on something i just wrote:

    incorrect, sadly. unity no likey interfaces, mostly, mainly in the older parts of the codebase, and Colliders, being one of the oldest, don't have any. Two versions ago, the finally made the unified Collider parent class which all colliders now inherit from, before that you had all separate BoxCollider, SphereCollider, MeshCollider, CapsuleCollider, BoxCollider2D, all the other *Collider2D variants,

    I wouldn't expect game developers to know what the fuck they're doing when writing quality software, but this kind of goes above and beyond.

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    all basically sharing the same interface without... actually sharing anything or even having any interface.

    But the great thing about C# is: "var" still works in that case. Depending on how faithfully they got all the exact method and properly names exactly correct. It's basically duck-typing.

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    (i love Unity, but it's got some crazyparts and some stupidparts, in the most unexpected places, as everything.)

    The Zen of C# is not present in the ancient crappy version of Mono it's using, I understand. What a shame.

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    i kinda like this mindset and at the same time i know it would be suffering for me to adopt it,

    C# isn't really about writing procedural code anymore, is the thing. You tell the compiler about operations it could potentially do, you tell the compiler what outputs it needs to generate, and you let it decide the most efficient way of doing it. There's a good reason the first syntax for LINQ looked an awfully lot like SQL-- the new behavior of C# is actually very similar to SQL in many ways.

    To take your example, since you're using concrete classes throughout, like I said above you've made refactoring more difficult. (If you change Collider for MeshCollider, you'll have to rewrite that code perhaps unnecessarily.) That's a minor benefit.

    But more than that, if you'd used LINQ (and yes, yes, I understand Unity is crap, bear with me), you'd have similar code but none of it would execute until the results were needed. Meaning: if you have 50,000 Collider objects but your player dies after only needing 4 of them, the other 49,996 operations will never happen. It's not just more maintainable code, but you've at the same introduced a major performance optimization.

    It's kind of beautiful when you wrap your head around it.

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    looking at that piece of code I wrote and how I thought how awesome it was yesterday, anyone making this mistake few times in their lives is pretty understandable to me

    There's a difference between "making a mistake" and "shipping it as a finished product to thousands of people for several major versions".



  • @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    C# isn't really about writing procedural code anymore, is the thing. You tell the compiler about operations it could potentially do, you tell the compiler what outputs it needs to generate, and you let it decide the most efficient way of doing it.

    i'm currently realizing this while teaching 14year old to code in it.

    "What's the difference between array and list?"
    "Technically, theoretically, regarding implementation it should be something like this. In practice, almost nobody really has any idea anymore. From usability point of view, list is slightly bigger in memory and slower to work with, but can be resized dynamically."
    "Eh... okay"

    or

    "Why did the destructor ran about 10 frames AFTER i destroyed the object?"
    "...because garbage collector decided that's the right time to actually destroy it. if you really want to destroy it IMMEDIATELY, as you call the function, there's another function for that, DestroyImmediate"
    "Eh... okay"

    ...yes, i know, not the most ideal, teaching someone beginnings of a language along with a whole engine that uses and misuses it in various ways, but still...
    ...things were easier, from this point of view, in the times of QBasic.



  • I would break this into individual statements that you can step through and reason about.

    That's all I have.



  • @sh_code Dude, you shouldn't answer either of those questions. You should be suggesting IEnumerables and absolutely never mention destructors ever. The former rarely means anything, and the latter is almost never correct, to the point where people who helped design the language warn you to just not think about them.


  • mod

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    "What's the difference between array and list?"
    "Technically, theoretically, regarding implementation it should be something like this. In practice, almost nobody really has any idea anymore. From usability point of view, list is slightly bigger in memory and slower to work with, but can be resized dynamically."
    "Eh... okay"

    You sound like a really shitty teacher, lol. (I say this as a moderately shitty teacher at best)


  • Impossible Mission - B

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    "What's the difference between array and list?"
    "Technically, theoretically, regarding implementation it should be something like this. In practice, almost nobody really has any idea anymore. From usability point of view, list is slightly bigger in memory and slower to work with, but can be resized dynamically."

    ...you were saying?



  • @masonwheeler And talking about speed differences, especially to someone new to the stuff, is kind of silly. Especially since you basically only use IList<> derivatives if you plan to do Adds.



  • @Yamikuronue said in Coding confession/brag/rfc on something i just wrote:

    You sound like a really shitty teacher, lol. (I say this as a moderately shitty teacher at best)

    I've been giving a C++ course at work so that the rest of the team can help with/have a job after the transition, and knowing when to go deeply into detail and when not to is hard. Especially in C++, with all its warts and backwards compatibility, I tend to tell them "this is true 90% of the time, there are ways to go around it but you don't need to know it now". You don't want to lie or say something that isn't accurate, but you also don't want to bog things down with needless detail.


  • Winner of the 2016 Presidential Election

    @Kian said in Coding confession/brag/rfc on something i just wrote:

    You don't want to lie or say something that isn't accurate

    Sometimes you do. Maybe with the caveat that it's not necessarily 100% true, but often just ignore the warts entirely until you revisit the topic and get to say "remember what I told you before? That's not entirely true"



  • @blakeyrat said in Coding confession/brag/rfc on something i just wrote:

    It's kind of beautiful when you wrap your head around it.

    Careful or someone will show up and start talking about monads.


  • Discourse touched me in a no-no place

    @boomzilla said in Coding confession/brag/rfc on something i just wrote:

    Careful or someone will show up and start talking about monads.

    Possible Laidlaw's Rule inversion?

    edwardkmett: Most monad tutorials are written by people who barely understand monads, if at all, but unfortunately nothing can stop someone from writing a monad tutorial. We've tried, there was blood everywhere.

    And then the murders began.



  • @Yamikuronue said in Coding confession/brag/rfc on something i just wrote:

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    "What's the difference between array and list?"
    "Technically, theoretically, regarding implementation it should be something like this. In practice, almost nobody really has any idea anymore. From usability point of view, list is slightly bigger in memory and slower to work with, but can be resized dynamically."
    "Eh... okay"

    You sound like a really shitty teacher, lol. (I say this as a moderately shitty teacher at best)

    kind of, possibly, maybe, from the point of view people are used to take with teachers?
    i'm shitty in the factography part, but i don't consider teaching (as in, relaying with personalized explanation) these things to be that important.

    first thing I told the boy (and before that his father) was something like: "I'm not going to teach you how to program, because I can't, because nobody can, and if they try they will make you into a codemonkey. But I am going to teach you how to think in order for that process to be compatible with computers and programming, and for it to enable you to solve and learn stuff from programming any time, on your own, for the rest of your life, because that's the only way how to be a programmer. Not necessarily a good one, but a programmer at all. So when we get to the point where you won't need me to learn and find out new stuff about programming, that's the point when our lessons had succeeded."

    So basically that I'm not going to teach him how to program, but I'm going to teach him how to think so he can learn (and keep learning) how to program by himself.

    so i'm not teaching him facts or ready-made already known solutions, i'm even purposely ignoring most of "industry-standard alogithms", at first, and I force him to THINK about the stuff and come up with his own solution, or even multiple ones based on "what if we considered this other feature of the problem as the most important one?", which I then take apart and force him to THINK about how to solve the weaknesses I found in it, and such. I purposefully stay silent when he does stupid mistakes, just so I could spend 5-15 minutes with him reading through the code, looking for the mistake, because I know that the pain and irritation of that effort is what makes the subsequently learned/realized thing stick, instead of giving him answers I usually give questions and force him not to avoid/be afraid of answering them, and such...

    The "factual" answers I give, then, are designed to do several things: explain that "it's complicated and in most cases you don't need to care, but roughly this area of cases is the one when you should care so you should go and learn more about it when you get into that situation", explain that "everything is complicated if it's important enough in the context", and "most of these complexities you can, and even HAVE TO, if you want to stay sane, ignore most of the time".

    ...basically, I'm self-taught, and as such I admit I don't have all the factual info and can't be bothered looking for it if I don't explicitly need it at that moment. but also, as such, I have experienced the value of the parts of learning process which teaching often (incorrectly, IMO) removes.


  • Discourse touched me in a no-no place

    @sh_code said in Coding confession/brag/rfc on something i just wrote:

    I have experienced the value of the parts of learning process which teaching often (incorrectly, IMO) removes.

    That'll work as long as the students stay motivated enough and have enough intelligence in the first place. However, the other style of learning can have value too, particularly when it is focusing on providing a higher-level model and explaining how lots of other things fit into it (a lot of algorithms teaching is like this because the deeper model relates to how to work out the cost of an algorithm). Some things work better one way, and some the other.

    Theoretically a student could luck into those higher-level models on their own, but they're usually created by very smart people indeed so it isn't shameful to say “this is a difficult problem that has had some deep analysis applied by others over many years, and they reckon that this is a good way to think about it; here, let me show you an example…”



  • @izzion said in Coding confession/brag/rfc on something i just wrote:

    SELECT izzion
    FROM dbo.Users
    WHERE UserType = 'Awesome'
    
    UPDATE dbo.Users SET izzion = NULL WHERE Username = 'Zecc'; 
    

  • Winner of the 2016 Presidential Election

    @Zecc said in Coding confession/brag/rfc on something i just wrote:

    @izzion said in Coding confession/brag/rfc on something i just wrote:

    SELECT izzion
    FROM dbo.Users
    WHERE UserType = 'Awesome'
    
    UPDATE dbo.Users SET boomzilla = true; 
    

    FTFY.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.