0% found this document useful (0 votes)
3 views

DevNet Associate -Coding Basics

The document covers coding basics for the DevNet Associate certification, focusing on clean code principles, methods, functions, modules, and classes. It emphasizes the importance of writing clean, readable, and maintainable code to facilitate collaboration and reduce technical debt. Additionally, it discusses best practices for encapsulating code into methods and functions for clarity and reusability.

Uploaded by

crio1q
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DevNet Associate -Coding Basics

The document covers coding basics for the DevNet Associate certification, focusing on clean code principles, methods, functions, modules, and classes. It emphasizes the importance of writing clean, readable, and maintainable code to facilitate collaboration and reduce technical debt. Additionally, it discusses best practices for encapsulating code into methods and functions for clarity and reusability.

Uploaded by

crio1q
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

7/23/24, 8:59 AM DevNet Associate -Coding Basics

  DevNet Associate v1.0

Show Menu
Lab - Software Version Control  / Software Development and Design / Coding Basics
3.3.11
with Git Development and
Software
3 Design 
3.4 Coding Basics  Coding Basics
Methods, Functions, Modules, and
3.4.1
Classes
3.4.1

Clean Code
3.4.2
Methods, Functions, Modules, 

and Classes
3.4.3 Methods and Functions

3.4.4 Modules It may seem easy to throw code in a file and make it work.
But as project size and complexity grows, and as other
3.4.5 Classes developers (and stakeholders) get involved, disciplined
methods and best practices are needed to help
developers write better code and collaborate around it
3.4.6 Lab - Explore Python Classes more easily.

One thing that most developers agree on is trying to write


3.5 Code Review and Testing 
clean code. But, what is clean code?

3.6 Understanding Data Formats 

Software Development and


3.7
Design Summary  3.4.2

Clean Code 
Understanding and Using
4 
APIs

Clean code is the result of developers trying to make their


code easy to read and understand for other developers.
5 Network Fundamentals 
What constitutes "clean code" is somewhat subjective,
but here are some common principles:
Application Deployment and
6  Is the formatting of the code neat, and does it follow
Security
generally-accepted formatting practices for the
computer language(s) involved, and/or meet specific
Infrastructure and requirements of the institutional, project, and/or team
7 
Automation "stylebook"?
Does it stick with ALL tabs or ALL spaces?

Cisco Platforms and


8 
https://contenthub.netacad.com/devnet/3.4.1 1/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  Does it use the same number of tabs or spaces per
indentation level, throughout the file? (Some
languages, such as Python, make this a
requirement.)
Show Menu Does it have the indentation in the right places?
Lab - Software Version Control
3.3.11 Does it use consistent formatting for syntax, such
with Git Development and
Software as the location of curly braces ({})?
3 Design 
Are variable, object, and other names used in the code
3.4 Coding Basics  intuitive?
Is the code organized in a way that it makes sense?
Methods, Functions, Modules, and For example, are declarations grouped, with functions
3.4.1
Classes up top, mainline code at the bottom, or otherwise,
depending on language and context?
3.4.2 Clean Code Is the code internally documented with appropriate
comments?
Does each line of code serve a purpose? Have you
3.4.3 Methods and Functions
removed all unused code?
Is the code written so that common code can be
3.4.4 Modules reused, and so that all code can be easily unit-tested?

Clean code emphasizes:


3.4.5 Classes

standardized formatting and intuitive naming for ease


3.4.6 Lab - Explore Python Classes of reading, understanding, and searching
overall organization to communicate intention and
make things easier to find
3.5 Code Review and Testing  modularity to facilitate testing and reuse
inline and other comments
3.6 Understanding Data Formats  other characteristics that help make code self-
documenting
Software Development and
3.7
Design Summary  In theory, other developers should be able to understand,
use, and modify your code without being able to ask you
questions. This accelerates development enormously,
Understanding and Using enabling reuse, debugging, security analysis, code review
4 
APIs and merging, along with other processes. It lets
longstanding projects (for example, open source projects)
incorporate your code with greater confidence. And it lets
5 Network Fundamentals  organizations keep using your code efficiently, after you
have moved on to other projects or jobs.

Application Deployment and


