Frequency code in a program language. Program code

In order for the program to perform the actions prescribed to it, for example, calculate, display the result, respond to user actions, for example, pressing buttons, selecting lines from a list, it is necessary program code.

Program code is a set of words and symbols of a programming language.

Alphabet - this is a complete set of letters, numbers and symbols adopted in the language to denote data and actions on them.

Language alphabet Visual Basic includes the following character set:

Uppercase (A - Z) and lowercase (a - z) letters of the Latin alphabet;

Numbers from 0 to 9;

Signs of arithmetic operations (in ascending order of priority): +, -, *, /, |, ^;

Signs of relational operations: =,<, >.

Punctuation marks and separators: ,  . : ; ();

The alphabet of the language also includes reserved words that cannot be used as names variables or procedures. Examples of reserved words: Dim, Sub, Integer, etc. By default, blue font is used to highlight keywords in the Visual Basic code editing window.

Words and symbols must be written strictly according to the rules of the language, without spelling and punctuation errors. It is the exact spelling that will allow the computer to unambiguously understand and execute the program.

Code Window

The program code is written in the code window. Each form has such a window.

Open code window:

1 way - in the window Project Explorer click right click in the required form and select in the menu that opens Show code.

Note: The code window may not be associated with the form. The separate code window is called Module. Modules in the Project Explorer window are grouped into a group Modules. To open a window with the module code, you need to in the window Project Explorer double click on the module name.

Method 2 - double-click on the control element on the form or on the form itself in the form window.

Note: this not only opens the code window, but also creates an event handling procedure (see below).

Code window structure:

Rice. 7. Program code window.

    List of controls

    List of control events

    Procedure (code)

Procedures

Since when the refrigerator is opened, the lamp lights up, and when the tap is opened, water flows out, we can say that when the event of opening the refrigerator occurs, one procedure is performed, and the event of opening the tap causes another procedure. Likewise program code consists of separate procedures, each of which performs its own specific actions, for example, one procedure lights a lamp, the other turns on (pumps) water.

Visual Basic - procedural programming language. This means that you can create blocks of code in it, which can then be referenced by name. Once a block of code has a name, it can be called and completed. It's like a program within a program. Small programs that "live" in large programs are called functions if they return some value, and subroutines, if they do not return values.

Subroutines and functions make programming easier and faster, and the code you create more reliable. Creating your own routines and functions is the first step to development encapsulated and reusable code. Encapsulation means hiding the implementation of an object's properties and methods behind its external interface.

Procedure (subroutine) is a separate fragment of program code with the help of which a small task is usually solved; it is a logically constructed, small program block into which the entire program is divided.

If a procedure can only be executed within a given program block (for example, only in this form), and cannot be called from another program block, then such a procedure is local. A local procedure is specified using the Private Sub keyword.

If a procedure can be called from other program blocks (for example, from another form), then such a procedure is global. A global procedure is specified using the Public Sub keyword.

By default, if before the Sub keyword, there is no keyword, then this is a global procedure.

The procedures are:

    Event Procedures. Executed when an event occurs on a control (or form). An event procedure is always associated with some object. To call an object's event procedure, simply double-left click on the object.

    Arbitrary procedures. It is not associated with events and can be called from any other procedure and executed at any time. If the same program block occurs in all forms in a program, then it can be written only once and in one place, in the form of a global common program. The launch of a general program is not associated with an object and with an event, but occurs when it is accessed from other program blocks. Calling a general procedure from the same form: ProcedureName (ParameterList). Calling a generic procedure from another form: OBJECT. ProcedureName (ParameterList). The general procedure can be both local Private and global Public

Procedure structure

The procedure consists of the following elements:

    Procedure header - marks the beginning of the procedure, its type, purpose (event).

An example of a procedure title that is executed when a button named Command1 is clicked.

    Word Private means closed, that is, the procedure belongs only to this form or module and cannot be used by other containers (forms, modules). If this word is omitted, the procedure will be open to other containers.

    Sub- procedure.

