4
gitgit
7y

1. getSomeData(params, ((err, data) => {
2. if(!err && data) {
3. try {
4. data = JSON.parse(data);
5. } catch(e) {
6. return null;
7. }
8. return data.someParam;
9. }
10.}

Nothing like bad practice in above code but I always feel that the line 4 should be replaced by below.

4. var result = JSON.parse(data);

and then use result variable to get data one is looking for, like below

8. return result ? result.someParam : null;

Your thoughts?

Comments
  • 1
    By doing "data = json.parse(data)" you modify parameter, which is considered bad practise, so you should make a new variable for sure.

    Personally, I do it like this:

    Try{
    Return json.parse(data).someParam;
    } catch (e){
    //Do whatever
    }

    Much cleaner and does not affect readability.
  • 1
    return null;
    Seriously?
  • 0
    @Mitiko any reason why not to return null?
Add Comment