6  By contrast, code that is not clean quickly becomes
Security
technical debt. It may be unmaintainable, or unusable with
complete confidence. It is code that needs to be
Infrastructure and refactored, or removed and rewritten, which is expensive
7  and time-consuming.
Automation

Cisco Platforms and


8 
https://contenthub.netacad.com/devnet/3.4.1 2/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  What are some other reasons developers want to write
clean code?

1. Clean code is easier to understand, more compact,


Show Menu and better-organized, which tends to result in code
Lab - Software Version Control
3.3.11 that works correctly (fewer bugs) and performs as
with Git Development and
Software required.
3 Design 
2. Clean code, being modular, tends to be easier to test
3.4 Coding Basics  using automated methods such as unit testing
frameworks.
Methods, Functions, Modules, and 3. Clean code, being standardized, is easier to scan and
3.4.1
Classes check using automated tools such as linters, or
command-line tools like grep, awk, and sed.
3.4.2 Clean Code 4. It just looks nicer.

Now that you understand the goals of writing clean code,


3.4.3 Methods and Functions
you can dive deeper into coding best practices.
Specifically, looking at how to break code into methods
3.4.4 Modules and functions, modules, and classes.

3.4.5 Classes
3.4.3

3.4.6 Lab - Explore Python Classes


Methods and Functions 

3.5 Code Review and Testing 

Methods and functions share the same concept; they are


3.6 Understanding Data Formats  blocks of code that perform tasks when executed. If the
method or function is not executed, those tasks will not
Software Development and be performed. Although there are no absolute rules, here
3.7
Design Summary  are some standard best-practices for determining
whether a piece of code should be encapsulated (in a
method or function):
Understanding and Using
4 
APIs Code that performs a discrete task, even if it happens
only once, may be a candidate for encapsulation.
Classic examples include utility functions that evaluate
5 Network Fundamentals  inputs and return a logical result (for example,
compare two strings for length), perform I/O
operations (for example, read a file from disk), or
Application Deployment and
6  translate data into other forms (for example, parse and
Security process data). In these case, you encapsulate for
clarity and testability, as well as for possible future re-
Infrastructure and use or extension.
7  Task code that is used more than once should
Automation
probably be encapsulated. If you find yourself copying
several lines of code around, it probably needs to be a
Cisco Platforms and
8 
https://contenthub.netacad.com/devnet/3.4.1 3/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  method or function. You can accommodate slight
variations in usage using logic to assess passed
parameters (see below).

Show Menu The most powerful thing about methods and functions is
Lab - Software Version Control
3.3.11 that they can be written once and executed as many
with Git Development and
Software
3  times as you like. If used correctly, methods and functions
Design will simplify your code, and therefore reduce the potential
3.4 Coding Basics  for bugs.

Methods, Functions, Modules, and Syntax of a Function in Python:


3.4.1
Classes

# Define the function


3.4.2 Clean Code def functionName:
...blocks of code...
3.4.3 Methods and Functions # Call the function
functionName()

3.4.4 Modules
Arguments and Parameters

3.4.5 Classes Another feature of methods and functions is the ability to


execute the code based on the values of variables passed
3.4.6 Lab - Explore Python Classes in on execution. These are called arguments. In order to
use arguments when calling a method or function, the
method or function needs to be written to accept these
3.5 Code Review and Testing  variables as parameters.

Parameters can be any data type and each parameter in a


3.6 Understanding Data Formats 
method or function can have a different data type.
Arguments passed to the method or function must match
Software Development and
3.7  the data type(s) expected by the method or function.
Design Summary

Depending on the coding language, some languages


Understanding and Using require the data type to be defined in the parameter (so-
4  called 'strongly typed' languages), while some permit this
APIs
optionally.

5 Even when parameter type specification is not required, it


Network Fundamentals 
is usually a good idea. It makes code easier to reuse
because you can more easily see what kind of parameters
Application Deployment and a method or function expects. It also makes error
6  messages clearer. Type mismatch errors are easy to fix,
Security
whereas a wrong type passed to a function may cause
errors that are difficult to understand, deeper in the code.
Infrastructure and
7 
Automation Parameters and arguments add flexibility to methods and
functions. Sometimes the parameter is just a boolean flag
Cisco Platforms and that determines whether certain lines of code should be
8 
https://contenthub.netacad.com/devnet/3.4.1 4/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  executed in the method or function. Think of parameters
as being the input to the method or function.

