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.

Weird code for defining `get` method

See original GitHub issue

Hi. I’m creating a library that can be used to write universal code and mount it easily in Express on the server and have it run standalone on the web: uexpress so I’m reading the Express code and I noticed that the way the get function (that returns the application settings) is defined is (imho) really weird.

First of all, it seems the actual code for Application.get is in the set function:

app.set = function set(setting, val) {
  if (arguments.length === 1) {
    // app.get(setting)
    return this.settings[setting];
  }

  // ....

application.js#L354

And then later the actual Application.get method is created as an exception in the code that generates the convenience functions for the HTTP methods:

methods.forEach(function(method){
  app[method] = function(path){
    if (method === 'get' && arguments.length === 1) {
      // app.get(setting)
      return this.set(path);
    }

    // ...
  };
});

application.js#L473

Can anyone explain why it is done this way? Or is this just for historical / back. compat reasons?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Feb 12, 2017

Hi everyone, I know the issue is already closed, but I just wanted to see if I could shed some more light. I can’t really explain the exact history on it, as the changes were made before I became deeply involved, but I can explain what it is at least doing.

See, some time long ago, app.set(name, value) was the way to set a setting to a value and app.get(name) was the way to get a setting value. The problem is that app.get is also the method for creating a GET route, which leads to a weird duel-use method. Back in the PR referenced above, the idea was to move that getting behavior into app.set as app.set(name), with the understanding that set was now a shortened word for “setting”, not “set” as in to set something. For back-compat app.get(name) still lives on, and has not really been revisited since there was talk right after 4.0 that perhaps they should both be moved to app.settings.get and app.settings.set or similar.

0reactions
Downloadcommented, Feb 12, 2017

@dougwilson Thanks for clarifying!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Weird C++ syntax: type definition before a function call
It's initializing the context_scope variable with debug_context. Context::Scope is the type (here's one ref page ...
Read more >
Python But It's Weird. Code snippets that will question your…
In programming languages like C/C++/Java/Go, we have increment (++) and decrement operators (- -) that update the value of the variable by 1....
Read more >
List of HTTP status codes - Wikipedia
There are five classes defined by the standard: 1xx informational response – the request was received, continuing process; 2xx successful – the request...
Read more >
The most obscure "Hello, world!" program - Mathspp
In this article we go over the most obscure “Hello, world!” Python program I have ever seen. A really weird code snippet that...
Read more >
One weird trick that will change the way you code forever
Test Driven Development is one weird trick that will change the way you code forever. It will make you a more badass developer....
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