Note: In addition to procedures, there are functions. Functions are not associated with events and can additionally return the result of their work (calculations).

    Control element(or form name): This specifies the exact name of the element stored in the property Name.

    Event- name of the event. Here are some events:

    Click - mouse click;

    DblClick - double click mouse;

    KeyPress - key press;

    UnLoad - unloading the form (when closing the form, ending the program);

    Activate - activation of the form (when you click on the form and its title is highlighted);

    Deactivate - deactivate the form (when clicking on another form).

    Initialize - when creating an object of type form.

    Resize - when changing the form size

    Terminate - at the moment of deleting the form

    Arguments- this is the initial data passed to the procedure for processing.

Custom procedures have the following header:

Procedure name there must be unique, must begin with a letter and must not contain spaces or characters other than underscores. The procedure is called by name when it needs to be executed.

    End of the procedure - ends the program code of the procedure: End Sub

Note: for functions: End Function

    Procedure body - these are the lines between the title and the ending. Their number is unlimited. The lines contain instructions that must be executed when the procedure is called (the event occurs).

Subroutine (Sub) - is a procedure that executes program code within its block and does not return a value. The syntax of a simple subroutine is:

( Private | Public ) Sub SubMain ()

..lines of code End Sub

Scope of the subroutine;

Sub - procedure type (namely, subroutine);

subMain the name given to the subroutine;

End Sub - end of the subroutine code block.

Creating a procedure

To create a procedure, do the following:

    1 way - double click on the desired control or form. The code window will open, and the title and end of the procedure will appear in it. If another event is needed, it is selected using the list in the upper right corner of the code window.

    Method 2 - open the code window, go to Tools → Add Procedure → specify the name and parameters of the procedure → Ok.

    3 way - open the code window and enter the required lines from the keyboard.

The result should be:

Private Sub Command1_Click()

Calling procedures for execution

    For an event procedure to execute, the event must occur.

    To execute an arbitrary procedure, specify the name of this procedure in the body of another procedure.

Private Sub Command1_Click()

Here, when you click on the Command1 button, the Click event occurs and the Kvadrat procedure is called and executed.

The procedure code is executed line by line and from top to bottom.

Function (Function) - it is a procedure that executes lines of its code and returns some value. The syntax of a simple function is:

Function FunctionName() As Datatype

... linescode

FunctionName = ReturnValueEnd Function

Function scope;

Function is a Visual Basic keyword indicating that it is a function;

FunctionName () - the name given to the function;

AS is a Visual Basic keyword that precedes the assignment of a data type;

DataType return value data type;

ReturnValue the value that should be assigned to the function name (this is a very important point!);

End Function - the end of this block of code.

Today, many companies, as well as individuals, feel the need to have their own website, which is why information on the topic of developing and promoting Internet projects is so in demand. Many people are interested in the question - how to create your own website, program code for which it is like a foundation for a house? Let's try to understand this issue by delving into the topic of web development.

A website is not just a collection of text, links, pictures and colorful banners, it is also a program code that runs on the user’s computer or on the server side. And if you create images of the required format in required resolution and quality today, almost everyone can use ready-made images from the Internet or any popular graphic editor, then create site code for a non-specialist it is fraught with considerable difficulties.

The quality of applications and the Internet project as a whole strongly depends on the skill of the programmer developing the site, the program code of which may contain errors that greatly affect the loading speed of web pages and many other aspects of the operation of the entire site, including those related to security. Therefore, detecting and eliminating errors in the code is a mandatory component when creating any website. It is best to entrust the development of a complex corporate website to specialists (if you are not one), because some errors are difficult to detect, and many of them can further lead to slow loading and incorrect display of web pages in the browsers of Internet users’ computers. Too much long loading can cause an outflow of visitors from the site and a decrease in the quality of traffic, which reduces the profit and efficiency of using commercial Internet projects.

