Brian's Blog

items I see across my tribes

Optional parameters in Javascript functions

January 04
by briancarter 4. January 2012 08:49

Most programming languages have a mechanism which allows optional parameters to be passed to a function/method and if they are not included in the call, default values are set.

This is not the case in Javascript. But thankfully, it is still possible to set up a function that take optional parameters.  Consider a function defined like so:

 
 
function myFuncName(param1, param2) {

In Javascript, it is perfectly legal to make a call to this function in any of three ways:
myFuncName(1, 2);
myFuncName(1);
myFuncName();

They will all work. In javascript, if you want the parameter to be optional, you need to add code in the function itself to deal with the cases when a parameter value is not given in the call. For example, consider the function above where we want to make the second parameter optional and if the second parameter is not given, we set a default value of '3.14'. That can be done like so:
function myFuncName(param1, param2) {
   // Set the optional parameter if needed
   if ( param2 === undefined ) {
      param2 = '3.14';
   }

   ...
}


You can do this for as many parameters as you like, but you need to be sure to order the parameters so that the "most optional" ones are further to the right since as soon as you leave off one parameter, it starts removing values from the right-hand side of the parameter list.

Categories: Development

Godaddy + PHP + mySQL = php5.ini

December 04
by briancarter 4. December 2011 11:24

Setting up the stack at Godaddy, had to update the php.ini file.  Defaults require you to put php5.ini in the root of your account folder.

Note, extension is required for mySQL: php_mysql.dll

Attached is the ini file that I used.  Update "yoursitehere.com" with your info and "#######" with your godaddy account info.

 

 

php5.ini (550.00 bytes)

Categories:

Force Vertical Scrollbar

November 13
by briancarter 13. November 2011 13:16
html {
       overflow-y: scroll;
}

This is invalid CSS, but it works in everything except Opera. The reason for this is to prevent "centering jumps" when navigating back and forth between pages with enough content to have a vertical scroll bar and pages that do not.

Another solution that works well in all widely used browsers with only a few lines of code, the document height will always be at least one pixel longer.

html, body {
  height: 100%;
  margin: 0 0 1px;
  padding: 0;
}

I typically use them both.  Avoids the ugly bouncing when going from page to page or when AJAX loading a div.

Categories:

Chanakya was a teacher

November 08
by briancarter 8. November 2011 22:26

Interesting guy, read more at: http://en.wikipedia.org/wiki/Chanakya

Good quote:

Once you start a working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest.
Chanakya

Read more:http://www.brainyquote.com/quotes/authors/c/chanakya_2.html#ixzz1dAfLALDI

Categories:

WP Find my phone :-)

November 08
by briancarter 8. November 2011 20:42

Used the Windows Phone find feature.  Phone fell out while on the trampoline.  Volume turned off while at the dentist, thought I had left it there until the site told me it was out back.

image

Categories:

GUIDs and guessing

November 08
by briancarter 8. November 2011 09:21

image

GUIDs get used a lot in creating unique user IDs and session keys for web applications. I've always wondered about the safety of this practice. Since the GUID is generated based on information from the machine, and the time, along with a few other factors, how hard is it to guess of likely GUIDs that will come up in the future.

.NET Web Applications call Guid.NewGuid() to create a GUID which is in turn ends up calling the CoCreateGuid() COM function a couple of frames deeper in the stack.

From the MSDN Library:

The CoCreateGuid function calls the RPC function UuidCreate, which creates a GUID, a globally unique 128-bit integer. Use the CoCreateGuid function when you need an absolutely unique number that you will use as a persistent identifier in a distributed environment.To a very high degree of certainty, this function returns a unique value – no other invocation, on the same or any other system (networked or not), should return the same value.

And if you check the page on UuidCreate:

The UuidCreate function generates a UUID that cannot be traced to the ethernet/token ring address of the computer on which it was generated. It also cannot be associated with other UUIDs created on the same computer.

The last contains sentence is the answer to the question. So I would say, it is pretty hard to guess. 

If someone kept hitting a server with a continuous stream of GUIDs it would be more of a denial of service attack than anything else; essentially the need to track request and failed request.

The possibility of someone guessing a GUID is next to nil.

Categories:

IIS7 Global.asax not calling BeginRequest

November 06
by briancarter 6. November 2011 13:56

On Godaddy, had an issue that Application_BeginRequest wasn’t getting called.  Turned out to be an IIS 7 issue.  Resolved by adding the following to modules in web.config:

 

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
  </modules>
</system.webServer>

Categories:

Unique Hash

November 05
by briancarter 5. November 2011 08:51

image_thumb

A hash function that returns a unique hash number/string is called a universal hash function.

Thus, we say that our hash function has the following properties

  • it always returns a value for an object.
  • two equal objects will always have the same value
  • two unequal objects may not always have different values

When we put objects into a hashtable, it is possible that different objects might have the same hashcode. This is called a collision. Here is the example of collision. Two different strings ""Aa" and "BB" have the same key:

"Aa" = 'A' * 31 + 'a' = 2112
"BB" = 'B' * 31 + 'B' = 2112

The problem I’m addressing, given 2 GUIDs, will the hash be unique? More research on my part is required, but I know the probability is extremely low – especially using a unique salt.  I made sure by it was  unique by adding a unique index (constraint) on the hash column in SQL Server (paranoid).

If you have any thoughts on my problem of 2 hashed GUIDS, please let me know.

Thank You for listening!

Categories:

SQL Saturday 10/29

October 30
by briancarter 30. October 2011 10:47

image

For a day, hundreds of people gathered to become students – to explore over 5 tracks and dozens of sessions.  Vendors made this event free to all, the University of Louisville provided the place of learning.  A special thanks to the SQL Saturday volunteers and our leader Malathi Mahadevan for making this event a success.

Categories: Development

Everyone Remote

October 24
by briancarter 24. October 2011 21:56

Sometimes talking with people 1/2 way around the world is easier than the people sitting next to you.     

One thing I found when working with people, if you need to think through an idea – some people sitting right next to you are a million miles away.

The key is to find a way to get everyone in the same room, no matter the distance apart.

Thank You for listening. 

Categories: Tribes


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.