Syntax of a function using arguments and parameters in


Show Menu Python:
Lab - Software Version Control
3.3.11
with Git Development and
Software
3  # Define the function
Design def functionName(parameter1,...,parameterN):
3.4 Coding Basics  # You can use the parameters just like
local variables
Methods, Functions, Modules, and ...blocks of code...
3.4.1
Classes # Call the function
functionName("argument1", 4,
3.4.2 Clean Code {"argument3":"3"})

3.4.3 Methods and Functions The example above is passing this function a string, an
integer (or number), and an object containing a key and a
value.
3.4.4 Modules

Return Statements
3.4.5 Classes
Methods and functions perform tasks, and can also return
a value. In many languages, the return value is specified
3.4.6 Lab - Explore Python Classes
using the keyword return followed by a variable or
expression. This is called the return statement. When a
3.5 Code Review and Testing  return statement is executed, the value of the return
statement is returned and any code below it gets skipped.
It is the job of the line of code calling the method or
3.6 Understanding Data Formats  function to grab the value of the return, but it is not
mandatory.
Software Development and
3.7
Design Summary 
Syntax of a function with a return statement in Python:

Understanding and Using # Define the function


4 
APIs def functionName(parameter1,...,parameterN):
# You can use the parameters just like
local variables
5 Network Fundamentals  ...blocks of code...
someVariable = parameter1 * parameter2
return someVariable
Application Deployment and # Call the function
6 
Security myVariable = functionName("argument1", 4,
{"argument3":"3"})

Infrastructure and
7  In the above example, the returned value would be the
Automation
string "argument1argument1argument1argument1",

Cisco Platforms and


8 
https://contenthub.netacad.com/devnet/3.4.1 5/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  because Python lets you concatenate strings using the
multiplication operator.

Function Example
Show Menu
Lab - Software Version Control
3.3.11 Let's say your original code looks like this:
with Git Development and
Software
3 Design 
# Print the circumference for circles with a
3.4 Coding Basics  radius of 2, 5, and 7
radius1 = 2
Methods, Functions, Modules, and radius2 = 5
3.4.1
Classes radius3 = 7
# Formula for a circumference is c = pi *
3.4.2 Clean Code diameter
# Formula for a diameter is d = 2 * radius
pi = 3.14 # (Will hardcode pi in this
3.4.3 Methods and Functions
example)
circumference1 = pi * radius1 * 2
3.4.4 Modules print ("Circumference of a circle with a
radius of " + str(radius1) + " is " +
str(circumference1))
3.4.5 Classes
circumference2 = pi * radius2 * 2
print ("Circumference of a circle with a
3.4.6 Lab - Explore Python Classes radius of " + str(radius2) + " is " +
str(circumference2))
circumference3 = pi * radius3 * 2
3.5 Code Review and Testing 
print ("Circumference of a circle with a
radius of " + str(radius3) + " is " +
3.6 Understanding Data Formats  str(circumference3))

Software Development and As you can see, there is a lot of duplicate code here.
3.7
Design Summary 

By using methods with parameters, your code can be


cleaned up:
Understanding and Using
4 
APIs
# Print the circumference for circles with a
radius of 2, 5, and 7
5 Network Fundamentals  # In this example, circumference and
# printCircumference are the names of
# the functions. 'radius' is a parameter to
Application Deployment and # the function and can be used in the
6 
Security function.
# This function returns the value of the
# circumferenceValue to the code that called
Infrastructure and
7  # the function.
Automation
def circumference(radius):
# Formula for a circumference is c = pi *
Cisco Platforms and diameter
8 
https://contenthub.netacad.com/devnet/3.4.1 6/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  # Formula for a diameter is d = 2 *
radius
pi = 3.14 # (Will hardcode pi in this
example)
Show Menu circumferenceValue = pi * radius * 2
Lab - Software Version Control
3.3.11 return circumferenceValue
with Git Development and
Software def printCircumference(radius):
3 Design 
myCircumference = circumference(radius)
3.4 Coding Basics  print ("Circumference of a circle with a
radius of " + str(radius) + " is " +
Methods, Functions, Modules, and str(myCircumference))
3.4.1
Classes radius1 = 2
radius2 = 5
3.4.2 Clean Code radius3 = 7
# In the below line of code, the value of
radius1 (2)
3.4.3 Methods and Functions
# is the argument to the printCircumference
function
3.4.4 Modules printCircumference(radius1)
printCircumference(radius2)
printCircumference(radius3)
3.4.5 Classes

