ConfigurationObject

From APIDesign

(Difference between revisions)
Jump to: navigation, search
Line 5: Line 5:
return text.toUpperCase();
return text.toUpperCase();
}
}
-
upper("Hello World!") == "HELLO WORLD!"
+
upper("Hello World!") === "HELLO WORLD!" || error();
</source>
</source>
Line 17: Line 17:
return text.toUpperCase();
return text.toUpperCase();
}
}
-
upper("hello world!") == "HELLO WORLD!"
+
upper("hello world!") === "HELLO WORLD!" || error();
-
upper("hello world!", true) == "Hello world!"
+
upper("hello world!", true) === "Hello world!" || error();
</source>
</source>
Line 24: Line 24:
<source lang="javascript">
<source lang="javascript">
-
function upper(data) {
+
function upper(data, firstLetterOnly) {
 +
if (typeof data === "string") {
 +
data = {
 +
"text" : data,
 +
"firstLetterOnly" : firstLetterOnly
 +
};
 +
}
if (data.firstLetterOnly) {
if (data.firstLetterOnly) {
return data.text.substring(0, 1).toUpperCase() + data.text.substring(1);
return data.text.substring(0, 1).toUpperCase() + data.text.substring(1);
Line 32: Line 38:
upper({
upper({
"text" : "hello world!"
"text" : "hello world!"
-
}) == "HELLO WORLD!"
+
}) === "HELLO WORLD!" || error();
upper({
upper({
"text" : "hello world!",
"text" : "hello world!",
"firstLetterOnly" : false
"firstLetterOnly" : false
-
}) == "HELLO WORLD!"
+
}) === "HELLO WORLD!" || error();
upper({
upper({
-
"text" : "Hello World!",
+
"text" : "hello world!",
"firstLetterOnly" : true
"firstLetterOnly" : true
-
}) == "Hello world!"
+
}) === "Hello world!" || error();
</source>
</source>
Adding named parameters is more easily evolvable. Moreover it is certainly easier to use ten named arguments than a function with ten parameters. No surprise the [[ConfigurationObject]] becomes more and more popular in many [[JavaScript]] libraries. As the core of [[DukeScript]] ecosystem is built around wrapping [[JavaScript]] libraries with type-safe [[Java]] [[API]]s it becomes more and more important to find proper realization of such [[API]] in [[Java]]. Here are few options.
Adding named parameters is more easily evolvable. Moreover it is certainly easier to use ten named arguments than a function with ten parameters. No surprise the [[ConfigurationObject]] becomes more and more popular in many [[JavaScript]] libraries. As the core of [[DukeScript]] ecosystem is built around wrapping [[JavaScript]] libraries with type-safe [[Java]] [[API]]s it becomes more and more important to find proper realization of such [[API]] in [[Java]]. Here are few options.

Revision as of 10:47, 22 February 2015

ConfigurationObject pattern is often used by JavaScript libraries to deal with evolution in a manageable way. While TheAPIBook advocates getting ready for the fact that first version of any API is never perfect, people keep repeating the same design mistake again and again: optimistically ignore the need for evolution! Usual history of an API starts with introducing function with one argument:

function upper(text) {
  return text.toUpperCase();
}
upper("Hello World!") === "HELLO WORLD!" || error();

then one finds out additional argument is needed:

function upper(text, firstLetterOnly) {
  if (firstLetterOnly) {
    return text.substring(0, 1).toUpperCase() + text.substring(1);
  }
  return text.toUpperCase();
}
upper("hello world!") === "HELLO WORLD!" || error();
upper("hello world!", true) === "Hello world!" || error();

and later another one, and another and so on, until one realizes the whole API is total mess and it is time to switch to ConfigurationObject design pattern:

function upper(data, firstLetterOnly) {
  if (typeof data === "string") {
      data = {
          "text" : data,
          "firstLetterOnly" : firstLetterOnly
      };
  }
  if (data.firstLetterOnly) {
    return data.text.substring(0, 1).toUpperCase() + data.text.substring(1);
  }
  return data.text.toUpperCase();
}
upper({ 
  "text" : "hello world!"
}) === "HELLO WORLD!" || error();
upper({ 
  "text" : "hello world!",
  "firstLetterOnly" : false
}) === "HELLO WORLD!" || error();
upper({ 
  "text" : "hello world!",
  "firstLetterOnly" : true
}) === "Hello world!" || error();

Adding named parameters is more easily evolvable. Moreover it is certainly easier to use ten named arguments than a function with ten parameters. No surprise the ConfigurationObject becomes more and more popular in many JavaScript libraries. As the core of DukeScript ecosystem is built around wrapping JavaScript libraries with type-safe Java APIs it becomes more and more important to find proper realization of such API in Java. Here are few options.

JavaBeans like Style

Personal tools
buy