Cloud Development Tips: Code Beauty - Beware!
Let's talk about beauty. Specifically, code beauty. That might sound strange for someone that does not write code for a living, but it really does exist! So where to start?
Beauty, of course, is subjective. What I might find appealing might be completely different from the person next to me. The same applies in the world of software development, specially in cloud development where “best practices” such as design patterns and styles are also highly subjective. There are developers who prefer the old fashioned way of 'hacking' their way through a solution and there are others, like myself, who prefer expressive code. Basically, code that transmits its function beyond a certain time frame; code you can read and understand months after you or someone else wrote it! I firmly believe that if you code expressively enough, you won’t need many comments on your code. Hence, beautiful code is simultaneously complex and elegant.
Let’s talk about the flip side of ‘code beauty’. For example, aesthetics can be tricky sometimes: You can find code that is very expressive, but totally wrong. Let me clarify this point a little. It is important to avoid going to the other extreme. Namely, being too expressive and forgetting the main point of writing code, which is to solve a problem — not create a new one!
Below you can find a piece of code for a cloud app that — as surprising as you might find it — is part of the generated code Visual Studio adds when you first create an Azure Worker Role (more on this in future blog posts). If you are a zealot and believe blindly on what your IDE does for you, this might be one of the cases you should review your faith. In this case, the idea behind this code is to avoid your worker role from stopping processing. If you look at it, it might deceive you. The code It’s simple and clearly expresses what the code is intended to do. What else is important you might ask then? Well, you’re wasting precious resources by doing this. Here is when you start shouting you’re working on an I7 processor with 16 GB of RAM and we are no longer in the late 90’s anymore! Although you’re right, remember we are talking about an Azure worker role. Also remember Azure has fees depending on your instance’s size. So, as long as you can save your resources, you’ll be saving a couple of bucks here and there. Probably more than you think.
The thing with this code is that with this construction, the CPU is always busy waiting and awakening.
while (true) { //do something Thread.Sleep(1000); } |
Here is when our savior code comes to the rescue. This might look a little less intuitive but still keeps your worker role from exiting the execution; At the same time, won’t waste precious clock cycles waiting and trying to awaken the thread.
CancellationTokenSource cancelSource = new CancellationTokenSource (); public override void Run() { //do something cancelSource.Token.WaitHandle.WaitOne(); } public override void OnStop() { cancelSource.Cancel(); } |
So, stay alert because the simpler more intuitive solution might not be the best one. Sometimes you need to second guess even your IDE and make sure your code is not only beautiful but efficient.
About the Author
Darien G. is a .Net Developer with 9 years of experience in the software development industry. Darien is passionate about learning new technologies and ways of improving the software development process.
Post Your Comment Here