Notice that the version of code that uses functions,


3.4.6 Lab - Explore Python Classes parameters, and arguments results in no duplicate code.
Also, by using functions, you are able to label blocks of
code, which make their purposes more understandable. If
3.5 Code Review and Testing 
this example were more complicated and there were a lot
of lines of code within each function, having the blocks of
3.6 Understanding Data Formats  code duplicated three times in the file would make it
much harder to understand.
Software Development and
3.7
Design Summary  Methods vs. Functions

If methods and functions share the same concept, why


Understanding and Using
4  are they named differently? The difference between
APIs
methods and functions is that functions are standalone
code blocks while methods are code blocks associated
with an object, typically for object-oriented programming.
5 Network Fundamentals 
Method example

Application Deployment and


6  The code from the function example can be modified to
Security
turn the function into a method, producing the same
result:
Infrastructure and
7 
Automation
# Print the circumference for circles with a
radius of 2, 5, and 7
Cisco Platforms and # In this example, there is a class named
8 
https://contenthub.netacad.com/devnet/3.4.1 7/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  Circle.
# Classes will be discussed later.
circumference
# and printCircumference are the names of
Show Menu the
Lab - Software Version Control
3.3.11 # method in the class. These methods returns
with Git Development and
Software the
3 Design 
# value of the circumferenceValue to the
3.4 Coding Basics  code
# that called the method.
Methods, Functions, Modules, and class Circle:
3.4.1
Classes def __init__(self, radius):
self.radius = radius
3.4.2 Clean Code def circumference(self):
# Formula for a circumference is c =
pi * diameter
3.4.3 Methods and Functions
# Formula for a diameter is d = 2 *
radius
3.4.4 Modules pi = 3.14 # (Will hardcode pi in this
example)
circumferenceValue = pi * self.radius
3.4.5 Classes
* 2
return circumferenceValue
3.4.6 Lab - Explore Python Classes def printCircumference(self):
myCircumference = self.circumference()
print ("Circumference of a circle with
3.5 Code Review and Testing  a radius of " + str(self.radius) + " is " +
str(myCircumference))
radius1 = 2
3.6 Understanding Data Formats 
radius2 = 5
Software Development and radius3 = 7
3.7
Design Summary  # Since Circle is a class, it must be
instatiated
# with the value of the radius first.
Understanding and Using circle1 = Circle(radius1)
4 
APIs # Since printCircumference is a method, it
must be
# called using the [class instance].[method]
5 Network Fundamentals  # syntax. Just calling printCircumference()
will
# not work
Application Deployment and circle1.printCircumference()
6 
Security circle2 = Circle(radius2)
circle2.printCircumference()
circle3 = Circle(radius3)
Infrastructure and
7  circle3.printCircumference()
Automation

Cisco Platforms and


8 
https://contenthub.netacad.com/devnet/3.4.1 8/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development 
3.4.4

Modules 
Show Menu
Lab - Software Version Control
3.3.11
with Git Development and
Software
3 Design  Modules are a way to build independent and self-
contained chunks of code that can be reused. Developers
3.4 Coding Basics 
typically use modules to divide a large project into smaller
parts. This way the code is easier to read and understand,
Methods, Functions, Modules, and and each module can be developed in parallel without
3.4.1
Classes
conflicts. A module is packaged as a single file. In
addition to being available for integration with other
3.4.2 Clean Code modules, it should work independently.

3.4.3 Methods and Functions A module consists of a set of functions and typically
contains an interface for other modules to integrate with.
It is essentially, a library, and cannot be instantiated.
3.4.4 Modules
Module example
3.4.5 Classes
Below is a module with a set of functions saved in a script
called circleModule.py . You will see this script again
3.4.6 Lab - Explore Python Classes later in the lab for this topic.

3.5 Code Review and Testing  # Given a radius value, print the
circumference of a circle.
# Formula for a circumference is c = pi * 2
3.6 Understanding Data Formats 
* radius

Software Development and


3.7  class Circle:
Design Summary

def __init__(self, radius):


