Thank you Stack Overflow

Tonight, I have successfully harnessed the power of the developer hive mind.

To play around with lists and randomness, I created a list of chocolate bars (as strings), and I wanted to select one at (pseudo) random. I tried the code we covered in college to deal with random numbers, but it whinged at me about int/string data type clashes (and, as usual, info in the Visual Studio help boxes was as clear as mud).

I did an Internet search, which isn’t unusual and my search terms were “how to use c# random instance to choose random string from list”. Normally I scan a few of the search result pages and nothing makes much sense to me, so I slink away, dejected.

My first Stack Overflow
Thanks Joey!

This time however, I found the above response to a question on Stack Overflow and it didn’t look too intimidating. I managed to adapt it and stick it in my code, and lo, it seems to have worked!

Random string choc
Random Twix selection

I’m sure the code above is not the most elegant, I don’t really understand what I did, and I broke things trying to fix the line breaks, but for now I am basking in the achievement of my randomly selected Twix.

Successfully implementing something I found on Stack Overflow – now I feel like I’m a programmer 😉

Back to school

Despite not updating this blog properly for a few weeks, I haven’t turned my back on the world of binary manipulation. As I mentioned in the last post, I have embarked on a 10 week course entitled ‘Programming for beginners’ at a local college.

I am currently three weeks in, and am just about coping so far. The lessons are 3 hours per week (not the 2 that was advertised in the course prospectus) which is pretty tough after 8 hours at work but I’ve found taking a thermos of tea and some Mini Cheddars along helps immensely…

Making tea
My tea making flow diagram

The first lesson was spent being gently introduced to some general computing principles, and to the representation of algorithms through flow charts and decision tables.

This took up most of the time, during which the free community version of Visual Studio was downloading/installing at a glacial rate on my old and creaky laptop. I then had the exciting decision of choosing the ‘dark’, ‘light’ or ‘blue’ display option – obviously I went for the cool dark one.

Once up and running, we bashed out a ‘Hello World’ relatively quickly (see this previous post), and then it was time to go home.

Homework entailed looking around my house at ‘technology’ to decide whether I would class it as a computer or not. Cue an in depth conversation with the boyfriend (a computer science graduate) about the nature of my washing machine and whether it might ‘go all Skynet’ on us in the night. (He says unlikely but in the era of internet fridges, I think caution around domestic appliances is prudent…)

The following week we moved onto declaring and initialising variables. Now I think this means:

  1. Decide some information to record and name it;
  2. Decide the type of data that best fits the value of this information;
  3. Give a starting value.

Or, in more depth…

  • Give a descriptive but concise name to some information I want to record. For example, how many students are enrolled on my college course? That last sentence is rather wordy so I might choose ‘classSize’ as a name instead.
  • I think ‘classSize’ is a variable. Variable (if that is indeed what it is) names are written in something called ‘camel case’. I don’t know why yet but I’m sure it’ll become clear. (My ‘camel case game’ has already been complimented by a developer I know… *smug*)
  • Knowing what to call my variable is one part of it. Next is deciding on the most appropriate type of data for storing the value of the variable. So far, I have been introduced to around 10 data types. I’m sure there are more lurking around C#…
  • For the number of students in my class, I’m happy to use an integer (int) data type because I don’t need fractions (you can’t have half a student enrolled).
  • Actually, I can use a ‘uint’ because, from what I can tell, this means a whole number that isn’t negative. And you can’t really have a negative number of students enrolled.
  • (uints can hold a huge number, more students than could ever be enrolled, so I could (and maybe should) choose a more appropriate type. However, I am comfortable with the word integer so tough.)
  • In the cool black Matrix-eqsue window that is there when you create a new ‘console application’ in Visual Studio, I write: ‘uint classSize’.
  • There are already some words and punctuation marks knocking around the console application but for now I’m ignoring them. All I know is put my stuff under the bit that has the word ‘main’ in it and make sure one of these wiggly things { has a closing one (}).
  • That’s my variable declared. Now I want to initialise it, or give it a starting value. For the number of students, I can give it a value of 10, because that’s how many people I counted in the room.
  • So now it looks like: ‘uint classSize = 10’. Although, something is missing…
  • My favourite punctuation mark, misunderstood and misused by many, is the semi-colon; if you can use one of these correctly in writing, my respect level for you will automatically increase. It turns out in C# they are bloody everywhere. I am currently operating on an ‘if in doubt, stick a ; on the end’ basis.
  • ‘uint classSize = 10;’ That’s looking better.
  • Last but not least, the big reveal. It seems that I can make the number 10 appear in a box by typing underneath: Console.WriteLine(classSize); and pressing play (run? compile?).
  • If I want to get really fancy, I can write some actual human words around the 10 by typing: Console.WriteLine(“The number of students enrolled in the class is ” + classSize);
Class size example
I’m sure this is how Facebook started…

So there we have it. I can make some words appear in a black box by typing some stuff. I don’t think the developers at work will be asking for my help just yet.

Some observations so far

  • There are many decisions to make (what I call the information I want to record, what data type is suitable for the value of the information, what colour scheme I use for Visual Studio…)
  • For these simple bits of code, I’ve noticed my fellow students are sometimes writing it differently. I can already see why shared conventions and effective communication will make code written by others easier to maintain.
  • Doing stuff with time is hard (ffs base 12/60 – who invited you to the party?!).
  • I am the only (obviously) female student in the class. I wonder why this is.
  • Programmers seem opinionated. My college teacher was less than complimentary about Visual Basic (I don’t know why), and a developer at my karate class asked why I wasn’t learning a ‘better’ language than C#. Developers, please try and explain you opinions to newbies/non-technical folk better! Saying VB is ‘clunky’ doesn’t really give me much to go on.