Weird code for defining `get` method
See original GitHub issueHi. 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];
}
// ....
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);
}
// ...
};
});
Can anyone explain why it is done this way? Or is this just for historical / back. compat reasons?
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 andapp.get(name)
was the way to get a setting value. The problem is thatapp.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 intoapp.set
asapp.set(name)
, with the understanding thatset
was now a shortened word for “setting”, not “set” as in to set something. For back-compatapp.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 toapp.settings.get
andapp.settings.set
or similar.@dougwilson Thanks for clarifying!