ada works . it

Readability

One of the most contentious subjects for C/C++ programmers is how to lay out the curly brackets { and } that are used to mark the beginning and end of a compound statement. Of course this is not an issue for Ada. Here we take a look at a three deep nested "if" statement as an example of readability. After all my years of C/C++ using the second format of bracketing here, I still conclude that the Ada code is pleasently more readable. At the time of writing I have been using Ada for 3 years and written thousands of lines of code.

C/C++ the common bracketing

I would like to propose that the most common bracketing mode introduced by Kernighan and Ritchie in their famous book "C Programming Language" is appauling beyond belief and that if you wanted to establish some rules intended to make code unreadable this does just that. To it's credit its only 37 lines.

#include <iostream> int main() { int a = 10, b = 20, c = 30; if (a > b) { if (b > c) { if (a > c) { std::cout << "a is the greatest" << std::endl; } else { std::cout << "c is the greatest" << std::endl; } } else { if (a < c) { std::cout << "a is not the greatest but greater than b" << std::endl; } else { std::cout << "a is greater than b but c is not the smallest" << std::endl; } } } else { if (b < c) { if (a < c) { std::cout << "c is the greatest" << std::endl; } else { std::cout << "b is not the smallest but greater than a" << std::endl; } } else { if (a > b) { std::cout << "b is not the greatest but greater than c" << std::endl; } else { std::cout << "b is the greatest" << std::endl; } } } return 0; }

Ada

Ada emmediately clarifies things and also in only 37 lines.

with Ada.Text_IO; use Ada.Text_IO; procedure Nested_If_Example is a : Integer := 10; b : Integer := 20; c : Integer := 30; begin if a > b then if b > c then if a > c then Put_Line("a is the greatest"); else Put_Line("c is the greatest"); end if; else if a < c then Put_Line("a is not the greatest but greater than b"); else Put_Line("a is greater than b but c is not the smallest"); end if; end if; else if b < c then if a < c then Put_Line("c is the greatest"); else Put_Line("b is not the smallest but greater than a"); end if; else if a > b then Put_Line("b is not the greatest but greater than c"); else Put_Line("b is the greatest"); end if; end if; end if; end Nested_If_Example;

C/C++ my improved bracketing

I can't fully claim the credit for this style as it was around before I started in C back in about 1990 but the rule is simple. The { must be in the same column or on the same line as the }. By doing this you emmediately can see the nested structure with as much clarity as Ada however this still takes 43 lines. I would perhaps agree that without syntax highlighting this might be more readable than Ada.

#include <iostream> int main() { int a = 10, b = 20, c = 30; if (a > b) { if (b > c) { if (a > c) {std::cout << "a is the greatest" << std::endl;} else {std::cout << "c is the greatest" << std::endl;} } else { if (a < c) {std::cout << "a is not the greatest but greater than b" << std::endl;} else {std::cout << "a is greater than b but c is not the smallest" << std::endl;} } } else { if (b < c) { if (a < c) {std::cout << "c is the greatest" << std::endl;} else {std::cout << "b is not the smallest but greater than a" << std::endl;} } else { if (a > b) {std::cout << "b is not the greatest but greater than c" << std::endl;} else {std::cout << "b is the greatest" << std::endl;} } } return 0; }

But C/C++ is faster

Faster to execute yes (C is about twice as fast), faster to write no and more likely to produce unreliable code.

OK let's replace C/C++ with Rust

Rust is not only a little faster than C but also it is a very safe language in which you cannot cause buffer overflows. It has yet to prove it's self in the real world to the extent that Ada has but its a solit language.

Now I am being cruel here and choosing something I know Ada is good at and readable in. Starting 100 independent concurrent tasks (threads) each to count to 1000 and print out the counts as we go. So perhaps I am being unfair to Rust here.

In Ada

with Ada.Text_IO; use Ada.Text_IO; procedure Concurrent_Counters is -- Define a task type that will count and print numbers. task type Counter_Task is entry Start; end Counter_Task; task body Counter_Task is begin accept Start; for I in 1 .. 1000 loop Put_Line(Integer'Image(I)); end loop; end Counter_Task; -- Create an array of 100 tasks. type Task_Array is array (1 .. 100) of Counter_Task; Tasks : Task_Array; begin -- Start all tasks. for I in Tasks'Range loop Tasks(I).Start; end loop; end Concurrent_Counters;

In Rust with good bracketing

use std::{thread, time::Duration}; fn main() { // Create a vector to hold the thread handles. let mut handles = vec![]; for i in 1..=100 { // Spawn a new thread. let handle = thread::spawn(move || { for j in 1..=1000 { println!("Task {}:{}", i, j); // Add a small delay to make the output more readable. thread::sleep(Duration::from_millis(1)); } }); // Store the thread handle. handles.push(handle); } // Wait for all threads to finish. for handle in handles {handle.join().unwrap();} }