tag:blogger.com,1999:blog-91948033836937255312024-03-13T22:17:41.017+02:00Sergey KovalyovSoftware DevelopmentAnonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-9194803383693725531.post-77133266301468675672018-02-21T19:11:00.005+02:002018-02-21T19:11:59.960+02:00<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: white; color: #14171a; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px; white-space: pre-wrap;">One of the most annoying programming mistakes I face on a regular basis is when people use boolean, where they should use enum instead. The rule is simple: if two possible values is just coincidence, it must be enum.</span><br />
<span style="background-color: white; color: #14171a; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px; white-space: pre-wrap;"><br /></span>
<span style="background-color: #f5f8fa; color: #14171a; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px; white-space: pre-wrap;">Might be not the best example, but GET /thumbnail?size=large instead of GET /thumbnail?isLarge=true
</span><span style="background-color: #f5f8fa; color: #14171a; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px; white-space: pre-wrap;">Or public gender: Gender instead of public isMale: boolean</span><span style="background-color: #f5f8fa; color: #14171a; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px; white-space: pre-wrap;">
</span></div>
Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com1tag:blogger.com,1999:blog-9194803383693725531.post-8525224982464721522013-01-23T16:27:00.000+02:002013-01-23T16:27:02.270+02:00Recursive remove directory for Node.js<p>It turned out that Node.js fs module does not have a method to remove the directory and its content recursively. Instead you should go through the directory structure and remove atomic items, i.e. individual files and empty directories. So I found a nice gist by Takuo Kihira at <a href="https://gist.github.com/2367067">https://gist.github.com/2367067</a> made in JavaScript and decided to make a CoffeeScript version of it:</p>
<script src="https://gist.github.com/4605986.js"></script>
<p>But in real life you should better use the system command:
<p><i>child_process.exec "rm -rf #{directory}"</i></p>
<p>This might be much faster at the cost of platform dependency.</p>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com2tag:blogger.com,1999:blog-9194803383693725531.post-35722076363068314492012-10-22T18:19:00.000+02:002012-10-22T18:20:29.537+02:00Deploy multiple Node applications on one web server in subdomains with Nginx<div dir="ltr" style="text-align: left;" trbidi="on"><p>Earlier I wrote about <a href="http://skovalyov.blogspot.dk/2012/07/deploy-multiple-node-applications-on.html">deploying multiple Node applications on one web server in subfolders with Nginx</a>.</p>
<p>Even though this approach is fully viable, you should not use it unless there are some really important reasons forcing you to go for it.</p>
<p>Given that the application is mounted to a subfolder, you should use relative URLs only in pages. Otherwise the application location must be configured in both Nginx and application itself. Use of relative URLs has a couple of major drawbacks:</p>
<ul>
<li>If you want to move a page within hierarchy, you need to update its content. For example, you have a page that makes an AJAX call to <i>../getValue</i>. Once you move your page one level up or down in the hierarchy, it does not work.</li>
<li>You need to solve a "trailing slash problem". For example, the page http://myhost/demo/pet-project/user-list must be also resolvable as http://myhost/demo/pet-project/user-list/. But if it refers a CSS file as <i>../css/user-list.css</i>, the server will look for it at <i>/opt/demo/pet-project/public/css/user-list.css</i> and <i>/opt/demo/pet-project/public/user-list/css/user-list.css</i> correspondingly. It can be fixed by introducing the concept of canonical URLs and redirecting the client to the "proper" URL with or without trailing slash, depending on the chosen convention.</li>
</ul>
<p>To minimize the maintenance complexity and avoid performance downgrade I decided to deploy Node applications in subdomains. With this approach I can freely use absolute URLs and thus easily shuffle my pages and have valid references without annoying redirects.</p>
<p>Nginx configuration for this setup is very similar to the one with subfolders and even a little bit more simple:<p>
<script src="https://gist.github.com/3932280.js"> </script>
<p>Finally you need to configure a DNS record for pet-project.myhost pointing to your server.</p>
</div>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com2tag:blogger.com,1999:blog-9194803383693725531.post-1865773206409945952012-09-18T15:16:00.001+02:002013-02-04T16:28:16.247+02:00JSON format command line utility<p>Very often I need to format a single-line JSON, so I can read it easily.</p>
<p>Even though modern browser developer tools do it nicely with server responses in preview tab, I want to be able to do something similar with local files.</p>
<p>Node.js has a very good API to work both with JSON and file system. So I created this simple command line utility in CoffeeScript. It accepts the input file name as a 1st parameter and the output file as an optional 2nd parameter. If the output file name is undefined, it writes the result to the console.</p>
<script src="https://gist.github.com/3742840.js?file=format-json.coffee"></script>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com0tag:blogger.com,1999:blog-9194803383693725531.post-31809449573639336092012-07-12T23:52:00.000+02:002012-10-22T18:23:55.310+02:00Deploy multiple Node applications on one web server in subfolders with Nginx<div dir="ltr" style="text-align: left;" trbidi="on">
Things get tricky with Node when you need to move your application from from localhost to the internet.<br />
<br />
There is no common approach for Node deployment.<br />
<br />
Google can find tons of articles on this topic, but I was struggling to find the proper solution for the setup I need.<br />
<br />
Basically, I have a web server and I want Node applications to be mounted to subfolders (i.e. http://myhost/demo/pet-project/) without introducing any configuration dependency to the application code.<br />
<br />
At the same time I want other stuff like blog to run on the same web server.<br />
<br />
Sounds simple huh? Apparently not.<br />
<br />
In many examples on the web Node applications either run on port 80 or proxied by Nginx to the root.<br />
<br />
Even though both approaches are valid for certain use cases, they do not meet my simple yet a little bit exotic criteria.<br />
<br />
That is why I created my own Nginx configuration and here is an extract:<br />
<br />
<script src="https://gist.github.com/3101166.js?file=pet-project.conf">
</script>
From this example you can notice that I mount my <i>Pet Project</i> Node application running on port 3000 to http://myhost/demo/pet-project.<br />
<br />
First Nginx checks if whether the requested resource is a static file available at <i>/opt/demo/pet-project/public/</i> and if so it serves it as is that is highly efficient, so we do not need to have a redundant layer like Connect static middleware.<br />
<br />
Then all other requests are overwritten and proxied to <i>Pet Project</i> Node application, so the Node application does not need to know where it is actually mounted and thus can be moved anywhere purely by configuration.<br />
<br />
<i>proxy_redirect </i>is a must to handle <i>Location </i>header properly. This is extremely important if you use <i>res.redirect()</i> in your Node application.<br />
<br />
You can easily replicate this setup for multiple Node applications running on different ports and add more location handlers for other purposes.
<br />
<br />
UPDATE: <a href="http://skovalyov.blogspot.dk/2012/10/deploy-multiple-node-applications-on.html">Why and how you should do it in subdomains instead</a>.
</div>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com3tag:blogger.com,1999:blog-9194803383693725531.post-84812924549782206512012-06-02T00:36:00.000+02:002013-02-07T16:53:45.408+02:00CoffeeScript version of the time span formatter between now and the date specified in a "Facebook way"<p>Earlier <a href="http://skovalyov.blogspot.dk/2010/09/how-to-format-time-span-between-now-and.html">I published</a> an ActionScript utility method that allows you to format the time span between now and the date specified in a "Facebook way".</p>
<p>Now I port it to CoffeeScript.</p>
<p>You can notice that the code is very similar, but thanks to CoffeeScript native string interpolation it is shorter and easier to read and understand!</p>
<script src="https://gist.github.com/skovalyov/4731388.js"></script>
You are welcome to contribute at <a href="https://github.com/skovalyov/time-format">https://github.com/skovalyov/time-format</a>!Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com0tag:blogger.com,1999:blog-9194803383693725531.post-29871341470940374222012-06-01T01:23:00.000+02:002012-10-11T23:56:22.822+02:00Game Lines made with CoffeeScript and Stylus<div dir="ltr" style="text-align: left;" trbidi="on">
Try my game Lines at <a href="http://skovalyov.github.com/game-lines">http://skovalyov.github.com/game-lines</a>.<br />
<br />
The source code is available at <a href="https://github.com/skovalyov/game-lines">https://github.com/skovalyov/game-lines</a>.<br />
<br />
A lot of improvements to be done, but you can already play!<br />
<br />
I plan to optimize for mobile, make the animation smoother, handle game over properly, introduce different complexity levels and high scores: global, today and probably personal.<br />
<br />
Stay tuned and you are welcome to join!</div>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com0tag:blogger.com,1999:blog-9194803383693725531.post-20798008425665331342010-09-22T19:19:00.008+03:002012-10-11T23:56:58.947+02:00How to format the time span between now and the date specified in a "Facebook way"<p>Recently I was looking for some method to format the time span between now and the date specified in a "Facebook way". There are some, but none was completely what I was looking for. So I have decided to create mine. Moreover, it's not that complicated:</p>
<script src="https://gist.github.com/2873377.js?file=DateUtil.as"></script>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com1tag:blogger.com,1999:blog-9194803383693725531.post-13488866721582629012008-11-06T15:51:00.002+02:002012-10-11T23:57:54.156+02:00Do not comment code!<p>Commenting code is the worst practice promoted by "wannabe smart" books about software engineering. <a href="http://en.wikipedia.org/wiki/Joshua_Bloch">Joshua Block</a> in his <a href="http://www.google.com/search?q=Joshua+Bloch+How+to+Design+a+Good+API+and+Why+it+Matters">How to Design a Good API and Why it Matters keynote</a> notes that <span style="font-style:italic;">code should read like prose</span> and shows the good example:</p>
<script src="https://gist.github.com/2873391.js?file=CarSpeedCheck.java"></script>
<p>And I completely agree with this statement. Thus good code is self-explanatory and does not require comments. On the other hand comments will never rescue bad code. Sure, we can use something like "TODO: I will fix this dummy trick in the next iteration", but no more than it.</p>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com10tag:blogger.com,1999:blog-9194803383693725531.post-5829708300925729992008-05-12T12:09:00.002+03:002012-10-12T00:04:04.784+02:00Second Ukrainian Flash Platform User Group official meetingThis time <a href="http://www.fpug.org.ua/">Ukrainian Flash Platform User Group</a> meeting has been held in Kiev. Two previous meetings have been held in Kharkov. We have gathered at cozy <a href="http://www.goldengatepubkiev.com/">Golden Gate Pub</a> of <a href="http://www.kartamir.com.ua/eng/index.html">Mirovaya Karta</a> chain near Golden Gates. Ilya Panin from Kharkov has compared different IDEs (<a href="http://www.adobe.com/products/flex/">Adobe Flex Builder</a>, <a href="http://fdt.powerflasher.com/">FDT</a>, <a href="http://www.flashdevelop.org/">FlashDevelop</a>, <a href="http://www.jetbrains.com/idea/">IDEA</a>) and summarized that as a whole Adobe Flex Builder is the best choice to develop Flex applications now. Tatyana Belaya from Moscow has told about sound visualization using <a href="http://livedocs.adobe.com/flex/2/langref/flash/media/SoundMixer.html">SoundMixer.computeSpectrum() method</a> with snowflakes on background. Roman Shuper has shared his experience using <a href="http://ant.apache.org/">Apache Ant</a> to deploy Flex projects and also told about conditional compilation and running executables from AIR applications.<br /><br /><a href="http://www.flickr.com/photos/skovalyov/2480974553/" title="Flash Platform User Group by Sergey Kovalyov, on Flickr"><img src="http://farm3.static.flickr.com/2229/2480974553_ec879f2431.jpg" width="500" height="375" alt="Flash Platform User Group" /></a>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com1tag:blogger.com,1999:blog-9194803383693725531.post-20846108374787989352008-04-17T09:19:00.002+02:002012-10-12T00:03:47.798+02:00Flex SDK coding conventions and best practicesAdobe has published <a href="http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions">Flex SDK coding conventions and best practices</a>. Now it is mostly about ActionScript 3.0. I work in a team for a long time, so we have created our own conventions long time ago, because it is a must for collaborative development. It is based on <a href="http://www.adobe.com/devnet/flash/articles/as_bestpractices_04.html">ActionScript 2.0 Best Practices</a>, <a href="http://java.sun.com/docs/codeconv/">Code Conventions for the Java Programming Language</a> and the common sense. Nevertheless, it's a great effort from Adobe, because vendor supported guidelines are always more respected and tend to become industry standard. Now I hope Adobe would also extend the document with MXML conventions.Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com0tag:blogger.com,1999:blog-9194803383693725531.post-78203955017269843532008-03-17T14:43:00.002+02:002012-10-12T00:03:18.093+02:00First Ukrainian Flash Platform User Group official meeting<a href="http://fpug.org.ua/node/54">The meeting</a> has been hosted by Kharkov office of <a href="http://www.globallogic.com/">GlobalLogic</a>, formerly known as <a href="http://www.validio.com/index.html">Validio</a>. Eldar Prilutsky has introduced <a href="http://livedocs.adobe.com/flash/9.0/main/00000136.html">ActionScript 3.0 event model</a>, Rostislav Siryk has shared his experience with <a href="http://www.puremvc.org/">PureMVC framework</a> while developing Moscow vector map and Denis Papazov has told about <a href="http://www.adobe.com/products/flashmediaserver/">Flash Media Server</a>.<br /><br /><a href="http://www.flickr.com/photos/skovalyov/2339147554/" title="Flash Platform User Group by Sergey Kovalyov, on Flickr"><img src="http://farm4.static.flickr.com/3237/2339147554_a2e49e1e5c.jpg" width="375" height="500" alt="Flash Platform User Group" /></a>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com1tag:blogger.com,1999:blog-9194803383693725531.post-84731961822907147752008-03-11T23:44:00.001+02:002012-10-12T00:02:43.870+02:00Macromedia Flash 2 box!<a href="http://www.flickr.com/photos/skovalyov/2312803730/" title="Old stuff by Sergey Kovalyov, on Flickr"><img src="http://farm3.static.flickr.com/2195/2312803730_3a03d61f22.jpg" width="375" height="500" alt="Old stuff" /></a>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com1tag:blogger.com,1999:blog-9194803383693725531.post-31003653652915154442007-02-17T23:46:00.000+02:002012-10-12T00:01:42.817+02:00Text control with truncateToFit property support<p>As you probably know, Label control supports truncateToFit property, while Text doesn't. Even if you explicitly set it to true in your particular Text instance, nothing happens. I have created TruncatedText class that acts the similar way as Label does. If a TruncatedText instance is sized to be smaller than its text, the text is truncated with "...":</p>
<script src="https://gist.github.com/2873429.js?file=TruncatedText.as"></script>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com30tag:blogger.com,1999:blog-9194803383693725531.post-49564521974589002012007-01-10T12:01:00.001+02:002012-10-11T23:59:01.328+02:00How to prevent pop-up blocking in Firefox<p>Using navigateToURL() method causes pop-up blocking feature activation in Firefox since Flash is a plug-in:</p>
<script src="https://gist.github.com/2873465.js?file=NavigateToURLPopUpBlock.as"></script>
<p>Use window.open() JavaScript method instead. I have created URLUtil class with openWindow() static method to make it easier:</p>
<script src="https://gist.github.com/2873441.js?file=URLUtil.as"></script>
<p>Now my blog could be opened in a new browser window this way:</p>
<script src="https://gist.github.com/2873489.js?file=URLUtilOpenWindowExample.as"></script>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com43tag:blogger.com,1999:blog-9194803383693725531.post-16688697881282313102007-01-09T13:51:00.000+02:002012-10-12T00:01:01.704+02:00Left aligned label in FormItem<p>FormItem class doesn't have any styles or properties to control corresponding label alignment. Right alignment is applied by default and it's good idea to keep it "as is", because <a href="http://jroller.com/page/microarchitect?entry=why_you_should_right_align">some usability researches</a> say it's more friendly for user. Though if you want to make your FormItem label left aligned, use ExtendedFormItem class instead of regular FormItem:</p>
<script src="https://gist.github.com/2873541.js?file=ExtendedFormItem.as"></script>
<p>ExtendedFormItem label is left aligned by default, but you can set labelAlign style to "right" if necessary.</p>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com13tag:blogger.com,1999:blog-9194803383693725531.post-61661035666098424872006-12-28T18:38:00.000+02:002012-10-11T23:59:31.739+02:00Singleton in ActionScript 3.0<p>There are some ways to create singleton in ActionScript 3.0, though I prefer public "facade" with static initialization of internal class:</p>
<script src="https://gist.github.com/2878493.js?file=AuthenticationManager.as"></script>
<p>Internal class (it should be in the same package with "facade"):</p>
<script src="https://gist.github.com/2878496.js?file=AuthenticationManagerInstance.as"></script>
<p>Usage example:</p>
<script src="https://gist.github.com/2878500.js?file=AuthenticationManagerUsage.as"></script>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com5tag:blogger.com,1999:blog-9194803383693725531.post-57403161933428872852006-12-27T19:30:00.000+02:002012-10-11T23:59:58.910+02:00Make the focused child visible!<p>Probably, one of the most annoying thing about Flex is lack of "browser native" features support. For example, if you have scrollable container it doesn't scroll its content upon Tab button click to make the focused child visible. However, browser do this! I have created easy to use class AutoScrollManager that makes it possible in Flex:</p>
<script src="https://gist.github.com/2878514.js?file=AutoScrollManager.as"></script>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com1tag:blogger.com,1999:blog-9194803383693725531.post-60741982212924731742006-12-12T16:58:00.000+02:002012-10-12T00:00:30.860+02:00truncateToFit() performance problem<p>Setting very long string to the text property of classes that decorate UITextField (e. g. Label) causes timeout exception. Take a look at the truncateToFit() method in UITextField class. You could find this block there:</p>
<script src="https://gist.github.com/2873564.js?file=TruncateToFitPerformanceProblem.as"></script>
<p>It means that actual text is cut char by char until result of its concatenation with truncation indicator fits into the width available or only one char left. When text is very long this looping is almost infinitive. I believe it would work much more faster with <i>division by 2</i> approach. In this case even 64K text truncation will take only 16 loop cycles.</p>Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com0tag:blogger.com,1999:blog-9194803383693725531.post-20683415141668743102006-12-12T10:52:00.001+02:002006-12-12T10:52:53.985+02:00My 1st post! :)Anonymoushttp://www.blogger.com/profile/12977809674331642977noreply@blogger.com0