HTML and CSS first

The basis of a web document is code written in HTML markup language. A markup language should not be confused with a programming language, and what the actual difference is is written in detail. In principle, using the set of commands that HTML offers for the site developer, you can set all the necessary parameters of a static web document - the arrangement of elements (block markup), headings, paragraphs, tables, images, etc. And with using CSS, a special add-on for HTML, you can position all the listed markup objects, change their style - color, size, format, etc.

Then JavaScript

Interactive and animated elements, for example - banners, ticker, form feedback, on web pages work due to the presence of scripts and code written in server-side or client-side programming languages. Scripts developed using the language are very popular JavaScript programming. Such client scripts do not use the server's capabilities in their work and are executed on the user's computer side, that is, in the browser. Thanks to this JavaScript applications are characterized by simplicity and high speed work.

And finally PHP

When it is necessary to write complex and voluminous codes, for example for forums or guest books, programmers turn to server-side programming languages ​​for help, and in particular to . PHP codes are executed on the server side, so their work may be slightly slower, depending on the speed of the connection with remote computer and the degree of its workload. WITH using PHP and SQL commands (a special query language for a relational database), you can organize the interaction of a website with databases and create interactive Internet projects - forums, online stores, message boards, various directories, etc.

Don't worry too much about which language you should choose to learn. Many novice programmers find it difficult to choose a language when they first start learning to write software codes. The actual language you choose does not matter when it comes to learning the structures and logic of constructing information. These skills are much more important and can be learned with any programming language.

  • When choosing a language, focus on the purpose for which you want to create program codes, and only then choose a starting language. For example, if you want to develop websites, then you should start by learning HTML and then supplement it with CSS, JavaScript and PHP. If you want to create programs for computers, then start learning C++ or any other basic programming language.
  • If you become a professional programmer, you will quickly realize that you will never use the language you originally learned for your work. Instead, you will continue to learn new languages ​​through documentation and experimentation all the time.

