Skip to main content

Crafting Ring Algorithms

  • Chapter
  • First Online:
Beginning Ring Programming
  • 413 Accesses

Abstract

In its most basic form, designing an algorithm, whatever programming language you are using, is all about transforming inputs to outputs. Hence, at its heart, programming is the process of crafting a logic flow from the data received and delivering data to the user as a solution to a problem.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 54.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 69.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    Bret Victor is an interface designer, computer scientist, and electrical engineer known for his talks on the future of technology. Victor received attention for his talks “Inventing on Principle” (2012) and “The Future of Programming” (2013). Now he is working on his Dynamicland vision where “programming” occurs in a visible space: a room. A “programmer” uses her sight and her body to “touch” the program she is creating and live inside it. This vision, if achieved, will free us from being slaves of our screens and IDEs, and will transform programming into a humanized activity open to everyone. You can find more on Dynamicland.org.

  2. 2.

    Examples are Visual Studio from Microsoft, Eclipse IDE from IBM, and Ring Notepad.

  3. 3.

    This particular facet of visualizing computer programs when they execute will be covered in Chapter 6 so you, as a Ring programmer, can “watch” your variables living in runtime and “moving” between the local, object, and global scopes. In the current chapter, we will focus on the visibility of the design facet of the computer code and not on its runtime facet.

  4. 4.

    The content of this section is widely based on the explanations given by https://drakonhub.com, an online tool providing the Drakon language in a web browser. The open source Drakon project itself, with a free desktop version of the software, can be found at http://drakon-editor.sourceforge.net/.

  5. 5.

    The skewer is a long piece of wood or metal used for holding pieces of food, typically meat, together during cooking. The Drakon Project uses it as a metaphor for the side line that maintains all the other components of the flowchart.

  6. 6.

    You could say that good and bad and pleasant and unpleasant are relative judgments that cannot be consistent across people and even across different statuses of the same person. How this can be used in a structured discipline like programming? My short answer is that programming is structured by design, but in reality isn’t, because those who write it are human beings. There is a lot of fussiness on what we say in code. In small programs, good and bad relate to the programmer assessment of the situation, but we need to stay consistent with that across the entire program. In large codebases, the customer perspective needs to be adopted: the happy path is what makes the customer happier, and the less pleasant path is what she tries to avoid happening in the first place.

  7. 7.

    Test it in a diskaria.ring file you create inside the folder for this chapter: c:\ringbook\chap5.

  8. 8.

    Performance optimization is one of the main requirements of software quality. Performance can be defined as the amount of work performed by a computer to accomplish a programming task. Many factors influence program performance: time spent by the program execution (including the time program itself, the operating system time, and the network time), speed of the CPU, and, especially, complexity of the program code. In Chapter 9, I will give you a hint about how this is approached by professional programmers in term of planning for the time and space complexity of computer programs.

  9. 9.

    Do you know what += used for? For example, n += 1 is an abbreviation of n = n + 1, yet it is more elegant and generally comes with better speed. You can also use n -= 3, n ∗= 5, and so on.

  10. 10.

    Note the change I made for better readability: but instead of else. Read about the syntax flexibility of the Ring language in the documentation center: http://ring-lang.sourceforge.net/doc1.11/syntaxflexibility.html.

  11. 11.

    I will explain the use of brackets ({ }) later in the chapter.

  12. 12.

    Please note that I’m aware this is not the cleanest approach to a counting algorithm, but it is helpful for the sake of pedagogy. By the way, test it in a new count10.ring file.

  13. 13.

    You’ll learn more about this in the next section.

  14. 14.

    You can use any variable name you choose instead of item. Usually this needs to reflect the nature of the list items. So, if we are iterating over a list of people called aPersons[], we say for person in aPerson[].

  15. 15.

    It’s not a stack overflow really, but a numeric overflow, even if the first error is correctly reported by Ring Notepad. I will explain the root error and how you can “unleash” it in the next section when I introduce while loops. For now, you can reflect on the difference between the two kinds of error by reading those definitions online.

  16. 16.

    As mentioned in previous chapters, Ring doesn’t provide a native form of Boolean variable. These can be expressed by the two variables TRUE and FALSE, which correspond to the numbers 1 and 0, respectively. In Ring, everything is TRUE except the number zero, which is FALSE.

  17. 17.

    In fact, you shouldn’t care about the // Functions part of the program because what we are learning here is what is related to the “thinking” stuff designed in the Drakon diagram. Structurally, this corresponds to the main region of a Ring program, as presented in previous chapters. Remember that functions and classes in Ring go always to the end of file, and this is really useful to stay focused on the problem at hand.

  18. 18.

    Don’t be disappointed if the result is “nothing” in the Output window. Try again many times (Ctrl+F5) so that the randomness engine generates a number corresponding to an existing puppy in the aMyPref[] list. We will fix this soon in the section.

  19. 19.

    I dedicate this to one of my friends on the Apress team who will recognize himself in the story.

  20. 20.

    Don’t take this as gospel. I pretend I’m fast, but in reality I’m notably slow in thinking before writing any code. So, take your time!

  21. 21.

    It is always better to use TRUE and FALSE variables to represent logical conditions. Despite this, the use of a string like :AVAILABLE and :UNAVAILABLE in the current case can provide us with more clarity and expressiveness.

  22. 22.

    Always use the console to test your programs that deal with infinite loops and a large amount of data. There you can interrupt the execution, anytime you want, by simply pressing Ctrl+D on the keyboard. In the Output window of Notepad, you are obliged to wait until an error message is displayed or close the IDE and open it again, which is impractical. You will prefer the console, in this situation, for another reason related to the nature of the error generated by Ring. To explain it, let’s take the case where the iteration loops over a number and increments it indefinitely (we will have an example like this in the next section). When you run the loop on the console, the error you get is Numeric Overflow, which corresponds to the root of the problem you are facing. When you use the Notepad Output window, which uses internally a QTextEdit widget using a QProcess object from the Qt framework, the error you will get is Stack Overflow, which is not wrong but camouflages the root error (Numeric Overflow) and prevents it from happening and being communicated to you. This is risky and misleading, especially when you are developing critical applications.

  23. 23.

    You will see in the next section that this condition can be also at the bottom of the while block.

  24. 24.

    To learn a language, you need to understand its culture. This includes the motivations of the language designer and the challenges he wants to solve, along with the design options he implemented to distinguish his language from other programming languages. In many places in the book, I try to show you the cultural aspects of the Ring language, so please pay attention to them. Also, the discussion I had with Mahmoud Fayed, the Ring inventor, as presented in the appendix of the book can be enlightening and really useful.

  25. 25.

    Here is some sincere advice: use this visual paradigm until it becomes unnecessary to you and is a natural reflex, in other words, a spontaneous way of thinking in code!

  26. 26.

    This example is taken as is from the Ring documentation.

  27. 27.

    Do you know that Linux, one of most secure and efficient software applications in the world, makes use of GOTO extensively? OK, but these are coding gurus, and you’ll do better not to imitate them.

  28. 28.

    This is a beautiful definition from Wikipedia.

  29. 29.

    An Arabic term that means let’s go!

  30. 30.

    This is a free photo from this public domain: https://tinyurl.com/yxq4kosv.

  31. 31.

    A practical example of that will be presented in Chapter 8 when we will refactor one of the games delivered with Ring.

  32. 32.

    Those who have practiced object-oriented programming know that these principles are actuated by the object paradigm itself (privacy and method call). Nevertheless, many OOP programmers can deviate from these principles and come with unorganized classes of code. Take it then as a general-purpose rule you should follow, whatever programming paradigm you opt for. By the way, in Chapter 7 you will discover the fluency of the Ring language in supporting many programming paradigms (structured, OOP, functional, declarative, natural and dynamic programming) and make a meaningful connection between them.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Mansour Ayouni

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Ayouni, M. (2020). Crafting Ring Algorithms. In: Beginning Ring Programming. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5833-0_5

Download citation

Publish with us

Policies and ethics