Software Engineering Concepts
Contents
Software Engineering Concepts¶
This page will have discussions on more advanced topics for those interested in diving deeper.
Coding Style and Documentation¶
Self documenting code¶
Using Type Declarations when to specify, vs when to use “generic” / over-loadable
Use of comments¶
Design Patterns¶
Everything we have talked about so far has been very practical, we are learning the basics of syntax and how to write some basic programs.
We are now going to take a step back and talk about some essentials in writing “good” code.
A Central Question to ask yourself before writing code¶
I would categorize two main types of writing code: scripting and everything else (I say it this way because the other word used is usually programming, and scripting is a type of programming so, yeah there isn’t a good word). I will call everything else code base development (development for short). These two concepts are not mutually exclusive, they are two sides of a coin or ends of a spectrum.
Scripting is writing (single use) code that uses other code to perform some specific task.
Development is building a larger set of functions that can be combined and used in many different ways to accomplish many related tasks.
Object oriented programming¶
“Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).”
The construct to define the data and code are called classes. These constructs are how we model the real world concepts in code.
A “instantiation” of a class (i.e. using the class definition and creating one) is called an object. E.g. if we have a person class, we can create multiple people objects representing the different people involved in our model.
A Quick Caveat¶
Julia is not an “object-oriented” language, however the concepts of objected oriented programming can and should still be applied. The exercise of arranging the functionality you build into logically separated groups will pay dividends in how well organized and understandable your code will be. I will talk about most of this in general and provide some concrete examples for study.
Bicycle Class¶
An easy example of a concept we want to class could be a bicycle. There is a multitude of attributes about a bicycle, so lets pick a context for the problem. Let’s think about what we would need to know if we were writing a simulation with a bunch of bikes on a road.
attributes about the car that do not change
make
model
year
vin number
weight
coefficient of drag
color
gears
wheel diameter
attributes that might change
its speed moving on the road
position on the road (gps, (x,y,z), etc)
what gear the bike is currently in
functions that could be performed on a bicycle
speed up (push the pedal)
slow down (hit the breaks or )
steer right or left
change gear
update position
Rider Class¶
The next question to ask is who is going to be using the bicycle. Obviously the bike doesn’t pedal itself, so it shouldn’t be calling the speed up function on itself. We can define a special type of person and its relationship with the bike. In our model we can describe the relationship as the rider “has a bike.”
The rider can push the pedals, hit the breaks, look for other bikes or obstacles and decide to steer the bike.
World / Simulation class¶
Then at the highest level we could define a class with functions that run the simulation. It keeps track of all the riders and their bikes, and prompts the riders to perform actions on the bikes.
Design Question¶
What class should keep track of positions and velocities of the bikes over time?
How would any of this differ if we were modeling a sailboat instead of a bike.
Example¶
I have made two “simulations” of bike riders moving around on a two dimensional area. One shows what code can look like if you do not follow any sort of patters, implementing all of the functionality in one big lump. The other shows how it might look if you follow some object oriented design. It should be evident which is easier to understand and to extend.