Jump to content

Post A Screenshot!


gm112
 Share

Recommended Posts

Guest Melphina Micaela

I have a legitimate question!! What is the point of declaring variables as constants inside methods when -- if the point of making variables constant is to make them immutable -- you can just as well declare them globally to avoid reassignment? Because here (correct me if I'm wrong, I haven't looked at anything beyond the screenshot) every time you call the method, it's just going to reassign values to what should be constant variables??

Link to comment
Share on other sites

  • Replies 54
  • Created
  • Last Reply

Top Posters In This Topic

2 hours ago, Melphina Micaela said:

I have a legitimate question!! What is the point of declaring variables as constants inside methods when -- if the point of making variables constant is to make them immutable -- you can just as well declare them globally to avoid reassignment? Because here (correct me if I'm wrong, I haven't looked at anything beyond the screenshot) every time you call the method, it's just going to reassign values to what should be constant variables??

And I have an illegitimate answer! :D

It provides a slight performance advantage in the context of JS interpretors to indicate when you're merely retrieving a value  You're establishing a read-only contract so when the compiler optimizes your code, a lot of what may seem unnecessary or redundant gets tossed in favor of inlining everything(meaning those three consts you see turn into a few operations that moves data between registers. I could've just avoided the need for two of the three consts. But it was easier to interface with something that returns a partially parsed format than expecting the client of the implementation to provide its own. So in truth, the code comes out to be more internally verbose and friendlier on the outside. 

As far as defining those outside of the scope of the onMessage() call, no they cannot be pre-defined in the global context. This is an event callback so the data it's interacting with comes from an outside source. The emitters(save for the default one) are defined in the global scope(unfortunately due to how Node.JS modules work, there isn't a true "global context" but rather a local-global context specific to the module.. and I am referring to the one within index.js in this case), however(going back to the point I made earlier about removing two of the three consts). In JS when you assign a constant/variable to another object, you are merely receiving a reference of that object and not doing a deep copy. 

Plus I'm practicing functional paradigms(I failed here by the use of "this" and OOP concepts, but w/e that was rather intentional for the sake of keeping the syntax familiar for my coworkers whom wrote most of the original code) :) 

 

EDIT: Also failed to elaborate more on the performance benefit bit. I was referring to const vs var/let. Also if you look at the logic, its essentially an if statement that no-ops if there isn't an emitter. Seems redundant if there's a "default emitter" and it could just simply call the emitter either way. But at the time I wrote that method, there was no default handle. Though now I suppose it's left there.. but eh I still think it's okay. There comes a point when you can over-optimize and over-think your code. 

But with all of that said, you could just as easily do this instead:

    onMessage(message) {
        if (message.author.bot || !message.content.startsWith(command_delimiter)) return
        
        const cmd_args = parseMessageContent(message.content)
         (((!cmd_args[cmd_args.length - 1] || !this.message_handlers[cmd_args[cmd_args.length - 1]]) && this.message_handlers.default)
            ? this.message_handlers.default
            : this.message_handlers[cmd_args[cmd_args.length - 1]]
         )
        	.emit(message, cmd_args.splice(0, cmd_args.length -1))
        	.catch(error => console.error(error))
    }

 

Though as you can see, things are getting a little too crazy. There isn't any advantage either to this way versus above besides not having to write out three different consts. But those consts are what make that block of code a bit more readable. Plus with the references to having to call .length - 1, there's more temp vars for the Garbage Collector to clean up. Which leads me to believe that this way would actually perform worse albeit only slightly by a few clock cycles due to more memory accesses. And if you were to convert this to traditional syntax, it would be a little bit longer and wordier. 

Oh, and I wrote this short and dirty example to demonstrate a little something regarding JavaScript's memory model. Note that despite changing the "copy" of the object, changes appear to affect the original object. And vice-versa.

https://jsfiddle.net/e5upcama/

 

Sorry if this response is a bit long and convoluted.. >_< But, if there's more you'd like to ask about or know then feel free to ask!

Link to comment
Share on other sites

Guest Melphina Micaela

Oh, okay! I see what went on now. Even so, I like that your reply was long because it was also really thorough and so easy to understand! The little example helped too, lol.

