« first day (1522 days earlier)      last day (3419 days later) » 

6:00 PM
user image
4
^ head pooping angel ?
 
dyslexia
 
hmm, lemme see
!!mdn try catch
 
@Retsam so try...catch...finally. That won't come in the 'abuse' category.
 
@AwalGarg No; I'd still call that abuse.
 
6:03 PM
@Retsam Explain
 
As Merdith said: try catch is for exceptions. What you're doing really isn't an exception. It's a perfectly normal and expected state.
 
No, it's an exception
 
@Retsam what?
 
You have an object which doesn't have a property; that doesn't seem like an "exceptional case" to me.
 
@Retsam Yeah it's an exception
The problem is that it's a hacky way to solve the problem
 
6:05 PM
It's not a hacky way, it's one of two ways
Both ways make sense
 
SEE COPY IS WITH ME? FUCK OFF YOU GUYS! I WIN!
:p
@copy which way is better, in your opinion? ^_^
 
cases where try-catch blocks in ECMAscript are the "best" way are veeerrryyy rarely
this is certainly not one of em
 
@AwalGarg I would usually not use try-catch for these cases
 
@copy Ehh; I get that it's a preference thing, and not explicitly "wrong"; but it just feels like a misuse of tools.
 
It's a bit broken in JavaScript
 
6:07 PM
try-catch is really underutilized in javascript
^ that's probably why
 
Exceptions are fun. As with most things, they were in C++ as a necessity (and then got weird), Java as a formality (and then got abused), and are in Javascript but you can live without them.
 
Is the object expected to have that property under normal conditions? if so, then it's an exception. if it's optional then you should check properly
 
try-catch can be used more, if more people get to know about it.
 
it makes sense if you really don't know the environment or have no way to detect the environment, like in the old IE days with XMLHttpRequest objects / ActiveX crap
 
With the exception of Java, people seem to agree that an exception means something is pretty screwed up, and we can't deal with it well.
 
6:08 PM
but if you can solve or detect issues by pure ECMAscript yourself.. you really should always go for it
 
try catch is also superb for monkey patching old code, which you don't wish to change much, since you can't understand it much :p
 
catching an exception is usually an expensive operation that should be avoided
 
There's a lot of stuff that can be done with try-catch, but really shouldn't. e.g.
 
@ssube Python disagrees
 
Whoa, I've spent so much money this month and I still have $300 left to save <3 Go me!
 
6:09 PM
function fooWithCleanup() {
    try {
        if(foo) {
            throw "foo";
        } else {
            throw "bar";
        }

    } catch (e) {
        //do cleanup here
        return e;
    }
}
 
It bothers me that string.indexOf returns -1 if the string isn't found
It should throw an exception
 
~string.indexOf() :-P
 
black-desert.com HOLY FUCKING SHIT @SomeGuy @Loktar
 
@copy how so?
 
@Meredith tbh, it is more convenient right now :P
 
@jAndy aha! nice :D
 
Yeah it's way more convenient but it's not semantic
 
@Klors I thought throwing was the expensive part, since that's where you need to pull data.
 
C++ does it too though :/
 
who cares about semantic's.. convenience FTW
 
6:11 PM
@ssube It's used more liberally
 
@ssube do you have a moment?
 
@jAndy yeah, try-catch all the things!
 
@ssube yes, you're right
 
grr you tricked me !
 
Wait hold on I might have it
 
6:12 PM
@SterlingArcher yes, as long as it doesn't get in the way of my sammich.
 
Goodnight
 
@StijnBernards Welcome to the JavaScript chat! Please review the room pseudo-rules. Please don't ask if you can ask or if anyone's around; just ask your question, and if anyone's free and interested they'll help.
 
but catching the actual exception gets that info
 
no for real, try-catch really makes not much sense in ES, unless its really things you can't influence whatsoever (like environmental issues)
 
also, try catch is very useful in userscripts, since you can't actually modify the script for the page, but you need to do things :D
 
6:12 PM
if you're using try-catch to catch "code" errors, you're doing it clearly wrong
 
Exception != error
 
@copy that just seems like a terrible way to write code. It might be my C/++ background, but exceptions are slow and dangerous, and should only be used when something is wrong.
 
if you can test for it to avoid using a try catch then you should
 
@jAndy eh no
 
Otherwise you end up with so much noise, you can't actually catch problems.
 
6:13 PM
@ssube Sounds like your C++ background
 