Find free online resources specific to your chosen language. The Internet is a treasure chest of free tutorials, courses, and videos dedicated to the language you choose to learn. You can learn the basics of almost any introductory language in a day.

  • Here are just a few popular sites: Bento, CodeAcademy, Code.org, html.net, Khan Academy, Udacity, W3Schools and many others.
  • wikiHow also has a wide variety of starter tutorials for different languages.
  • You can find educational videos for almost any language on YouTube.
  • Stack Exchange is one of the most popular forums where professional programmers answer any questions from users.
  • Download a good text editor. Many programming languages ​​allow the use of external text editors for writing programs. Find a text editor that will display indentation and code markup.

    • Popular programs include Notepad++ (Windows), TextWrangler (OS X), and JEdit (any system).
  • Download any necessary compilers. Some programming languages ​​require a compiler to display the source code you have written. Compilers translate source code into an equivalent low-level language program, which is then processed by the computer. Many compilers are open source and free programs. Languages ​​that require the use of compilers include:

    • BASIC
    • Fortran
    • Pascal
  • Start your first project. Choose a good introductory project that will allow you to try out your new skills. There are many offers and tutorials on this topic on the Internet; for example, you can start by creating simple websites with HTML, simple databases and functions with PHP, or simple programs in any of the languages ​​that require the use of compilers.

    Purpose

    Source code either used to produce object code or executed by an interpreter. Changes are never made to object code, only to source code, and then converted back to object code.

    Another important purpose of source code is as a description of a program. Based on the text of the program, you can reconstruct the logic of its behavior. Comments are used to make source code easier to understand. There are also tools that allow you to automatically obtain source code documentation - the so-called. documentation generators.

    In addition, the source code has many other uses. It can be used as a teaching tool; Beginning programmers may find it useful to examine existing source code to learn programming techniques and methodology. It is also used as a communication tool between experienced programmers due to its concise and unambiguous nature. Sharing Code management is often cited by developers as a contributing factor to improving the programmer experience.

    Programmers often move source code (in modules, as is, or with adaptations) from one project to another, which is called code reuse.

    Source code is a critical component for the process of porting software to other platforms. Without the source code of any piece of software, porting is either too difficult or not possible at all.

    Organization

    The source code of some part of the software (module, component) may consist of one or more files. Program code is not necessarily written in only one programming language. For example, often programs written in C language, for optimization purposes, contain inserts of code in assembly language. It is also possible that some components or parts of a program are written in different languages, and then assembled into a single executable module using a technique known as library linking ( library linking).

    Complex software when assembled, it requires the use of dozens or even hundreds of source code files. In such cases, to simplify the build, project files are usually used that contain a description of the dependencies between the source code files and describe the build process. These files may also contain options for the compiler and design environment. For different design environments, different project files can be used, and in some environments these files can be in a text format, suitable for direct editing by a programmer using universal text editors; in other environments, special formats are supported, and the creation and modification of files is carried out using special tools programs. Project files are usually included in the term "source code". The vast majority of modern language environments require the use of project files, regardless of the complexity of the other source code included in the project. Often, source code also means resource files containing various data, for example, graphic images, needed to build the program.

    To facilitate work with source code, for collaboration on code by a team of programmers, version control systems are used.

    Quality

    Unlike humans, there is no “well-written” or “badly written” code for a computer. But how the code is written can greatly influence the software maintenance process. The quality of the source code can be judged by the following parameters:

    • code readability (including the presence of comments to the code);
    • ease of support, testing, debugging and error correction, modification and porting;
    • economical use of resources - memory, processor, disk space;
    • absence of comments output by the compiler;
    • absence of “garbage” - unused variables, unreachable code blocks, unnecessary outdated comments, etc.;
    • adequate error handling;
    • portability - the ability to use a handler (compiler, interpreter, translator) different versions, or even different OS;
    • possibility of interface internationalization.

    Non-executable source code

    Copyleft licenses for free software require distribution of the source code. These licenses are also often used for works that are not software - for example, documentation, images, data files for computer games.

    In such cases, the source code is considered to be the form of the work that is preferred for editing. In licenses other than software, it may also be referred to as the "transparent format" version. This could be, for example:

    • for a file compressed with loss of data - lossless version;
    • for rendering a vector image or a three-dimensional model - respectively, the vector version and the model;
    • for a text image - the same text in text format;
    • for music - a file in the internal format of the music editor;
    • and finally, the file itself, if it satisfies the specified conditions, or if a more convenient version simply did not exist.

    See also


    Wikimedia Foundation. 2010.

    See what “Source code” is in other dictionaries:

      Source materials of the film: negative, counterprint, control copy of the film, original magnetic phonograms of music dubbing, noise, video phonogram master, CD, etc. Synonyms: the text is either directly executed by the interpreter, or... ... Financial Dictionary

      Human written text computer program in any programming language Dictionary of business terms. Akademik.ru. 2001 ... Dictionary of business terms

      source code- - Telecommunications topics, basic concepts EN source code ... Technical Translator's Guide

      source code- 3.1.13 source code: A computer program expressed in a human-readable form (programming language) that is translated into machine-readable form (object code) before it can be tested with... ... Dictionary-reference book of terms of normative and technical documentation

      source code- source text language... Explanatory translation dictionary

      Source code: Source code is the human-written text of a computer program. Source code techno-thriller directed by Duncan Jones ... Wikipedia

      This term has other meanings, see source code. Source code Source Code ... Wikipedia

      This article lacks links to sources of information. Information must be verifiable, otherwise it may be questioned and deleted. You can... Wikipedia

      Open Source Initiative (OSI) logo Open source software is open source software. Source code created programs open, that is, available for viewing and changing. This is... ... Wikipedia

      Source program (source code)- 48) source program (source code) a corresponding representation of one or more processes that can be converted by a programming system into a form executable by hardware (object code or object language) (categories 6, 7 and 9); ... Official terminology

    15 Rules for Writing Quality Code

    There are myriads of ways to write bad code. Fortunately, to rise to the level of quality code, you just need to follow 15 rules. Following them will not make you a master, but it will allow you to convincingly imitate it.

    Rule 1: Follow coding standards.

    Each programming language has its own standard for formatting code, which tells how to indent, where to put spaces and parentheses, how to name objects, how to comment on code, etc.

    For example, in this piece of code, according to the standard, there are 12 errors:

    For(i=0 ;i

    Study the standard carefully, learn the basics by heart, follow the rules like commandments, and your programs will be better than most written by college graduates.

    Many organizations tailor standards to suit their specific needs. For example, Google has developed standards for more than 12 programming languages. They are well thought out, so check them out if you need help programming with Google. The standards even include editor settings to help you follow the style, and special tools to verify your code conforms to that style. Use them.

    Rule 2: Give descriptive names.

    Limited by slow, clunky teletype machines, programmers in ancient times used contracts for variable and procedure names to save time, keystrokes, ink and paper. This culture is present in some communities for the sake of maintaining backward compatibility. Take, for example, the language-breaking C function wcscspn (wide character string complement span). But this approach is not applicable in modern code.

    Use long, descriptive names like complementSpanLength to help you and your colleagues understand your code in the future. The exceptions are a few important variables used in the body of a method, such as loop iterators, parameters, temporary values, or execution results.

    It is much more important that you think long and hard before naming something. Is the name accurate? Did you mean highestPrice or bestPrice? Is the name specific enough to avoid its use in other contexts for similar objects? Wouldn't it be better to call the method getBestPrice instead of getBest? Does it fit better than other similar names? If you have a ReadEventLog method, you shouldn't call another NetErrorLogRead. If you name a function, does the name describe the return value?

    In conclusion, a few simple rules naming. Class and type names must be nouns. The method name must contain a verb. If a method determines whether some information about an object is true or false, its name must begin with "is". Methods that return properties of objects must begin with "get", and methods that set property values ​​must begin with "set".

    Rule 3: Comment and document.

    Begin each method and procedure by describing in a comment what this method or procedure does, parameters, return value and possible errors and exceptions. Describe in the comments the role of each file and class, the contents of each class field, and the main steps of complex code. Write comments as you develop the code. If you think you will write them later, you are deceiving yourself.

    In addition, make sure your application or library has a manual that explains what your code does, defines its dependencies, and provides instructions for building, testing, installing, and using it. The document should be short and convenient; a simple README file is often sufficient.

    Rule 4. Don't repeat yourself.

    Never copy and paste code. Instead, isolate the common part into a method or class (or macro, if necessary), and use it with the appropriate parameters. Avoid using similar data and pieces of code. Also use the following techniques:

    • Generate API references from comments using Javadoc and Doxygen.
    • Automatic generation of Unit tests based on annotations or naming conventions.
    • Generate PDF and HTML from a single tagged source.
    • Retrieving the class structure from the database (or vice versa).

    Rule 5: Check for errors and respond to them.

    Methods may return error symptoms or throw exceptions. Process them. Don't rely on the fact that the disk will never fill up, your configuration file will always be there, your application will run with all the rights it needs, memory allocation requests will always succeed, or that your connection will never fail. Yes, good processing errors are hard to write and make the code longer and harder to read. But ignoring errors simply sweeps the problem under the rug, where an unsuspecting user will one day discover it.

    Rule 6: Divide your code into short, discrete parts.

    Each method, function or block of code should fit in a regular screen window (25-50 lines). If it turns out longer, divide into shorter pieces. Even within a method, divide long code into blocks, the essence of which you can describe in a comment at the beginning of each block.

    Moreover, each class, module, file or process must perform a certain kind of task. If a piece of code performs completely different tasks, then divide it accordingly.

    Rule 7. Use framework APIs and third-party libraries.

    Explore what features are available through your framework's API. and also what mature third party libraries can do. If the libraries are supported by your system package manager, then they will most likely be good choice. Use a code that discourages the desire to reinvent the wheel (and a useless square one at that).

    Rule 8: Don't over-design.

    Design only what is relevant now. You can make your code generic enough to support future development, as long as it doesn't become too complex. Avoid creating parameterized classes, factories, deep hierarchies, and hidden interfaces to solve problems that don't even exist - you can't guess what will happen tomorrow. On the other hand, when the structure of the code does not suit the task, do not hesitate to refactor it.

    Rule 9: Be consistent.

    Do the same things in the same way. If you are developing a method whose functionality is similar to that of an existing one, use a similar name, similar parameter order, and similar body structure. The same applies to classes. Create similar fields and methods, give them similar interfaces, and match new names to existing ones in similar classes.

    Your code must follow the conventions of your framework. For example, it is good practice to make ranges semi-open: closed (inclusive) on the left (at the beginning of the range) and open (exclusive) on the right (at the end). If there are no agreements for a particular case, then make a choice and stick to it fanatically.

    Rule 10: Avoid security problems.

    Modern code rarely works in isolation. It has an imminent risk of becoming a target of attack. They don't have to come from the Internet; the attack can occur through your application's input data. Depending on your programming language and domain, you may need to worry about buffer overflows, cross-site scripting, SQL injection, and other similar issues. Study these problems and avoid them in your code. It's not difficult.

    Rule 11: Use efficient data structures and algorithms.

    Simple code is often easier to maintain than the same code but modified for efficiency. Fortunately, you can combine maintainability and efficiency using the data structures and algorithms your framework provides. Use map, set, vector and algorithms that work with them. This will make your code cleaner, faster, more scalable, and more memory efficient. For example, if you store a thousand values ​​in a sorted set, the intersection operation will find common elements with another set in the same number of operations, rather than a million comparisons.

    Rule 12. Use Unit tests.

    The complexity of modern software makes it more expensive to install and more difficult to test. A productive approach would be to accompany each piece of code with tests that check the correctness of its operation. This approach simplifies debugging because it allows errors to be detected earlier. Unit testing is necessary when you program in dynamically typed languages ​​like Python and JavaScript because they only catch any errors at runtime, whereas languages ​​with static typing like Java, C# and C++ can catch some of them at compile time. Unit testing also allows you to refactor your code with confidence. You can use XUnit to make it easier to write tests and automate their execution.

    Rule 13: Keep your code portable.

    Unless you have a specific reason, don't use functionality that is only available on a specific platform. Do not rely on certain data types (like integers, pointers, and timestamps) to have a specific length (for example, 32 bits), because this parameter differs on different platforms. Keep program messages separate from the code, and don't hardcode culture-specific parameters (such as decimal separators or date formats). Conventions are needed so that code can run in different countries, so make localization as painless as possible.

    Rule 14: Make your code composable.

    A simple command should assemble your code into a form ready for distribution. The command should allow you to quickly build and run the necessary tests. To achieve this goal, use automated build tools like Make, Apache Maven, or Ant. Ideally, you should install an integration system that will check, build, and test your code whenever it changes.

    Rule 15: Place everything in version control.

    All your elements - code, documentation, tool sources, build scripts, test data - should be in version control. Git and GitHub make this task cheap and hassle-free. But many others are also available to you powerful tools and services. You should be able to build and test your program on a configured system simply by downloading it from the repository.

    Conclusion.

    By making these 15 rules part of your daily practice, you'll end up creating code that's easier to read, that's well tested, that's more likely to run correctly, and that's much easier to change when the time comes. You'll also save yourself and your users a lot of headaches.

  • Share