Skip to content

Random Tid Bits

Categories: Flash, Software, Web Development

Table of Contents

Heres some programming tid bits that really didn’t deserve their own post, so I grouped them all together into one post! 😛

else-switch

We all know about the common else-if language construct that is available in almost all programming languages, but here one you might of not been aware of: the else-switch statement.
[code lang=”javascript”]
if(false) {

} else switch(number) {
case 0:
//do something
break;
case 1:
//do something else
break;
default:
//do default action
}
[/code]
Actually, you can follow if with any control statement. For instance, you can have an else-return or an else-break statement. This might be old news to some programmers, but I never knew you could do that!

Declare Variables Inside a switch Statement

I never had to do this before, but recently I needed to declare some variables inside a switch statement. A little googling revealed that variable declaration inside a switch statement is possible:
[code lang=”cpp”]
switch(something) {
case 1: {
int a; //a variable is declared!
}
}
[/code]All you have to do is enclose your case statement with brackets and you can define all the local variables you want.

Reset mySQL’s auto_increment

I love mySQL’s auto increment feature, but while developing applications using mySQL I sometimes need to reset the auto_increment counter. The follow code snippet will reset your auto_increment so the next database record id will be 0:
[code lang=”sql”]alter table your_table auto_increment = 0[/code]

Text Conversion Utility

MacDevCenter has a nice article on a ‘hidden’ text conversion utility, textutil thats bundled with the os x dev tools.

Javascript

Something that I’ve always hated about javascript is the lack of reusable libraries and classes. Finally their has been some movement to standardize alot of common javascript tasks, not just to post ad-hoc solutions on the various script sites out there. You can get a comprehensive list of the various libraries available here.

F-Script Anywhere

This is one of the coolest things that I’ve come across for OS X. F-Script anywhere lets you look inside a program while its running. You can call methods of objects, inspect the properties of UI elements. It’s pretty incredible. You’ll need to get the F-Script Framework before you can use the F-Script Anywhere SIMBL plug-in.

Objective-C Instance Variable Initialization

In objective-C their is no need to initialize your instance variables to nil/NULL (you normally should initialize all variables to nil/NULL, or some other initial value for reasons described in this article). This is already done for you in the alloc method of an object.

Carbon Data Types

Its hard to me to understand Carbon code well because of all the ‘opaque’ data types used by Carbon. At first glance what is ItemCount? Is it a int? An unsigned int? A long? Theirs no documentation of these data types in the Apple docs anywhere, so I went looking around to see if i could find the header files that contained the definitions for the various data types. Luckily my search was not in vain:
[code]/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/[/code]
The above directory contains the definitions for most common Carbon data types, the majority of common Carbon data type definitions are located in the following header file:

[code]/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/MacTypes.h[/code]

Initializing All Elements of a C-Array to 0

You can easily initialize all elements of a C array to zero by initializing the first element of the array to 0 using the “bracket initialization” method:
[code lang=”cpp”]
int intArray[1000] = {0};
[/code]
The above code will initialize all 1000 elements of intArray to 0. All elements that aren’t given a initial value using “bracket initialization” are given an initial value of 0.