Default variables in JavaScript functions

This is what we call default/optional parameters:

Here, we passed “this is my new title” string as $title to the new_post($,$,$) function. The function could parse 3 parameters, however, we only provided one parameter: $title. The other two parameters have default values, defined on between the paranthesis like $subtitle = “no subtitle”.

Unluckily, doesn’t have such a feature yet. If you don’t pass one of the arguments to the function in , it gives “undefined” type to the variable, which often breaks our code unless we catch them explicitly.

If you want to experiment with optional parameters drafted in EcmaScript 6, see MDN.

There are few widely mentioned options to implement this kind of behavior in JS. Let’s start with the one I find easiest and cleanest.

Use OR

We will  use “||” (OR) comparison operator to check if our variable is defined or undefined, and if undefined, we will assign a value to it.

This introduces someone to someone else :) We are not really interested his/her age, so we made this parameter optional. Let’s see what happens when we run it:

Normally, you would be more careful with such a function as you would only want to accept integer values for “age” variable.

Latest news

There is this sweet JavaScript library which allows you to manage your function in a better way. I have never used it because I just discovered it today.

It’s called ArgueJS and has several interesting features like default values, type checking (it doesn’t “cast” them as much as I see) and optional parameters. The library makes use of the “argument” variable of JavaScript function. “argument” is an implicit variable (an Array), containing all of the arguments passed to the function. This is the second of methods you can use for default value assignment on JS.

It seems like a perfect match for larger JS applications.

Use “arguments”

Let’s go on with the function above.

The function introduction() has no pre-defined variables to accept, but ”arguments” variables contains every parameter passed to the function regardless if they were expected or not. In this method, we should always know the order of variables that should be passed to the function: index 0 would always be name and index 1 would always be age (unless if you make a workaround for a dynamic ordering). Let’s test:

That’s all. Use the one that makes the most use to you.


Laravel 4 – Custom Packages

When using Artisan to create your own custom package in 4, there is this very simple step that is never mentioned in the documentation.

You can follow this link to learn more about how to create a package in L4: http://jasonlewis.me/article/laravel-4-develop-packages-using-the-workbench

But. Just after you created your package with Artisan tool, you should go to your package directory (mostly /workbench/vendor/library/, or going on the example of Jason Lewis, /workbench/solitude/profile), and run:

This way, the package will install the dependencies (like laravel support) and add himself to the system. This is never mentioned anywhere and I struggled to find it out.

Thanks to: http://forums.laravel.io/viewtopic.php?id=7851


Question about Dev to Production

You can see and (please) reply my question about how to switch from development environment (take this saying as generally as possible) to a production environment.


Web application with any language/platform

Can a web application be made in C#, C++, Fortan, Pascal, etc?

My answer to the question from StackOverflow:

I believe you can use any platform/language for web development, as long as they provide necessary functionality.

A web process includes a few steps:

  1. Listen to a port to which client (browser, mostly) sends requests (which is :80 by default)
  2. Do something. Access to the DB, talk to somewhere. Enjoy yourself.
  3. Send an output back to the client

So, if you can make these kinds of specifications comfortable, then you are good to go with any language/platform.

Or you can build a bridge between a currently used web server (like Apache, Nginx, IIS and such) and your Pascal application. This is what most developers would go.

I am currently using , Java Application through command line, Python through command line all together in a single application. I love the way command line works, simple and fast. And we will add another server with Windows to the application for a Windows-specific function. So… If you have enough ropes, you can tie anything together.

Correct me if I am wrong :)


Response now, process later

In a application, I had some process going on. However, it too almost 6 seconds to complete the request on the server. Client would have to wait 6 seconds to get a response and see the action completed. This was bad. Really bad.

In AJAX request, I would process an action and then, I would post to the Facebook wall of the user, through Facebook PHP SDK. The request to and waiting for response of Facebook was slowing the response to the client. Though, I already had the data to give the client back, before I interact with Facebook API.

So… I thought, why don’t I just give the required result back to the client instantly before interacting with Facebook which slows the response time. Good boy! We can do it with PHP and flush stuff. See:

