From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "G.B." Newsgroups: comp.lang.ada Subject: Re: if-then-no-else Programming Date: Tue, 19 Apr 2016 14:17:16 +0200 Organization: A noiseless patient Spider Message-ID: References: <1mlx1gf.ebrae11jak5tyN%csampson@inetworld.net> Reply-To: nonlegitur@futureapps.de Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 19 Apr 2016 12:13:54 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="b96887e80893c84a90c3007226ca0d1c"; logging-data="22373"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Tfv/oN97PvQ/G0ukmLXcAaNFyZGLrfsE=" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 In-Reply-To: <1mlx1gf.ebrae11jak5tyN%csampson@inetworld.net> Cancel-Lock: sha1:DLMT1k63CdP6TaABQjb7Uly9Db4= Xref: news.eternal-september.org comp.lang.ada:30191 Date: 2016-04-19T14:17:16+02:00 List-Id: On 19.04.16 04:24, Charles H. Sampson wrote: > He says that there's a move to ban the use of the else-statement. The > preferred approach is to execute the else-part first, then change the > effect if the if-condition is satisfied. For example: > > Variable := 3; > if then > Variable := 1; > end if; I think that "software engineering" here might mean web development or media development; not the typical areas of engineering, AFAIK. But while speculation, this really seems to about making sure that variables have a value assigned, not so much about conditionals per se. If about guaranteed assignment, then languages have started supporting the initialization problem in much better ways. The above example reminds me of PHP/Python/Javascript, etc. Swift, OTOH, a newly developed language, guarantees assignment before use and rejects, as do some older language for constants, this definition: func foo(condition: Bool) { var Variable : Int if condition { Variable = 1 } bar(Variable) NN:error: ^ variable 'Variable' used before being initialized } GNAT has a similar warning. Is there still a reason why, in embedded system, assignment is considered costly, maybe, and thus had better be done only as necessary, instead of being enforced, using default values, or dummy values? Or even constructor invocations? Ada 2012 adds conditional expressions (in addition to the old nested functions that need to return). So, if the value that is to be assigned depends on Condition, Variable := (if Condition then 1 else 3); is another way of making sure that Variable is initialized, in *one* statement! This expression is part of many languages, albeit using different syntax. The list includes Python, Javascript, C, ...