Edited by Melphina Micaela
Link to comment
Share on other sites

  • 8 months later...
Guest Melphina Micaela

I'm working on a sort of pseudo(?) chess piece feature extraction program. As per usual, it's due tomorrow. ; v ; It's mostly a lot of GUI, honestly...

Capture.JPG

Link to comment
Share on other sites

Guest Melphina Micaela

It was due at midnight tonight but I got sick and woke up late and long story short I didn't get to turn it in on time, but here it is anyway. Might as well show it off somewhere, I guess. (I'm actually really bummed I couldn't turn it in on time, since the prof doesn't accept late work at all... : \)

chesspiecerec.JPG

Edited by Melphina Micaela
Link to comment
Share on other sites

Guest Melphina Micaela

Thanks, guys. ; v ;

Working on a photo share program atm. Everything works though the code is a biiiit messy. Rn I'm just trying to implement security measures, but here's the program running after some parameters have been input after logging in.

cs.JPG

Link to comment
Share on other sites

I see Java there. What UI toolkit are you using because that definitely doesn't look like Java's Swing or unless it's heavily styled. ?

On 5/5/2018 at 2:49 AM, Fragment said:

*walks in* Whoa everything here looks complicated *walks out*

/me pulls Fragment back in here

Hey, you can always ask questions an we can help you out! Learn somethin new. ?

Link to comment
Share on other sites

Guest Melphina Micaela
22 hours ago, gm112 said:

I see Java there. What UI toolkit are you using because that definitely doesn't look like Java's Swing or unless it's heavily styled. ?

/me pulls Fragment back in here

Hey, you can always ask questions an we can help you out! Learn somethin new. ?

JavaFX! I've never delved too deep into Swing, but I hear it can get messy in there.

On 5/5/2018 at 1:49 AM, Fragment said:

*walks in* Whoa everything here looks complicated *walks out*

He's right! Feel free to ask anything as I could probably learn from your questions as well.

Link to comment
Share on other sites

On 5/8/2018 at 2:24 AM, Melphina Micaela said:

JavaFX! I've never delved too deep into Swing, but I hear it can get messy in there.

Ah, right. Yeah, I haven't done traditional Java development in awhile besides workin with Spring MVC. But I do recall Swing being a butthole, so that's good they've replaced it with a better API.

18 hours ago, Fragment said:

I don't really know anything about coding like you guys. 

That's fair. You doin anything creative on the side? ? On that topic.. I've been working on something and have some concept art to share. My team produced the following art...

As an added bonus, here's a WIP song Renards_Theme_Sample.wav

17390773_401969593493418_7884607982345712122_o.jpg17457536_402270273463350_3879106369933373033_n.jpg

Link to comment
Share on other sites

Guest Melphina Micaela
23 hours ago, Fragment said:

I don't really know anything about coding like you guys. 

Same tho.

@gm112 What a cute art-style!! The second picture's character's hair reminds me of Link if he had Ghibli hair.
I only gave the music file a quick listen but it's really good!! Best yet, I'd say.

Link to comment
Share on other sites

On 5/9/2018 at 12:42 PM, Melphina Micaela said:

Same tho.

@gm112 What a cute art-style!! The second picture's character's hair reminds me of Link if he had Ghibli hair.
I only gave the music file a quick listen but it's really good!! Best yet, I'd say.

Thank you. ? That's a very humbling comparison, I'll make sure to let them know! That'll make their day. And the music also wasn't my work, but I appreciate the time given to listen to it so thank you! Lotta thank yous in this post sorry :X

 

 

On 5/11/2018 at 2:30 PM, Fragment said:

Not as creative as i used to be. Definitely had a lot more time to give this place when i was younger. Now I have to be an adult.

Yeah, seems like with age, more things eat up your free time... derp

Link to comment
Share on other sites

  • 1 month later...
Guest Melphina Micaela
On 5/14/2018 at 9:08 AM, gm112 said:

Thank you. ? That's a very humbling comparison, I'll make sure to let them know! That'll make their day. And the music also wasn't my work, but I appreciate the time given to listen to it so thank you! Lotta thank yous in this post sorry :X

Definitely!! They deserve the praise! : > No worries, dude. Always inspiring/great to take a look at others' creative works!

Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...