question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newspaper metaphor support

See original GitHub issue

The newspaper metaphor (introduced by Robert C. Martin in his book Clean Code) is a useful concept that puts the highest-level code to the top, so it’s easy to figure out the high-level goals of a module or class. Because you start reading at the top, which is what we usually do anyway. But if it starts with the implementation details, it’s much harder, and I usually ask myself: why is this function needed, where is it going to be used? It’s a constant struggle for me.

Your style guide never mentions the newspaper metaphor, which is somewhat incompatible with your view that “functions should not be used before they are defined”:

Functions

[7.1](https://github.com/airbnb/javascript#functions--declarations) Use named function expressions instead of function declarations. eslint: [func-style](https://eslint.org/docs/rules/func-style)

    Why? Function declarations are hoisted, which means that it’s easy - too easy - to reference the function before it is defined in the file. This harms readability and maintainability.

Even the ESLint rule description fails to talk about the newspaper metaphor:

https://eslint.org/docs/latest/rules/func-style

But, the biggest problem is not hoisting, IMO. It’s the use of var. When we only use const or let, the newspaper metaphor can be used most of the time:

const main = function() {
  lowerLevelFunc();
}

const lowerLevelFunc = function() {
}

// This would normally be the first line...
main();

Not perfect, sadly. The language limits us here. But that doesn’t mean we should introduce a style guide that makes the situation even worse.

(Off topic, but I pretty much agree with this comment: https://github.com/airbnb/javascript/issues/794#issuecomment-656928852 , i.e., not giving functions 2 names. If people can’t keep their browsers updated, then they will inherit the security vulnerabilities too.)

Also, I would consider the use of arrow functions almost everywhere (i.e. function expressions), for the reason that this cannot be overridden by callers, which reduces mental complexity or any defensive code to be written. But the world does not always agree:

https://davidwalsh.name/i-dont-hate-arrow-functions

(In short: a function that looks like a variable assignment is confusing and not easy to read.)

Then, is it really worth standardizing the way we declare functions? For callbacks, sure, I agree, but for higher scopes, the picture isn’t clear. It’s more like a language design problem for which a straightforward solution cannot be given currently.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
rkrisztiancommented, Oct 17, 2022

I think your guide is one of the best out there, seems very thoroughly thought out, even given explanations not just rules, so I do plan to follow, I just couldn’t get my head around this one problem. Thanks for the explanations you gave so far.

0reactions
ljharbcommented, Oct 24, 2022

The point of style guides is to enforce largely subjective rules in an opinionated fashion. If it were objective, no guide would be needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Metaphor in newspapers - LOT Publications
CHAPTER 5 Metaphor in news texts: A quantitative analysis ... support to the idea that news ranks high in direct metaphor because it...
Read more >
War and Violence Metaphors in Newspaper Headlines Essay
In this essay, I want to explore three newspaper headlines that utilize metaphors of war and violence to generate a desired feeling towards ......
Read more >
Metaphors in newspaper headlines - Prezi
1st April 2019 · Help your kid face peer pressure with the big rock · Help your kid face peer pressure with the...
Read more >
Reusing Metaphors Across Media Genres
Newspaper Metaphors : Reusing Metaphors Across Media Genres · Full Article · Figures & data · References · Citations · Metrics · Reprints...
Read more >
The Importance of Utilizing Metaphors and Similes in News ...
Keywords: Metaphor, simile, dichotomous, news/mass media, influence. ... and they help the researchers to gather quick and straightforward ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found