@Klors and @ssube have similar avatars :/
 
some things you can't test for, they should be caught (catched?!) :)
 
IF you're throwing every time data looks funny, and you log when you throw, then how are you supposed to find out the disk has fallen off the box in the logs?
 
@AwalGarg wat no
 
You only catch the exceptions you want to catch
And of course you don't write try-catch over large chunks of code
 
6:15 PM
@jAndy I mean, you don't really catch all the errors... why would you write code which gives errors?
 
you only catch the exceptions you can't catch anyhow
 
@ssube do you know how to redirect a route in Spring MVC? My application initially runs /ivr/[en/sp] to determine client language, which sets some flags, and then I need to redirect it to /ivr
return "/ivr"; didn't work twilio just said "slash ivr"
 
@SterlingArcher Not at all, sorry. I've been avoiding spring for a few years, so I don't know any of the internals anymore.
 
@AwalGarg what I'm really saying is, try-catch is only for situations where you cannnnnooott do anything about it from "within the code"
 
Ok no worries!
 
6:16 PM
I don't know how to express myself any better
 
@jAndy That's what we're disagreeing with
 
then we can agree to disagree
because that's lazy bs
 
@jAndy try catch is for things like - try to do this, but don't let the code break if there is an exception.
 
Functions should only do one thing
 
@copy See, that is fine, you only catch what you can fix. But eagerly throwing... how can you possibly have performant code then?
 
6:17 PM
If a function can't do that one thing, it should throw an exception
 
back to @Dan 's original question... what you could really do with is the safe navigation operator... object?.property?.subproperty
 
We will deal with the exception, fine. But not let the entire script go down...
 
Isn't a try catch block only used for eg getting data from a different server. Becuase you'll NEED to have a response and your code will die without any? or something.
 
Yeah, I'm with jAndy. It works; Python likes to do it... but I'm not a fan of it.
 
@AwalGarg arguing like that, you really declare that "put a big try-catch clause" around your entire code is ok
 
6:17 PM
If I saw it in a code review, I'd flag it.
 
@Klors avoid if (x != null && x.y != null && x.y.z != null). It's just sugar for those chains, really.
 
@ssube Only a small part of the code needs to be performant, that part you write without exceptions (after figuring out the bottleneck)
 
@jAndy haha no. Think of it like this way: how much of your code do you think has a realistic possibility of not working?
 
@AwalGarg Umm the part you haven't unit tested.
2
 
@Retsam after unit testing...
 
6:19 PM
Like, write your code to work, and test it to verify it works. Don't just throw exception handling in cause "shit happens".
 
@Retsam so all of the code!
 
@copy but aren't you supposed to throw if anything ever goes funny? A buffer is empty, or data is unusual, or auth fails?
 
@rlemon xD
 
Consider this:
 
@AwalGarg do you want to trycatch every line of code that has the possibility of not working?
 
6:20 PM
You need to write a function to find the position of a substring in a string
 
@AwalGarg after unit testing, there are only possibilites left I just described
 
What do you do when the substring isn't found?
 
"things you cannot catch in-code"
 
@StijnBernards if your code has a lot of them, then you suck. Else, yes.
 
@Meredith Return -1.
 
6:20 PM
so mozilla array.sort uses mergesort, what does chrome use?
 
@Retsam Why?
 
try {
  var a = 1;
} catch( e ) {
  console.log( 'there may have been an invalid left side assignment, or freaky unicode crap. I don\'t know. It\'s handled at least')
}
5
 
@Meredith In C++, return null and write an error to the output param that took a struct. In Java, throw a checked exception. In JS, return -1. In Python, throw an exception wrapped in an exception thrown by an exception?
 
@Meredith Because it's unambiguous; (it's standard, but that's a cheater answer) and it's an expected and reasonable result.
 
@AwalGarg Why would you want to do that?
 
6:21 PM
@ssube Uhm, no?
 
Then what is the python way to do it?
 
thing is that, you wanna see if a.b.c.d.e.f is there or not, so you try to see if it is accessible, else you do something else. What is out of place?
 
@Retsam You return -1 because the user needs to know that the string wasn't found and the other numbers could be legitimate results
 
@AwalGarg I never try catch my mysql querys, ajax requests or anything because there's a less than 1% chance of them failed. Would you say I should've try catched those instead because of the 1%
 
@StijnBernards DB queries and AJAX both absolutely should be caught, because they're completely dependent on the network, which can go out at any time.
 