Understanding and Using self.radius = radius
4 
APIs
def circumference(self):
pi = 3.14
5 Network Fundamentals  circumferenceValue = pi * self.radius
* 2
return circumferenceValue
Application Deployment and
6  def printCircumference(self):
Security
myCircumference = self.circumference()
print ("Circumference of a circle with
Infrastructure and a radius of " + str(self.radius) + " is " +
7 
Automation str(myCircumference))

Cisco Platforms and


8 
https://contenthub.netacad.com/devnet/3.4.1 9/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  An application that exists in the same directory as
circleModule.py could use this module by importing it,
instantiating the class, and then using dot notation to call
its functions, as follows:
Show Menu
Lab - Software Version Control
3.3.11
with Git Development and
Software from circleModule import Circle
3 Design 
# First instantiation of the Circle class.
3.4 Coding Basics 
circle1 = Circle(2)
# Call the printCircumference for the
Methods, Functions, Modules, and
3.4.1 instantiated circle1 class.
Classes
circle1.printCircumference()

3.4.2 Clean Code


# Two more instantiations and method calls
for the Circle class.
3.4.3 Methods and Functions circle2 = Circle(5)
circle2.printCircumference()

3.4.4 Modules
circle3 = Circle(7)
circle3.printCircumference()
3.4.5 Classes

3.4.6 Lab - Explore Python Classes

3.4.5

3.5 Code Review and Testing 


Classes 

3.6 Understanding Data Formats 

Object-orient programming (OOP), as originally


Software Development and
3.7
Design Summary  conceived, is based on some formally defined properties:
encapsulation , data abstraction , polymorphism, and
inheritance. In this course, using Python, you will focus on
Understanding and Using Python class structures as one manifestation of OOP.
4 
APIs
In most OOP languages, and in Python, classes are a
means of bundling data and functionality. Each class
5 Network Fundamentals  declaration defines a new object type.

Encapsulating functionality together with data storage in a


Application Deployment and single structure also accomplishes one aspect of data
6 
Security abstraction. Functions defined within a class are known as
class methods. Classes may have class variables and
object variables. As a new class object is created, new
Infrastructure and
7  class data members and object data members (variables)
Automation
are created. New classes may be defined, based on
existing, previously defined classes, so that they inherit
Cisco Platforms and
8 
https://contenthub.netacad.com/devnet/3.4.1 10/11
7/23/24, 8:59 AM DevNet Associate -Coding Basics
8 Development  the properties, data members, and functionality
(methods).

As with other Python data structures and variables,


Show Menu objects are instantiated (created) as they are first used,
Lab - Software Version Control
3.3.11 rather than being predeclared. A class may be
with Git Development and
Software instantiated (created) multiple times, and each with its
3 Design 
own object-specific data attribute values. (Python classes
3.4 Coding Basics  also support class variables that are shared by all objects
of a class.) Outside of the class name scope, class
Methods, Functions, Modules, and methods and data attributes are referenced using the dot
3.4.1
Classes notation: [class instance].[method name].

3.4.2 Clean Code Note: Unlike other OOP languages, in Python, there is no
means of creating 'private' class variables or internal
methods. However, by convention, methods and variables
3.4.3 Methods and Functions
with a single preceding underscore ( _ ) are considered
private and not to be used or referenced outside of the
3.4.4 Modules class.

3.4.5 Classes
3.4.6

3.4.6 Lab - Explore Python Classes


Lab - Explore Python Classes 

3.5 Code Review and Testing 

In this lab, you review Python methods, functions, and


3.6 Understanding Data Formats  classes. You then create a class and instantiate it several
times with different values. Finally, you review the Circle
Software Development and class example used in the course.
3.7
Design Summary 
You will complete the following objectives:

Understanding and Using Part 1: Launch the DEVASC VM


4 
APIs Part 2: Review Functions, Methods, and Classes
Part 3: Define a Function
Part 4: Define a Class with Methods
5 Network Fundamentals  Part 5: Review the circleClass.py Script

Application Deployment and


6   Explore Python Cla…
Security

Infrastructure and
7 
Automation
 
3.3 3.5
Version Control Sys… Code Review and T…

Cisco Platforms and


8 
https://contenthub.netacad.com/devnet/3.4.1 11/11

You might also like