tag:blogger.com,1999:blog-15227288938394337772024-02-22T12:26:46.835-08:00Some stuff about computersAnonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.comBlogger21125tag:blogger.com,1999:blog-1522728893839433777.post-64262566623150583022014-09-06T10:25:00.001-07:002014-09-06T10:25:50.003-07:00paradigm shift<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Georgia, Times New Roman, serif;">What is a paradigm </span><span style="font-family: Georgia, 'Times New Roman', serif;">shift</span><span style="font-family: Georgia, 'Times New Roman', serif;">?</span><br />
<span style="font-family: Georgia, 'Times New Roman', serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">Paradigm shift is something like a revolution. As Thomas Kuhn describes it in his book </span><b><i><span style="font-family: Courier New, Courier, monospace;">the Structure of Scientific revolutions</span></i></b><span style="font-family: Georgia, Times New Roman, serif;">, it's a change that has been driven by the emergence of an idea which challenges an existing orthodoxy. </span><br />
<span style="font-family: Georgia, Times New Roman, serif;">Initially, the two ideas compete while the old order maintains its dominance. Over time, the strength and popularity of the new idea grows, until it begins to displace the older order: This is what Thomas Kuhn calls a </span><b><span style="font-family: Courier New, Courier, monospace;"><i>paradigm shift</i></span></b><span style="font-family: Georgia, Times New Roman, serif;">.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">That's how all great ideas come into existence. It's not easy to think of something that can cause a revolution, something that can change history as well as existing patterns of thoughts. People are usually resistant to change, a new idea is not necessarily well accepted most of the time. Thus, when thinking of a new revolutionary idea, one must have enough resolve and keep on holding onto their own ideas. We have to think different, think outside the box, in order to be able to change the world. And as a programmer, it's not enough to have an idea, we must make the effort to implement this idea, turn it into something concrete.</span></div>
Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-45224562069043686852014-09-06T09:43:00.001-07:002014-09-06T09:44:21.467-07:00How did fortran (and modern programming languages) come into existence<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Georgia, Times New Roman, serif;">Excerpt from stanford C++ course (cs106b)</span><br />
<span style="font-family: Verdana, sans-serif; font-size: 12pt;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: 12pt;">In the early days of computing, programs were written in </span><span style="font-family: Verdana, sans-serif; font-size: 12pt; font-weight: 700;">machine language, </span><span style="font-family: Verdana, sans-serif; font-size: 12pt;">which
consists of the primitive instructions that can be executed directly by the machine. </span><br />
<span style="font-family: Verdana, sans-serif; font-size: 12pt;">Machine-language programs are difficult to understand, mostly because the structure of
machine language reflects the design of the hardware rather than the needs of
programmers. In the mid-1950s, a group of programmers under the direction of John
Backus at IBM had an idea that profoundly changed the nature of computing. Would it
be possible, they wondered, to write programs that resembled the mathematical formulas
they were trying to compute and have the computer itself translate those formulas into
machine language? In 1955, this team produced the initial version of Fortran (whose
name is an abbreviation of </span><span style="font-family: Verdana, sans-serif; font-size: 12pt; font-style: italic;">formula translation</span><span style="font-family: Verdana, sans-serif; font-size: 12pt;">), which was the first example of a </span><span style="font-family: Verdana, sans-serif; font-size: 12pt; font-weight: 700;">higher-
level programming language. </span><br />
<span style="font-family: Verdana, sans-serif; font-size: 12pt; font-weight: 700;"><br /></span>
<span style="font-family: Georgia, 'Times New Roman', serif;">I guess that this bright idea was what brought the modern programming languages into existence. After all, it's possible to express any mathematical formula with an instruction or two in any programming language. Programming requires logical thinking, which is something mathematicians are good at. It might not be always the case, but I think a good mathematician can be a good programmer. However, a good programmer is not necessarily good in mathematics.</span><br />
<div class="page" title="Page 2">
<div class="layoutArea">
<div class="column">
<br /></div>
</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-71494554700438038622014-08-30T02:39:00.002-07:002014-08-30T02:40:17.286-07:00Beware of command substitution in bash<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Georgia, Times New Roman, serif;">It's dangerous to use a loop with ls results in your bash scripts:</span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">for i in $(ls)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">Unix/Linux allows for almost any character in file names, including new lines and spaces. Then, what happens when there's a file with a space in its name:</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="color: #4c2f2d; font-family: Courier; font-size: 12px;">╭─</span><span style="color: #34bd26; font-family: Courier; font-size: 12px;"><b>mansuro@localhost</b></span><span style="color: #4c2f2d; font-family: Courier; font-size: 12px;"> </span><b style="color: #5330e1; font-family: Courier; font-size: 12px;"> ~/workspace/new_dir</b><span style="color: #4c2f2d; font-family: Courier; font-size: 12px;"> </span><br />
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
╰─<span style="color: #943a20;"><b>$</b></span> <span style="color: #34bd26;">touch</span> some\ file</div>
<div style="color: #5330e1; font-family: Courier; font-size: 12px;">
<span style="color: #4c2f2d;">╭─</span><span style="color: #34bd26;"><b>mansuro@localhost</b></span><span style="color: #4c2f2d;"> </span><b> ~/workspace/new_dir</b><span style="color: #4c2f2d;"> </span></div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
╰─<span style="color: #943a20;"><b>$</b></span> <span style="color: #34bd26;">ls</span></div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
some file</div>
<div style="color: #5330e1; font-family: Courier; font-size: 12px;">
<span style="color: #4c2f2d;">╭─</span><span style="color: #34bd26;"><b>mansuro@localhost</b></span><span style="color: #4c2f2d;"> </span><b> ~/workspace/new_dir</b><span style="color: #4c2f2d;"> </span></div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
╰─<span style="color: #943a20;"><b>$</b></span> <span style="color: #c33720;"><b>(</b></span>for i in $(ls); <span style="color: #afad24;">do</span> echo $i; <span style="color: #afad24;">done</span><span style="color: #afad24; text-decoration: underline;">)</span></div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
some</div>
<br />
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
file</div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
<br /></div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
<br /></div>
<span style="font-family: Georgia, Times New Roman, serif;"> As you can see, the file was treated as two different arguments, and that would have undesirable effects in your bash scripts.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">How can we fix this? We could change </span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;">for i in $(ls)</span><br />
<br />
to<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;">for i in *</span><br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<br />
<div style="color: #5330e1; font-family: Courier; font-size: 12px;">
<span style="color: #4c2f2d;">╭─</span><span style="color: #34bd26;"><b>mansuro@localhost</b></span><span style="color: #4c2f2d;"> </span><b> ~/workspace/learning/Terminal/new_dir</b><span style="color: #4c2f2d;"> </span></div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
╰─<span style="color: #943a20;"><b>$</b></span> <span style="color: #c33720;"><b>(</b></span>for i in <span style="color: #5330e1;">*</span>; <span style="color: #afad24;">do</span> echo $i; <span style="color: #afad24;">done</span><span style="color: #afad24; text-decoration: underline;">)</span> </div>
<br />
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
some file</div>
<div style="color: #4c2f2d; font-family: Courier; font-size: 12px;">
<br /></div>
<div style="color: #4c2f2d;">
<span style="font-family: Georgia, Times New Roman, serif;">And you're done, your bash script works the way you want it to.</span></div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-38625556305880653142014-08-01T23:25:00.000-07:002014-08-30T02:03:04.569-07:00Vim persistent undo<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Georgia, Times New Roman, serif;">Starting from Vim 7.3, it's possible to keep your "</span><span style="font-family: Courier New, Courier, monospace;">undo history</span><span style="font-family: Georgia, Times New Roman, serif;">". Even after closing and reopening the file, you can still use </span><span style="font-family: Courier New, Courier, monospace;">u </span><span style="font-family: Georgia, Times New Roman, serif;">and</span><span style="font-family: Courier New, Courier, monospace;"> ctrl+r </span><span style="font-family: Georgia, Times New Roman, serif;">in order to go back to changes you made before closing the file.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">In order to enable this feature, all you need to do is to set the properties </span><span style="font-family: Courier New, Courier, monospace;">undofile</span><span style="font-family: Georgia, Times New Roman, serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">undodir</span><span style="font-family: Georgia, Times New Roman, serif;"> in your</span> .<span style="font-family: Courier New, Courier, monospace;">vimrc </span><span style="font-family: Georgia, Times New Roman, serif;">file</span><br />
<pre>set undofile
set undodir=$HOME/.undodir
</pre>
</div>
<br />
<span style="font-family: Georgia, Times New Roman, serif;">Don't forget to create the directory undodir, or else this won't work</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">You can find more information about this feature here: <a href="http://vimdoc.sourceforge.net/htmldoc/undo.html#undo-persistence">Vim documentation: undo</a></span><br />
<br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-63321072754301081772013-01-21T04:41:00.004-08:002013-01-21T04:41:58.904-08:00The expr command<div dir="ltr" style="text-align: left;" trbidi="on">
The expr command is used to evaluate an expression and output the corresponding value. It could be used for example to use the terminal as a calculator:<br />
<br />
<span style="font-family: "Courier New", Courier, monospace;"> $ expr 80 - 25</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> 55</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> $ expr 80 / 25</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> 3</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> </span><br />
Note that there is a space between the different arguments, otherwise the expression might not be evaluated properly.<br />
It can be used as well for string operations and boolean operations<br />
<br />
<span style="font-family: "Courier New", Courier, monospace;"> $ expr length "abc"</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> 3</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> $ expr 1 "&" 0</span><br />
<span style="font-family: "Courier New", Courier, monospace;"> 0</span></div>
Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com2tag:blogger.com,1999:blog-1522728893839433777.post-33638710494678712082013-01-18T04:05:00.003-08:002013-01-18T04:11:45.473-08:00Working with multiple files in vim<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Georgia,"Times New Roman",serif;">There is a couple of useful commands that can be used to work with multiple files as tabs. I've mentioned them in a stackoverflow answer:</span><br />
<a href="http://stackoverflow.com/a/14395583/612920"><span style="font-family: Georgia,"Times New Roman",serif;">http://stackoverflow.com/a/14395583/612920</span></a><br />
<br />
<ul style="text-align: left;">
<li><span style="font-family: Georgia,"Times New Roman",serif;">Open a file in another tab</span><span style="font-family: "Courier New", Courier, monospace;"><br /><br /><b>:tabe filepath</b></span></li>
<br />
<li><span style="font-family: Georgia,"Times New Roman",serif;">save the current session in order to be able to open the same set of files again</span><br /><b><br /><span style="font-family: "Courier New", Courier, monospace;">:mksession session_name.vim</span></b></li>
<br />
<li><span style="font-family: Georgia,"Times New Roman",serif;"> navigate between different tabs</span><br /><br /><b><span style="font-family: "Courier New", Courier, monospace;">gt: next tab<br />gT: previous tab</span></b></li>
<br />
<li><span style="font-family: Georgia,"Times New Roman",serif;">close all tabs at once</span><br /><br /><b><span style="font-family: "Courier New", Courier, monospace;">:qa</span></b></li>
<br />
<li><span style="font-family: Georgia,"Times New Roman",serif;">open a saved session</span><br /><br /><b><span style="font-family: "Courier New", Courier, monospace;">vim -S session_name.vim</span></b></li>
</ul>
<br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com1tag:blogger.com,1999:blog-1522728893839433777.post-25832579089077754202012-05-23T06:32:00.001-07:002012-05-23T06:32:29.592-07:00cryptic perl<div dir="ltr" style="text-align: left;" trbidi="on">
<blockquote class="tr_bq">
"Perl is the only language that looks the same before and after RSA encryption." <br />Keith Bostic. Programmer, created Sleepycat, contributed to free BSD unices.</blockquote>
</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-48412735839444240352012-05-23T06:27:00.000-07:002012-05-23T06:27:03.077-07:00Funny C quote<div dir="ltr" style="text-align: left;" trbidi="on">
<blockquote class="tr_bq">
"One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs."<br />Robert Firth. Author of programming books.</blockquote>
</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-27549078886011696252012-05-21T03:47:00.001-07:002013-01-18T04:11:07.078-08:00one of a million reasons to love programming<div dir="ltr" style="text-align: left;" trbidi="on">
From the introduction to the excellent free book <a href="http://ftp.cc.uoc.gr/mirrors/nongnu.org/pgubook/ProgrammingGroundUp-1-0-booksize.pdf">Programming ground up</a>, <br />
<blockquote class="tr_bq">
<br />
I love programming. I enjoy the challenge to not only make a working program, but to do so with style. Programming is like poetry. It conveys a message, not only to the computer, but to those who modify and use your program. With a program, you build your own world with your own rules. You create your world according to your conception of both the problem and the solution. Masterful programmers create worlds with programs that are clear and succinct, much like a poem or essay.</blockquote>
</div>
Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-45786766328521144002012-04-06T03:49:00.000-07:002012-04-06T03:55:07.623-07:00Swapping numbers in java, pass-by-value VS pass-by-reference<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
I never thought about it, but I just discovered it is not possible to write a method to swap numbers in Java since Java is strictly pass-by-value, not pass by reference.</div>
<pre class="brush: java">public void swapNumbers(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
</pre>
</div>
If we call this function to try to swap numbers, the numbers won't be changed.<br />
There is an example of swapping numbers in this link: <a href="http://www.roseindia.net/java/beginners/swapping.shtml">http://www.roseindia.net/java/beginners/swapping.shtml</a>, but it's confusing for beginners.<br />
<br />
<code>
<span style="color: #7f0055;"><b>public class </b></span><span style="color: black;">Swapping{</span><br />
<span style="color: white;"> </span><span style="color: #7f0055;"><b>static </b></span><span style="color: #7f0055;"><b>void </b></span><span style="color: black;">swap</span><span style="color: black;">(</span><span style="color: #7f0055;"><b>int </b></span><span style="color: black;">i,int j</span><span style="color: black;">)</span>{<br />
<span style="color: white;"> </span><span style="color: #7f0055;"><b>int </b></span><span style="color: black;">temp=i;</span><br />
<span style="color: white;"> </span><span style="color: black;">i=j;</span><br />
<span style="color: white;"> </span><span style="color: black;">j=temp;</span><br />
<span style="color: white;"> </span><span style="color: black;">System.out.println</span><span style="color: black;">("</span><span style="color: blue;">After
swapping</span><span style="color: black;"> </span><span style="color: #2a00ff;">i = " </span><span style="color: black;">+ i + </span>"<span style="color: #2a00ff;"> j = </span>"<span style="color: #2a00ff;"> </span><span style="color: black;">+ j</span><span style="color: black;">)</span><span style="color: black;">;</span><br />
<span style="color: white;"> </span><span style="color: black;">}</span><br />
<span style="color: white;"> </span><span style="color: #7f0055;"><b>public static </b></span><span style="color: #7f0055;"><b>void </b></span><span style="color: black;">main</span><span style="color: black;">(</span><span style="color: black;">String</span><span style="color: black;">[] </span><span style="color: black;">args</span><span style="color: black;">){</span><br />
<span style="color: white;"> </span><span style="color: #7f0055;"><b>int </b></span><span style="color: black;">i=</span><span style="color: #990000;">1</span><span style="color: black;">;</span><br />
<span style="color: white;"> </span><span style="color: #7f0055;"><b>int </b></span><span style="color: black;">j=</span><span style="color: #990000;">2</span><span style="color: black;">;</span><br />
<span style="color: white;"><br />
</span><span style="color: white;"> </span><span style="color: black;">System.out.prinln("</span><span style="color: blue;">Before swapping
i=</span><span style="color: black;">"+i+" </span><span style="color: blue;"> j=</span><span style="color: black;">"+j);</span><span style="color: white;"><br />
</span><span style="color: black;">swap</span><span style="color: black;">(</span><span style="color: black;">i,j</span><span style="color: black;">)</span><span style="color: black;">;</span><br />
<span style="color: white;"><br />
</span><span style="color: black;">}</span><br />
<span style="color: black;">}</span></code><br />
<div style="font-family: Times,"Times New Roman",serif;">
<br /></div>
<span style="color: black;">This example doesn't mention that after we call the method swap, the numbers will return to their original values after this call.</span><br />
The same operation is really simple in C++ which allows us to pass values by reference<br />
<br />
<pre class="brush: c">void swapNumbers(double& a, double& b)
{
double temp = a;
a = b;
b = temp;
}
</pre>
<pre class="brush: c"> </pre>
Now, when we call this function, a and b will be swapped.*<br />
<br />
<pre class="brush: c">double number1(5.5), number2(7.0)
swapNumbers(number1, number2);
cout << "First number : << number1 << endl;
cout << " Second number :" << number2 << endl;</pre>
<br />
The output will be<br />
First number : 7.0<br />
Second number 5.5; <br />
<br />
A reference is merely an alias, another name for the same variable. Hence, making operations on a variable will also change the variable value.</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-34732507038587401632012-04-06T01:48:00.002-07:002012-04-06T01:48:57.644-07:00real life example for passing argument by reference in java<div dir="ltr" style="text-align: left;" trbidi="on">
I read this in one of the comments for this answer in SO <a href="http://stackoverflow.com/a/40523/612920">http://stackoverflow.com/a/40523/612920</a><br />
<br />
<blockquote class="tr_bq">
<span class="comment-copy">My attempt at a good way to visualize object
passing: Imagine a balloon. <br />Calling a function is like tieing a second string
to the balloon and handing the line to the function. <br />parameter = new
Balloon(); will cut that string and create a new balloon (but this has no
effect on the original balloon). <br />parameter.pop(); will still pop it
though because it follows the string to the same, original balloon. Java
is pass by value, but the value passed is not deep, it is at the
highest level, i.e. a primitive or a pointer. Don't confuse that with a
deep pass-by-value where the object is entirely cloned and passed.</span></blockquote>
</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-82476065124140280632012-04-04T01:43:00.000-07:002012-04-04T01:46:18.418-07:00Everyone should learn Vi / VIm<div dir="ltr" style="text-align: left;" trbidi="on">
I always wanted to learn vi. It seems that once you get to know it well, you can be a lot more productive.<br />
The problem is you got to invest some time to learn all the shortcuts that will help you to be productive and practice them often to get used to them. A good start is the vimtutor which is an interactive learning resource that can be started by typing vimtutor in the shell. Once you finish the tutorial, you can find a ton of resources on the internet. Personally, I'm starting with vim tips <a href="http://vim.wikia.com/wiki/Vim_Tips_Wiki">http://vim.wikia.com/wiki/Vim_Tips_Wiki</a>. There is a lot of good tips, like autocompletion feature which can be done using Ctrl+N for next word and Ctrl+p for previous word.<br />
If I can be as good in vi as this guy <a href="http://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim">http://stackoverflow.com/a/1220118/612920</a> , I'm sure I'll be at least 10 times faster. If you decide as well to start learning vim, I'm sure you are making a good decision.</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-15155804995598336162012-01-20T02:40:00.000-08:002012-01-20T08:19:33.026-08:00Getting started with TDD - Test Driven Development<div dir="ltr" style="text-align: left;" trbidi="on">
Update: Here is the GitHub repository containing a FlashBuilder project containing the final test:<br />
<a href="https://github.com/Mansuro/Flex-HashMap">Flex-HashMap</a><br />
<br />
I am just familiar with some basic concepts of unit testing. I didn't have the chance to work in a professional environment that would allow me to write unit tests for complicated business code, so all I've tried is really simple examples like the famous addition example. In this article, I'm trying to get a little bit more in depth by using TDD (Test Driven Development).<br />
Test Driven Development can be defined as reversing the traditional development life-cycle upside down. Instead of writing the tests after our code is ready to be tested, the first thing we do is to write tests even before we have any code to test. The code is written for the sole purpose of making tests pass. So the development life-cycle is as follows:<br />
- add a test<br />
- make sure that the test fails<br />
- write the simplest code to make the test pass<br />
- refractor<br />
- do it again<br />
as simple as that. The benefits for using TDD come from the fact that we are repeating very short development life-cycles, and as a result we end up having a high quality and error prone code. I'm implementing the unit tests for a HashMap class implemented in AS3 By Eric J. Feminella. You can find the final code here :<br />
<a href="http://code.ericfeminella.com/classes/as3/HashMap.as.html">Eric J. Feminella HashMap Implementation</a>. Though, I will be making some changes to it later, I'll put the final code on github.<br />
Of course, in TDD, we have to begin by writing the tests, so before implementing that class, we need to implement a test class:<br />
<br />
<pre class="brush: javascript">package tn.zuro.tests.collections
{
public class HashMapTest
{
private var map:IMap;
[Before]
public function setUp():void
{
map = new HashMap();
}
[After]
public function tearDown():void
{
}
}
}</pre>
<br />
Here is our first failing test. A test that doesn't compile can be considered as a failing test. Our test won't compile here since the compiler won't recognize neither IMap or HashMap as we have not implemented them yet. Now let's write some code to make this test pass or at least compile. <br />
<br />
<pre class="brush: java">package tn.zuro.collections
{
public interface IMap
{
}
}
package tn.zuro.collections
{
public interface HashMap
{
}
}
</pre>
Now our code will compile, but it still fails: we haven't implemented any runnable methods yet. Let's start by adding some basic methods. The HashMap in our case will use flash.utils.Dictionary since it allows us to implement key/value pairs. we could start by implementing a test method for the method put:<br />
<pre class="brush: javascript">[Test]
public function testPut():void
{
map.put("d", "value A");
map.put("e", "value B");
map.put("f", "value C");
map.put("u", "value X");
map.put("v", "value Y");
map.put("w", "value Z");
}
</pre>
Now if we run the test, it will fail, the method is not implemented. We go back to IMap, and we add the put method declaration:<br />
<pre class="brush: javascript">function put(key:*, value:*) : void;</pre>
<br />
Now we need to implement the simplest possible code to make the test pass:<br />
<pre class="brush: javascript">public function put(key:*, value:*) : void
{
_map[key] = value;
}
</pre>
Finally, we have a test that passes without problems. Our first iteration is complete, now we have to repeat. We will test the size method next, so that we can add some assertions to our code:<br />
<pre class="brush: javascript">[Test]
public function testSize():void
{
assertEquals(map.size(), 6);
map.put("d", "value D");
assertEquals(map.size(), 7);
map.put("a", "value K");
assertEquals(map.size(), 7);
}
</pre>
We can also add some code to the setUp method, so that we have some key/value pairs in our hashmap to play with.<br />
<pre class="brush: javascript">private var map:IMap;
[Before]
public function setUp():void
{
map = new HashMap();
map.put("a", "value A");
map.put("b", "value B");
map.put("c", "value C");
map.put("x", "value X");
map.put("y", "value Y");
map.put("z", "value Z");
}
</pre>
The test fails again, since the method size doesn't exist, yet. Thus, we declare it in the interface and implement it in the HashMap class:<br />
<pre class="brush: javascript">function size() : int;
public function size() : int
{
var length:int = 0;
for ( var key:* in _map )
{
length++;
}
return length;
}
</pre>
The loop <br />
<pre class="brush: javascript">for (var key:* in _map)</pre>
allows us to iterate over the keys of the dictionary, to iterate over the values, we can use a for each loop. Now our test passes again, and we can see the results of our assertions. In the method testSize, we have the following assertions:<br />
<pre class="brush: javascript">assertEquals(map.size(), 6);</pre>
which should return true since we have put 6 values in the setUp method. We are also sure that the put method works the way it should be.<br />
<pre class="brush: javascript">assertEquals(map.size(), 7);</pre>
returns true for the same reasons<br />
and then:<br />
<pre class="brush: javascript"> map.put("a", "value K");
assertEquals(map.size(), 7);</pre>
this returns true as well, because we used the same key again, so we have <br />
<pre class="brush: javascript">_map["a"] = "value K";</pre>
which updates the value associated with the key "a".<br />
<br />
Let's repeat one more time to make sure we got the process right, and to understand how useful tests can be.<br />
I'm going to add a test for the clear method which will remove all the elements from the dictionary. The clear method will use another method which will remove a key/value pair from the dictionary. We should start by adding a testRemove method to have a failing test, make the test pass, and repeat again. We skipped the refractoring step here, cause the code is too simple and does not require any refractoring. Then we repeat again. Then we write a test for the clear method. <br />
<pre class="brush: javascript">[Test]
public function testRemove():void
{
map.remove("a");
assertEquals(map.size(), 5);
}
[Test]
public function testClear():void
{
map.clear();
assertEquals(map.size(), 0);
}
</pre>
IMap.as <br />
<pre class="brush: javascript">function remove(key:*) : void;
function clear() : void;
</pre>
HashMap.as<br />
<pre class="brush: javascript">public function remove(key:*) : void
{
_map[ key ] = undefined;
delete _map[ key ];
}
public function clear() : void
{
for ( var key:* in _map )
{
remove( key );
}
}
</pre>
Now that all the code of the two iterations is in place, everything should be working smoothly, right? Well, not quite. The test method testClear is going to fail. This means that there is a problem with the clear function. The problem comes from the first instruction in the remove function. <br />
<pre class="brush: javascript">_map[key] = undefined;</pre>
I managed to find the problem after I asked a question in StackOverflow: <a href="http://stackoverflow.com/q/8929128/612920">Flex dictionary question</a>. If we didn't test this code thouroghly, it's possible we wouldn't notice this error. And using TDD, we noticed it very early in the development lifecycle, so that it could be corrected very early, and now we have less bugs to worry about. The remove function should look like this:<br />
<pre class="brush: javascript">public function remove(key:*) : void
{
delete _map[ key ];
}
</pre>
And this time, the tests pass. I guess that's all what's TDD is about. It's not complicated. It just takes a little time to get used to it, and once we know it good enough, it can speed up development significantly.<br />
<br /></div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-68012566379362548872011-12-18T05:43:00.000-08:002011-12-18T06:10:27.064-08:00Getting started with the javadoc<div dir="ltr" style="text-align: left;" trbidi="on">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYzmv25_etTQ4aafaiC05VF2RC7Nmm7OH3_u-ZdUBMdyeqZn5C6tTZW5LAt76foDxWPqAwfFZUOaYoCKArS1SBM-08kltGYnxnhU2hYwtdNXvjfJe4EcSP73_D0AeG-y2EaFZToBdS00k/s1600/Screen+shot+2011-12-18+at+2.15.09+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"></a>If you are using eclipse, working with javadoc is pretty easy. It's not that complicated using the command line either, personally I'm an addict to the command line, but I didn't bother too much with generating the javadoc using it since I tried already and I failed miserably. I had a problem when trying to set the classpath, since there was a lot of dependencies to include. I should have written an ant script, but I was too lazy. All programmers are :). So, I just chose the easy way for once, selecting project > generate javadoc... You have then to go through the usual straightforward process, choosing some options and then next, next. <br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYzmv25_etTQ4aafaiC05VF2RC7Nmm7OH3_u-ZdUBMdyeqZn5C6tTZW5LAt76foDxWPqAwfFZUOaYoCKArS1SBM-08kltGYnxnhU2hYwtdNXvjfJe4EcSP73_D0AeG-y2EaFZToBdS00k/s1600/Screen+shot+2011-12-18+at+2.15.09+PM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="381" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYzmv25_etTQ4aafaiC05VF2RC7Nmm7OH3_u-ZdUBMdyeqZn5C6tTZW5LAt76foDxWPqAwfFZUOaYoCKArS1SBM-08kltGYnxnhU2hYwtdNXvjfJe4EcSP73_D0AeG-y2EaFZToBdS00k/s400/Screen+shot+2011-12-18+at+2.15.09+PM.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">java heap space configuration</td><td class="tr-caption" style="text-align: center;"><br /></td><td class="tr-caption" style="text-align: center;"><br /></td><td class="tr-caption" style="text-align: center;"><br /></td></tr>
</tbody></table>
And don't forget to configure the JVM heap space, especially when you have a big project with a lot of comments in it. This way, the javadoc will be generated a lot faster, and you won't be at the risk of having an error with heap space.<br />
My advice is, don't forget to document well you code. A good code is a self documenting code. Compare:<br />
<br />
<pre class="brush:java">int x;
String y;</pre>
<br />
and<br />
<pre class="brush:java">
int hitPoints;
String playerName</pre>
<br />
A lot better, isn't it?<br />
And if you have a self documenting code like the last one, when you put comments to explain it, if you are not going to add some value to the explanation, it's better to leave it as it is.<br />
<br />
<br />
<pre class="brush:java">
/** calcualte the number of the likes */
public int calculateNumberOfLikes()</pre>
<br />
That's what I would call a waste of time. Who'd do that anyway, well, more people than you think, not all people have learnt to program the way programming should really be.<br />
<br />
<br />
<pre class="brush:java">
/** This function calculates the number of the likes of a friend.
*
* This function calls an FQL query which determines the pages a friend likes,
* and calculates the number of times he liked an item in that page
*
* @author someone
*/
public int calculateNumberOfLikes()</pre>
<br />
This is just a little example to show the forms a javadoc comment has. The first sentece is a summary of what the function does. Then a more detailed description, then some javadoc-specific tags about the purpose of the function, we can add html tags to this description in order to format it the way we want. There is a lot of tags to know about, which can give us a lot of information about a function, a class or a variable. So, it's good to have the habit of well documenting our code, cause we are not the only ones that work in our code, and we can make others win a lot of time by making it easier for them to understand our code. And in case of a solo programmer working on his hobby project, you'll eventually end up coming back to the code you wrote after some months, and then, there is no telling how much of your code you will remember and how much time you will waste because of a poor documented code.</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-90154900722721489992011-11-10T04:11:00.000-08:002011-12-18T06:12:26.692-08:00Let's talk about finalize in java<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
So, what is finalize, how do we use it?<br />
Well, the answer is simple, we don't. To be more precise, most of the time we don't.<br />
The finalize method gets called when an object is garbage collected to clean any mess caused by that object. All java object have that method, since it is implemented on the Object class. And as you are already aware of, it's the mother class of all other java classes. If the object which was just garbage collected, was using system resources, the finalize method will take care of releasing them. An example of these resources could be an open file. So be careful when you try to use it, it won't clean your java objects, it will only take care of non-java resources.<br />
<br />
The finalize method shouldn't be used since it's not a very reliable method, since there is no guarantee that the object will be garbage collected during the lifetime of an application, in addition to the fact that it takes care only of non-java resources. But if you want to use it, here's how to proceed.<br />
<br />
You have to declare your method this way:<br />
<br /></div>
<pre class="brush:java">protected void finalize () throws throwable</pre>
</div>
Now, inside the method:<br />
<br />
Since the method is error-prone, it should contain the famous try-catch-finally statement<br />
<br />
<br /></div>
<pre class="brush:java">try
{
//you do your personalized cleaning operations here
} finally {
//then you have to call the parent finalize method
super.finalize()
}</pre>
<pre class="brush:java"> </pre>
Now you have your finalize() method ready for action, just don't forget, don't rely on it too much, because maybe it will be called, and maybe it won't.</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-78202547438671236042011-10-26T03:32:00.001-07:002011-10-26T03:32:59.263-07:00AI contest<div dir="ltr" style="text-align: left;" trbidi="on">
If you are interested in game programming and AI, you might want to take a look at the <a href="http://aichallenge.org/">AI ants challenge</a>. It is a challenge sponsored by google, and this is the presentation of the contest by the official website :<br />
<blockquote class="tr_bq">
The AI Challenge is all about creating artificial intelligence,
whether you are a beginning programmer or an expert. </blockquote>
All you have to do is pick your favourite programming language. There is already about 20 of the most popular languages, and the contest organizers are willing to add another ones if they receive requests on the forum. Once you know what programming language you are going to use, you can download the tools that will help you in your quest, and then download the starter kit for that language. The game engine is written in python, so you'll need to install python on your computer. Don't worry if you don't know any python at all, you don't need to. As for the installation of python, it's pretty straightforward. If you are on a Linux machine, it should be installed already. If you are on windows, you can <a href="http://www.python.org/download/">download</a> a windows installer which will do the work for you, or you can use cygwin.<br />
<br />
You should participate regardless of your level. You could get a good rank in the leader board, which will look good on your resume, but even if you don't, it was a good learning opportunity, and you'll have some prerequisites for future contests.<br />
<br />
Good luck everyone!</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-12834715233676227832011-10-21T02:58:00.000-07:002011-10-21T02:58:27.033-07:00Creating an object in actionscript<div dir="ltr" style="text-align: left;" trbidi="on">
The creation of a visual component is an operation that requires many steps.<br />
First, we start by constructing the component, either using the default constructor or any constructor you have defined on your own<br />
<br />
<pre class="brush: javascript">var imDaButton:Button = new Button();
</pre>
Now the component needs some properties, "hey, I'm like all the other buttons out there, give me some properties so that I feel a little bit different", said imDaButton<br />
Here you go<br />
<br />
<pre class="brush: javascript">imDaButton.label="warrior";
imDaButton.setStyle("cornerRadius", 2);
imDaButton.addEventListener(MouseEvent.CLICK, doStuff);
</pre>
<br />
<br />
Ok, now our button is happy, it has some properties of its own. A label to be proud of, some weird corners, and it does stuff when it's clicked.<br />
<br />
Once you've got your button all ready to hit the road, you can add it to a parent.<br />
<br />
<pre class="brush: javascript">
someParent.addChild(imDaButton); </pre>
<br />
The method <span style="font-size: x-small;"><span style="font-family: "Courier New",Courier,monospace;">initialize()</span></span> is called automatically when we call <span style="font-size: x-small;"><span style="font-family: "Courier New",Courier,monospace;">addChild()</span></span>. This method is used to initialize the internal structure of the component.<br />
It first dispatches a <span style="font-size: x-small;"><span style="font-family: "Courier New",Courier,monospace;">preinitialize </span></span>event which could be used for example to load some external resources, like loading styles dynamically or loading resource bundles. Be careful when you use a listener for this event in your container and try to access children for the container. For example, if you have:<br />
<br />
<pre class="brush: javascript">
someParent.addEventListener(FlexEvent.PREINITIALIZE, parentPre);
private function parentPre(event:FlexEvent):void
{
imDaButton.enabled=false;
}
</pre>
<br />
In this case you'll have an error <span style="color: red;">#1009 null has no properties</span> since imDaButton wasn't created yet. So, when is it created?<br />
<br />
That's simple, the method <span style="font-size: x-small;"><span style="font-family: "Courier New",Courier,monospace;">createChildren()</span></span> is going to do that for you. That's where the component internal structure is created. It could be the class managing the button's label, which is <span id="pageFilter"><a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/core/UITextField.html">UITextField</a> in this case. This method can be overriden in custom components so that you create the children you want, and place them where you want once this method gets called</span><br />
<span id="pageFilter"><br /></span><br />
<span id="pageFilter">Now that your button was created, along with any other component in your container, the initialize event is dispatched, and you can now play with the children of your container using the listener to this event. </span><br />
<span id="pageFilter"><br /></span><br />
<span id="pageFilter">After that, creationComplete event is dispatched to notify us that the children, and their children were created. Note that many other steps happen during the creation of the internal structure of a component: </span><span id="pageFilter">
property processing, measuring, layout, and drawing.
</span><br />
<br />
<br /></div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-72301919209235386202011-10-16T02:15:00.000-07:002011-10-16T02:18:50.704-07:00Pointers<div dir="ltr" style="text-align: left;" trbidi="on">
Pointers is such a difficult concept for beginners. Back when I started learning C, I was never able to understand them, not until I've read countless tutorials about them. So I'll try to explain them for anyone who will face the same issues as me. You already know what variables are and what we use them for. Let's take the example of the city you live in. It's a mixture of houses, and different kinds of buildings. Our variables here are the buildings.<br />
<br />
<pre class="brush: c">int my_var;
</pre>
<br />
Every building has an address. Likewise, every variable has an address in memory.<br />
<br />
<pre class="brush: c">/** we can access the address of a variable in memory using &my_var
to print this value, we have to use %p in printf to print an address*/
printf("the variable my_var has the address: %p ", &my_var);
</pre>
<br />
So what's a pointer, a pointer is also a variable. A variable which contains the address in memory of another variable. How do we illustrate this concept in our city. Suppose you want to send a postcard to your friend. In order for that postcard to reach its destination, you have to write the address of your friend. So a pointer in real life is that postcard. A pointer could be also, a direction sign pointing to your city. So when someone needs to get to your city, he has to "ask" that pointer. In C, some functions, the functions that take a pointer as parameter need to "ask" the pointer to know the value of the variable they point to.<br />
<br />
<pre class="brush: c">/** declaring a pointer is pretty straightforward, you just give him the type of the variable he needs to point to, and the variable he points to */
int my_var = 7;
int *pointer_to_my_var = &my_var;
printf("the value of my_var is: %d", *pointer_to_my_var);
printf("the address of my_var is : %p", pointer_to_my_var);
</pre>
<br />
<br />
To access the variable my_var using the pointer we have to use *, which is also called the indirection operator. Therefore, you should be careful, as you can see, it's not *pointer_to_my_var that contains the address of my_var, but pointer_to_my_var. So the following declaration is equivalent to the previous one.<br />
<pre class="brush: c">int my_var = 7;
int *pointer_to_my_var ;
pointer_to_my_var = &my_var ;</pre>
<br />
<br />
Ok, so what about functions that take as parameter a pointer.<br />
<pre class="brush: c">int add(int * x, int * y) {
return *x + *y;
}
//to call that function we have two methods
int x=7;
int y=9;
//we use the address of the variable directly
add(&x, &y);
// or declare a pointer to x and to y and pass them to the function
int *pointer_to_x=&x;
int *pointer_to_y = &y;
add(pointer_to_x, pointer_to_y);
</pre>
<br />
<br />
That's what I called earlier asking the pointer to know the value of the variable it points to, it's like the postman read the address on your postcard so that he could deliver the postcard to your friend.</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-4736069742750843702011-10-14T03:05:00.000-07:002011-10-14T03:05:51.832-07:00Thoughts about programmingIMHO, programming is not just about writing code, correcting bugs, learning a couple of programming languages, or knowing how to do some tricks with them. Programming is about solving problems, or even better, it's about the approach taken to solve a problem. Sometimes there is only one way to do things. But most of the time, there are many ways. What makes the difference between an average programmer and a good one is that the latter will always find a better way to solve the problem. <br />
<br />
It's not that the first one is not able to find that solution, it's just that when he found the first solution that came to his mind, he didn't take the time to think further about the problem, he didn't ask himself the questions you should ask yourself: Is this the only way to do it? Isn't there a better way? Why don't I try to solve this problem in more elegant way. You have to challenge yourself, try to be better than yourself.<br />
<br />
That's what makes the difference. There are average programmers who remain average either because they don't want to go that extra mile and try to find that better solution, or because someone taught them in the beginning some way to think, which was not the best way and they didn't have the chance to learn another way. As for the rest, those who aspire to become always better, stop writing code for a second, take the time to think deeply, try to read some code written by professional people and ask yourself why did they do it this way not that way. And as long as you are passionate about programming and you keep learning new things, you'll end up being the one finding the best solution.Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-7301656198460609562011-09-11T14:11:00.000-07:002011-09-11T14:11:43.962-07:00Tomcat 7 managerI've installed tomcat so many times before, but when I did it this time, I had some trouble finding how to run the manager app. If you go to the manager documentation, they are going to tell you add the role manager-script. I added that role, and I'm still having the 403 error, I was wondering, what the hell is going on, after so many retries, I've made this search on google, tomcat 7 manager roles, I found <a href="http://tomcat.apache.org/migration.html#Manager_application">this result</a>. Well, it's true that apache has a good documentation, and that these new roles are a good way to separate different tasks, but why would they put this under migration guide instead of the manager how-to.Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0tag:blogger.com,1999:blog-1522728893839433777.post-16945324726057706822011-03-14T13:48:00.000-07:002011-07-20T01:20:23.765-07:00Les victimes de Pwn2Own<div dir="ltr" style="text-align: left;" trbidi="on">Bonjour tout le monde,<br />
<br />
Le fameux challenge de piratage a eu beaucoup de victimes cette année, ce qui est toujours le cas, on trouve dans la liste,le BlackBerry, l'iphone, le mac qui a été pénétré en 5 secondes, bah ouais, 5 secondes, c'est même inférieur au temps que ça prend de taper "le mac a été piraté en 5 secondes". En effet, celui qui l'a piraté a juste exploité des vulnérabilités dans le browser safari. Un autre browser a été exploité dès le premier jour, vous l'aurez tous deviné, c'est IE8 bien évidemment. Google et Firefox n'ont pas été touchés parce que personne n'a accepté le challenge, personne n'a donc eu le prix de 20.000 dollars proposé par Google pour l'équipe de recherche qui réussira à pirater son navigateur, mais ça ne prouve pas que ces navigateurs n'ont pas de faiblesses, bien au contraire.<br />
<br />
Le concours Pwn2Own est organisé par HP TippingPoint et elle offre aux chercheurs des sommes d'argents pour la découverte des vulnérabilités dans un les navigateurs et les plateformes mobiles. Ensuite, ces points faibles sont fournis aux vendeurs associés afin qu'ils effectuent des corrections dans leurs produits.<br />
<br />
Ce qu'il faut retenir surtout, c'est qu'il n'y a pas le concept de sécurité absolue dans le monde de l'informatique, il est possible de tout pirater, il suffit juste de posséder les outils nécessaires.Vous pouvez toujours utiliser chrome ou firefox, mais ça n'empêchera pas quelqu'un qui veut vous pirater d'accéder à vos données, alors prenez toutes les mesures nécessaires de sécurité, comme l'installation des dernières mises à jour pour le système d'exploitation, les différents logiciels et surtout votre antivirus.<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>Anonymoushttp://www.blogger.com/profile/04010109121530888382noreply@blogger.com0