Skip to content Skip to sidebar Skip to footer

How Do You Read a Code on a Toyostove

When you lot cease reading this, yous'll know how to code.

You'll exist bad at it, but any.

  • people who are interested in coding, but want to become a gustatory modality of what it's like before they commit any time or effort to it;
  • people who work directly with coders and want to sympathize the nuts and bolts of what they practise;
  • people who are starting their Computer science journey and want a 100-level primer in a nutshell;
  • people who have seen programmers on Tv and are curious to know what they're typing all the fourth dimension.

What is a computer program?

A computer programme — also known as an app, an application, or a piece of software — is a collection of many lines of special text. They're special considering a computer can empathize them, and computers are incredibly impaired. The smallest practical apps take a hundred or then lines of text in them. The largest apps have billions.

The tools of the trade

A programmer's toolbox typically consists of a few things:

  • A computer.
  • An internet connectedness. You need an Net connection so you can use Google. When you lot don't know how to exercise something (which happens virtually 787283493857 times per 24-hour interval) you Google it. When your app gives you an fault message, yous Google it. You Google everything.
  • A code editor (or an IDE, which is a code editor on steroids). Call back of it every bit Microsoft Discussion (or ameliorate yet, Edward the App) for coding. Information technology helps you organize and proofread your code.
  • A compiler or interpreter. This is a programme that reads your code, tries to find mistakes then it can bug you almost them, gathers your code into a nice piddling package, so passes it on to the computer to execute. It does a lot of other things, also, merely these are the things you need to know about correct now.
  • A skilful pair of headphones. These help you ignore people then you tin focus.

Programming languages

There are thousands of programming languages in the world. Many of them are impaired and useless. Yous can have a long and happy career only knowing three or 4 — but don't worry, this isn't as hard every bit learning three or four homo languages.

Putting data in code

Enough background: permit's write some code. The virtually basic building cake of a computer plan is a variable. A variable is a name for a piece of data, like a number or a sentence. You take to name your data.

"Sully. You're not supposed to proper name it. Once you name it, you first getting attached to it."
  1. You lot don't know what it is beforehand. Does Facebook know your birthday before you type it in? No, of course non. Facebook's code is using a name like dummy's_birthday behind the scenes (the underscore is there because variable names tin't have spaces in them). Information technology attaches that proper name to whatever birthday you lot blazon in. That style, the lawmaking can be exactly the aforementioned whether your birthday is June 10th, September 9th, or December 86th. Information technology just moves dummy's_birthday effectually in the plan until it reaches an ugly warehouse computer.
  2. You don't desire to forget what it means. Suppose the computer programme needs to know that December 86th isn't a real date. So somewhere yous have to tell it that Dec has 31 days. 31 is a number, a piece of data. But if you're using the number 31 all over your code, and also using numbers like 30 and 28 (because, thank goodness, December isn't the just calendar month), your code is going to be confusing to read. What are all those numbers for? So you proper name them. 31 becomes the_number_of_days_in_december, and your code becomes self-explanatory. Beautiful.

This may be aligned to the right or bottom edge of your browser.

Circuitous types

Objects

An important part of coding is learning how to organize data. Have the birthday example: Facebook has hundreds of millions of birthdays (and anniversaries and breakup dates) stored in computer retention in its warehouse. How does it know what'due south what? If I gave y'all a list of a billion dates, would you know who they belong to and what they're for? Of course you would, because y'all are the great Calendifus, Greek god of randomly meaning dates.

          var dummy = {
id: 12884002,
age: 28,
name: 'Bob',
favorite_song: 'Photo, by Nickelback'
}
          dummy.id
dummy.proper name
dummy.age
dummy.favorite_song

Arrays

Sometimes y'all don't desire to call up up a unique proper noun for every belongings in an object, especially if they're all very like. Or you don't know how many there are going to be. That'south when it's time to utilise an array, which is a list of like pieces of information. Arrays tin can grow or shrink as needed.

          var dumb_tweets = ['Hi, Twitter!', 'My friends are and then absurd', 'Does anyone want a LaCroix?']        
          dumb_tweets[0]
dumb_tweets[1]
dumb_tweets[2]
          dumb_tweets[2] = 'I regret literally everything I accept ever said'        
          var nested_object = {
an_array: [
{
another_array: [
{
yet_another: [
{
message: 'Blink twice if you demand help'
}
]
}
]
}
]
}
          nested_object.an_array[0].another_array[0].yet_another[0].message        

Objects (continued)

1 more trippy part, and and then we can move on to the fun stuff.

          dumb_tweets.length        

APIs

Fourth dimension for a scary campfire story.

          dumb_tweets.push button('Man I detest proficient attitudes')        
          ['Hello, Twitter!', 'My friends are then cool', 'I regret literally everything I take ever said', 'Man I hate good attitudes']        