6:22 PM
If you need to tell whoever called a function that the operation couldn't be performed
 
I wrote a function before to test object property chains. that was some horrible ass code
 
@ssube That depends. Invalid internal state that can't be fixed would be a case for assertions (that aren't caught), for authentication you would probably use a NULL equivalent
 
It's better to throw an exception than return a nonsemantic value
 
@rlemon I have a decent one around here somewhere...
 
Things like writing to a file is a good use case for exceptions
 
6:23 PM
I'm pretty sure I used eval
maybe not
 
@StijnBernards Imagine you go to wallmart, and you are looking for something. The store doesn't have it. Do you expect to be kicked out of the store?
 
@Meredith Ehh, no I really don't think it is.
 
@ssube Would you trycatch a connection to lets say a local database?
 
@mere
@Meredith I disagree (and can't type)
 
@Retsam Ok let's say you call a function foo and it returns -1
Why did it return -1?
 
6:24 PM
@rlemon haha dito..
 
@AwalGarg That totally depends on your application structure though.
 
such a checking is true pain the ass
 
@Meredith I dunno, what does the documentation say?
 
There is no documentation
 
@AwalGarg if you goto a walmart and it's missing everything. Would you want to visit it?
 
6:24 PM
@Meredith Well there's your problem.
To me, exception means "the thing you're asking me to do can't be performed", e.g. I want to read from a database, but the database doesn't exist.
 
That's exactly how it's used here
 
function lookup(query, obj) {
  var parts = query.split('.'),
       cache = obj;
  for( var i = 0, l = parts.length; i < l; i++) {
    if( parts[i] in cache ) cache = cache[parts[i]];
  }
  return cache;
}
var a = { b: { c: { d: "Hello World" }}};
lookup("b.c.d", a);
"Hello World"
 
@StijnBernards exactly. You are saying that you don't use try..catch for this, because you don't require it much. This is unfair..
 
The operation couldn't be performed because of some condition
 
6:25 PM
If I want to know "Is 'foobarbaz' a substring of 'hodor'", there's no reason to give me an exception; you can do that fine.
 
@StijnBernards Absolutely. What if you run out of file handles/sockets? If the DB is down or in a state where it can't write? The backing store is out of space?
You want to return a reasonable error to the client in all those cases, most likely.
 
@Meredith "nowhere" is a valid answer to the question of "where is this string within another string", choose a testable value for "nowhere" and return it..... exception for me is "that wasn't a string or something else blew up"
 
@Klors That's an error, not an exception
 
damn
 
@ssube That is true, but I wouldn't try catch every query or request. If thats what you're saying?
 
6:26 PM
@rlemon That implementation started pretty simply, but got pretty complicated (supporting not and method calls) after a bit. It's still reasonably clean.
 
@rlemon Recursion???
 
@ssube I would have lodash would have implemented a helper that does that?
 
@StijnBernards yeah, absolutely. They're very likely to fail in a predictable, well-handled way.
@rlemon lodash has one, or you would add one to it?
 
@rlemon lil bit sloppy tho
 
@ssube no wondering if it already has one
s/I would have/I would have thought/
 
6:29 PM
No, I'm pretty sure lodash doesn't have a helper for that.
 
Dammit this is tough
 
@SterlingArcher something something TWSS
 
@Meredith how is that a useful distinction?
 
Because an error occurs when something goes wrong and an exception occurs when something exceptional happens
 
@Meredith And you think "this string isn't in this other string" is "exceptional"?
 
6:33 PM
Well yeah
 
@Meredith again, how are those useful distinctions if "it's not in the string" is a valid response?
 
I mean, if you have to return a result that is an exception to the pattern of the other results
It's an exception
 
@Retsam when you are looking for a string in another string, it seems exceptional to me :P
but what is there is more convenient xD
 
"Return the index of the substring except when the substring isn't found, then return -1"
 
@rlemon @Retsam yeah, don't think underscore or lodash has it.
 
6:34 PM
> except
 
It is super useful when you don't know what data will be looking like, like: jsfiddle.net/mtw05Lmo
 
@Meredith Or you just accept that the definition of "the index" is -1 for things not in the other thing.
 
Only thing I don't like is the double-array, if you only need a single function call in the chain
 
That's still an exception
 
@Meredith if there was a separate test for "does this string contain this string" then it could be an exception, but as it performs that function, it's a normal part of its operation
 
6:36 PM
Not really, no. It always returns "the index".
I could just as easily say the definition is "return the index of the substring, unless the string begins with the substring, in which case return 0" and call that "an exception".
 
Yeah, no
 
It all comes down to how you define "the index of the substring" and there's no single universal definition of that phrase.
 
One's a pattern, the other isn't
 
Whatever. I don't really care to keep arguing the point. Again, if having to catch an exception every time you don't find a string you're looking for is what you want to code with... go for it.
But I'm perfectly happy with getting a -1, personally.
 
you get used to it
it isn't ideal
 
6:38 PM
@ssube I figured it out :)
Took a while, but god dammit I got it
 
