Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

A tutorial (for Javascript) I'm doing suggests we write a function like this one:

function sayHello() {
   //Some comments explaining the next line
   window.alert("Hello");
}

Other than obfuscation are there benefits to writing something like this in real life? If so, what are the benefits?

share|improve this question
5  
It's probably just trying to show you how to define your own functions. –  Doval 19 hours ago
3  
I would never write code in such a way to obfuscate it - code is for the programmer to read easily, not the other way around. Use an obfuscater for obfuscation. –  rhughes 14 hours ago
19  
The benefit is that it wraps a function with parameters. This way if you later want to make your app spanish, you only have to change "Hello" to "Ola" in one place. –  Pieter B 12 hours ago
2  
@PieterB actually, "Hola" –  AcidShout 6 hours ago
    
@PieterB Hola == Hello . Ola == Wave, in spanish. :) –  rpax 4 hours ago

5 Answers 5

Please pardon my memory if I have this incorrect... Javascript isn't my preferred implementation language.

There are several reasons why one would want to have a no arg function wrap another function call. While the simple call to window.alert("Hello"); is something that you could imagine just instead calling directly instead of sayHello().

But what if there is more to it? You've got a dozen places where you want to call sayHello() and have written window.alert("Hello"); instead. Now you want it to do a window.alert("Hello, it is now " + new Date()). If you wrapped all those calls as sayHello() you change it one place. If you didn't, you change it in a dozen places. This touches on Don't Repeat Yourself. You do it because you don't want to have to do it a dozen times in the future.

I worked with an i18n / l10n library in the past that used functions to do client side localization of the text. Consider the sayHello() function. You could have it print out hola when the user is localized to a Spanish language. This could look something like:

function sayHello() {
  var language = window.navigator.userLanguage || window.navigator.language;
  if(language === 'es') { window.alert('Hola'); }
   else { window.alert("Hello"); }
}

Though, this isn't how the library worked. Instead, it had a set of files that looked like:

# English file
greeting = hello
# Spanish file
greeting = hola

And then the library would detect the browser language setting and then create dynamic functions with the appropriate localization as the return value for a no-arguemnt function call based on the appropriate localization file.

I'm not enough of a Javascript coder to say if that is good or bad... just that it was and can be seen as a possible approach.

The point being, wrapping the call to another function in a function of its own is often quite useful and helps with the modularization of the application and may also result in easier to read code.

All that bit aside, you are working from a tutorial. It is necessary to introduce things as simply as possible at the start. Introducing varargs style function calls from the start can result in some very confusing code for a person who is unfamiliar with coding in general. It is much easier to go from no argument, to arguments, to varargs style - with each building on the previous examples and understanding.

share|improve this answer
    
window.alert is also often used as a placeholder during development until a nice modal/popup can be designed/implemented, so, same as the language issue, it can be switched out much more easily. Or if you already have one, a design update a few years down the line might require similar changes. –  Izkata 3 hours ago

I think its useful sometimes for hiding implementation.

 function sayHello() {
  window.alert("Hello");
 }

And this gives you the flexibility to change it later

 function sayHello() {
  console.log("Hello");
 }
share|improve this answer

Other than obfuscation are there benefits to writing something like this in real life? If so, what are the benefits?

  • centralization: although the implementation is a single line long, if it is a line that changes often, you probably prefer to change it in a single place, than wherever sayHello is called.

  • minimizing/hiding of dependencies: your client code no longer needs to know there is a window object, and you may even change the whole implementation without affecting client code at all

  • contract fulfillment: client code may expect a module that has a sayHello function. In this case, even if trivial, then function must be there.

  • consistence of abstraction levels: if client code uses high level operations, it is in your interest to write client code in terms of 'sayHello, sayBye' and some other 'sayXXX' function(s), instead of window objects. Infact, in client code you may not even want to know there is such a thing as a 'window' object.

share|improve this answer
    
I like this answer. Often design is the foremost reason for these functions, especially OO design where you want objects responsible for behaviour to execute functions of other objects with specific behaviour. Imaging your car, more particularly you shifting gears. You shift your gear up by "telling" your stick to shift up. In turn it forwards this call to your gearbox. But in addition it first checks you can shift up from your current position. –  Eric Tobias 9 hours ago
    
The other reasons are good but +1 for mentioning abstraction levels. Using clear abstractions and good function names can make it much easier to read and maintain old code. At the point where sayHello() is called, you're not necessarily concerned with how it's implemented, you just want to understand what is the intention of this line of code. And a function name like sayHello() makes that obvious. –  kkrambo 6 hours ago

As Doval said, this example is probably just trying to introduce you to functions. I'm general, though, it is useful. Especially by specifying some but not all do the arguments, and passing the other ones through, you can make a more case-specific function from a more general function. As a somewhat trivial example, consider a sorting function that takes an array to sort and a comparator function to sort with. By specifying a comparator function that compares by numerical value, I can make a sortByNumericalValue function, and calls to that function are much more clear and concise.

share|improve this answer

I would say it is a combination of the answers given by @MichaelT and @Sleiman Jneidi.

Perhaps there are a number of places in the code where you want to greet the user with a Hello message so you pack that idea in a method. In the method you can translate it or expand on a simple 'Hello' by displaying a longer text. Also you might want to use a nice JQuery dialog in place of an alert.

The point is that the implementation is in one place. You just need to change the code in one place. (SayHello() is perhaps a too simple example)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.