ob_end_clean() : clean current output… :D this function removes any output if you have given ob_start() command in previous lines. Most frameworks does. (I use CodeIgniter in this project). read more about this function here

ignore_user_abort() : Amazing function! The core of what we want to achieve. Normally, if the client receives a Response from the server, PHP stops execution. We don’t want it to stop, even after we provide response to the client. read more about this function here

ob_start() : This one turns of output buffering. I recommend reading more about this. You can do lots of tricky stuff with this cutie putie little bitty function. read more about this function here

header("Connection: close"); : Now we are setting HTTP response to the client. We do it by commanding header()  function in PHP. On this line, we say to the client (the requester) that this connection is finished and closed. You can let the user know the Ajax execution -or whatever- has finished. read more about this function here

header("Content-Length: " . ob_get_length()); : If the output has no size, it means null, right? It seems browsers are not that smart… We need to let them know the size of the content. ob_get_length()  makes it possible to get the size of content between ob_start()  and  ob_end_flush() . read more about ob_get_length() here, and header() here

ob_end_flush(); : Now… We are giving the client, which is often a browser, the response in buffer. What was it? See the line starting with echo ? Yep. This is our buffer between ob_start and this line (ob_end_flush()). There is mindblowing concept here, between all these ob’s and flushes… Here are a couple of links to read more about them:

http://stackoverflow.com/questions/7355356/whats-the-difference-between-ob-flush-and-ob-end-flush
http://stackoverflow.com/questions/4191385/php-buffer-ob-flush-vs-flush

Basically, ob_end_flush()  function flushes (read as “printing the data in buffer”) the data between the previous ob_start()  and itself and frees the buffer (read as “removes the data, thus more empty space we have in buffer”). And we, then, should do flush() , because other components, such as a web server, may want to add something else to the response. read more about ob_end_flush() here, and flush() here

Happy fast responses!

(

As a first time techie blogger, I think I am going quite good, huh?

Note to the reader: I am trying to give as much info as possible on my posts. I spend a good amount of time on them and provide you links, tooltips (mouse oveeeeeer them!), links on concepts and and others. Before googling, try to hover on the word here. I love saving time!

)


On-site fix: Yoast and Double Site Name

I have installed yesterday. I like how it works and its beautiful settings page.

HOWEVER. There was this little issue: I had my blog name on the site title twice. But this is a theme issue. The theme had hardcoded title function in its  header.php

Let’s see what is in .:

As Yoast handles titles on its own (it binds filter for ), we no more need bloginfo()  part here. Stay out of our way!

Good boy.


Never cURL Google API!

Yesterday, I spent an hour of my precious life to figure out a 401 Authorization error. I was using , specifically to insert, to OCR and then get back the results. However, none of exportLinks or downloadUrl properties seemed to be working well for me.

I was using to retrieve the content of the URLs given on exportLinks property. WRONG!

If you are using Google SDK Types to query API, you should use Google’s HttpRequest class to query with authorization. Muuuuch simpler. Let’s get to it:

This is close to what I tried with cURL to download a Google Drive file which I inserted via API with  ocr: true
However, I figured out that there was a much better way to do it with no authorization struggle:

This is a piece from my brand new app. Well, the only thing you see from is  App::abort() function anyway. This way, I could get the file content with no hassle.

Enjoy it!

(come Googlers cooommmmeeee)


First post :)

Ops!

Here we are. I have gathered information on many topics, from all around the world since I was born. And I thought, why don’t I give what I gathered back to the universe? We have been talking about Karma since WWII and it looked like a very smart move now!

I even paid for the domain (English blog, $13, Turkish domain $13) and the theme ($35, Basic, from ThemeForest; amazingly simple).

Let the beauty of knowledge begin… (was this too puffy?! come ooooon.)

I will most probably talk about these stuff: (vanilla, CodeIgniter, 4), (vanilla, jQuery, CanJS, ExtJS, Sencha Touch), various APIs such as API, Chrome Browser, Facebook API, Twitter API, LinkedIn API, SugarCRM and such (they are never as good as an OPI…) general development flows/issues/ideas, startups, hints on servers and computers, CSS, HTML 5 etc… This will probably be one of those blogs you come across while searching a solution for an error… :)