Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, March 13, 2016

Success with Ruby and BASIC

A while back, I started a project to learn Ruby. The project was to implement a version of BASIC, using that as a means to learn the Ruby language.

I am happy with my progress. I've learned a great deal about Ruby -- and learned more about programming. Ruby has a different "take" on programming than C++, Java, and C#, and those differences forced me to re-think a lot of ideas about programming. The design patterns for Ruby are different from the patterns of the "classical" object-oriented languages. I found that much of my code followed the classic patterns, and it worked poorly. Only after I changed my thinking about programming and my designs in Ruby did my code "work".

Rubocop helped too. It complained about many things, from spacing around operators (or lack thereof) to the number of lines and complexity of functions. At first I thought that some long functions would have to remain long -- in C++ or Java I would leave them as long functions. But with Rubocop's prodding, I redesigned them and made them smaller, many times breaking them into smaller functions.

i started using Rubocop after a significant amount of code had been written, and Rubocop reported somewhere north of 80 "violations". Over time, with thought and experimentation, I have reduced that number to one. The changes have, I believe, improved the code and made it more reliable and more readable.

My success has been due to the design of the Ruby language, the Rubocop tool, the Ruby documentation pages, and StackOverflow. These made it easy to develop in Ruby. Yet there were two other things: an open mind and time. I needed to change my thinking about programming, and accept the Ruby way of code. I also needed time. Time to think about the code, time to try things, and time to revise my initial code into something better.



Sunday, January 2, 2011

A little bit of fun

For fun, I wrote a little class in C# to handle arithmetic with significant figures. This is one of my goals for the new year. (I'm already getting one of them done! Wow, I am productive!)

I used Visual Studio Express for C#, which is simpler than the full Visual Studio package yet powerful enough to handle this task. The Express edition works almost identically to the Professional edition, so I was familiar with the environment.

The solution consists of a small class to handle the arithmetic and a small program to invoke the class. I don't have any test cases; I want to create some as I am uncomfortable working without them.

But the class does what it is supposed to do. The result of 1000 (1 sig-fig) plus 20 is 1000, not 1020 as one would expect. The answer should be 1000 since you must keep the number of significant figures at 1 and not expand it. Which means that 1000 plus 200 is also 1000, and 1000 + 200 + 200 + 200 + 200 + 200 is still 1000 although 200 + 200 + 200 + 200 + 200 + 1000 results in 2000, since the '200' amounts add up to 1000 and then the second 1000 yields 2000.

Leaving the oddities of sig-fig math, and coming back to programming, it was a fun project. Now perhaps I will port the solution to a few other languages.