Functions

Role is some other word for method. Information technology'south just a slice of lawmaking that does something and (unremarkably) has a name. Functions are easy to declare in JavaScript:

          role giveMeOne() {
render ane
}
          var the_loneliest_number = giveMeOne()        
  1. giveMeOne() is a office nosotros wrote by ourselves. push button() is a function that some strangers wrote. It's okay, they don't listen if nosotros use it.
  2. button() is a method of dumb_tweets (and whatsoever other array we'll e'er create). giveMeOne() is global, meaning that we don't need to refer to a specific object in guild to utilise information technology.
          function addTheseNumbersTogetherPlz(number1, number2) {
return number1 + number2
}
          function addTheseNumbersTogetherPlz(number1, number2) {
var sum = number1 + number2
render sum
}

Logical branches and comparisons

This is where code gets extra fun. (It was fun already.)

          var age = getUserAge()
if (historic period >= 21) {
allowThemInTheNightclub()
} else {
throwThemOutOnTheirButt()
}
  • === (3 equals signs) compares the values on either side to see if they are exactly equal. If they are equal, the effect is truthful. half-dozen === half-dozen would be truthful.
  • !== compares the values on either side to see if they are not exactly equal. If they are non equal, the event is truthful. 6 !== 3 would be true.
  • > checks to see if the value on the left side is bigger than the value on the right side. half-dozen > 3 would be true.
  • < checks to see if the value on the right side is bigger than the value on the left side. 3 < 6 would be truthful.
  • >= checks to see if the value on the left side is bigger than, or equal to, the value on the right side. 6 >= 6 and vi >= 5 are both true.
  • <= checks to see if the value on the right side is bigger than, or equal to, the value on the left side. vi <= half dozen and 6 <= 7 are both truthful.

Loops

Sometimes, especially when you're working with an array, you lot want to execute a block of code several times in a row. This is non the fourth dimension to utilize re-create and paste. Instead, y'all should apply a loop. The simplest kind of loop in JavaScript is a while loop:

          var the_real_slim_shady = ['My proper name is', 'My name is', 'My name is', 'Waka waka Slim Shadyyy']          var index = 0
while (index < the_real_slim_shady.length) {
rap(the_real_slim_shady[index])
index = index + one
}

Zippo and undefined

If you lot declare a variable and practise not assign a value to it, it will hold a special value called undefined. This is a geeky discussion that ways "motility along, nothing to run into hither." Information technology's more often than not useless.

Scopes

Functions are very selfish. If you declare a variable inside of a function, the part won't permit any of the code exterior of itself use the variable. For example:

          function whatHappensInVegas() {
var wildIndiscretions = ['partied', 'danced']
return 'I admit goose egg'
}
whatHappensInVegas()
whatHappensInVegas()
whatHappensInVegas()
if (wildIndiscretions.length > 0) {
getADivorce()
}

Comments

It isn't always obvious what a piece of code is doing, or what still needs to be done with it. If y'all demand to break out of the reckoner language and take some real talk near what'due south going on in the code (or only drop some dope lyrics), you tin can use a comment, or a line of code that the computer volition ignore. Yous kickoff a comment with // two frontwards slashes. Like this:

          role isEven(num) {
// This function determines if "num" is even or odd.
// If even, it returns true. If odd, it returns simulated.
// TO DO: add a second argument that lets yous specify
// a bulletin to brandish if "num" is odd.
return (num % 2) === 0
// Hey, yo, practise it like Isaac
// If you ain't with me, baby ain't wise-aac
}

O Google tree, O Google tree…

The last and most important matter I can teach yous is this: when you don't know how to do something, immediately get to google.com and inquire. The generosity of the programming community will astound you. Thousands of developers all effectually the world take freely shared their lawmaking and knowledge on sites like GitHub and Stack Overflow, which means that all you need to become an expert in any programming language is a steady internet connexion and the ability to read.

          [programming linguistic communication] how to [something]        

Synthesis

Nigh large-scale apps use all the concepts and keywords I've described. Their code comprises thousands upon thousands of lines of code, all built from these same bones elements. So what are they doing?

Conclusion

Ya done expert. I've taught y'all enough programming basics that, with a little imagination and plenty of Googling, you can teach yourself everything you lot need to know to write apps. If you want to.

Further Reading

  • How to Recollect Similar a Figurer Scientist, by Allen Downey, Jeffrey Elkner and Chris Meyers. This is a more than well-written and comprehensive (but arguably less entertaining) version of what I've written here, geared toward the Python programming language.

rogerswittleen1945.blogspot.com

Source: https://medium.com/@isaaclyman/when-you-finish-reading-this-youll-know-how-to-code-721339942b51

ارسال یک نظر for "How Do You Read a Code on a Toyostove"