@Retsam because the spec says so?
 
posted on December 16, 2014

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); try { var pageTracker = _gat._getTracker("UA-3727700-1"); pageTracker._trackPageview(); } catch(err) {}

 
@SMBC get a better deployment process
I'm sick of getting feeds 2 minutes before you upload the damn picture
 
@AwalGarg Because it's consistent, documented, understandable, common convention, etc
 
@Retsam is it common? and is it intuitive?
@Sam \o/
 
6:40 PM
I hate this notion.
 
Sam
Heya
 
@rlemon Yeah; I think so. Even if you had no documentation but understand the word "index", I think you could pretty well guess what "foo".indexOf("bar") === -1 means.
 
@Retsam it isn't consistent with any other string functions tho
-1 means negative from the end
 
Redis uses -1 to represent expired time :/
 
!!> "123".slice(0,-1)
 
6:42 PM
@rlemon "12"
 
problem is there are inconsistencies all over the place. you can pick a side and like it, but there is still a fractured system. and that is bad.
 
if you're expecting complete consistency from javascript then you've not been reading the documentation
 
I more meant it's consistent with how other languages do indexOf.
 
@Klors I expect complete consistency with every language I use. I don't get it, but I expect it.
3
don't be complacent with mediocrity
 
lol
 
6:44 PM
"My car works, but I wish it worked better" <-- mediocrity acceptance
 
Ehh, it's inevitable that there's some ambiguity, if you're just looking at function signatures.
 
@rlemon I find I insulate myself from more mistakes if I expect inconsistency
 
@SterlingArcher also something you can fix. Changing stdlibs is a little bit harder.
 
True. Harder but cheaper!
 
@SterlingArcher "my car works, but there is no heater and no radio, but none of this model have any, so those are not important anyways"
 
6:45 PM
Does .substr(2, 5) return "characters between 2 and 5" or "5 characters, starting at position 2"; it's completely arbitrary, and depends on what language you're looking at.
 
@rlemon STOP IMPROVING MY METAPHORS DAMMIT
 
metaphor or analogy ?
or both... ?
 
!!mdn substr
 
There's no way to "intuit" the behavior of "hello world".substr(2,5). You just need to suck it up and look at what the documentation says.
 
6:46 PM
@rlemon lets just go with anal and get it overwith
analphors
 
@Retsam and that is poor
 
metanalogies
 
just because we have no options against it currently, doesn't make it 'good' by any means. just means we have to accept it for now.
 
thats so obviously #gayjoke fishing for stars
whatever, I love it !!
 
what's a metaphor? ... complaining about bad questions and audits
 
6:47 PM
pfft I don't want hats
I got my menorah
 
badumtish
 
That wasn't a joke, I literally have my menorah hat
 
it wasn't aimed at you, it was at me
 
booooooooo >=(
 
14
Q: try-catch in javascript... isn't it a good practice?

akpThere is a provision for try-catch block in javascript. While in java or any other language it is mandatory to have error handling, I don't see anybody using them in javascript for greater extent. Isn't it a good practice or just we don't need them in javascript?

 
that's actually a good answer by Raynos
20
A: try-catch in javascript... isn't it a good practice?

RaynosOne should avoid throw errors as the way to pass error conditions around in applications. The throw statement should only be used "For this should never happen, crash and burn. Do not recover elegantly in any way" try catch however is used in situation where host objects or ECMAScript may throw...

 
In case you didn't know, Crossing the Rubicon by The Sounds is a pretty fantastic album.
 
I'm jealous of Raynos' hair
Is there some anti-porn legislature going on..?
 
yea
UK banned a bunch of crap
"face sitting" protests all over reddit
 
as the only example of banned activity that you can do in public without being arrested
Yep. You can do it as a protest but you can't film it. Ludicrous.
 

« first day (1522 days earlier)      last day (3419 days later) »