We (Anders Ohlsson and humble me) use
for i := 1 to Limit do begin
blah blah
end;
if Condition then begin
blah blah
end;
while Condition do begin
blah blah
end;
Coding guides recommends and VCL source use
for i := 1 to Limit do
begin
blah blah
end;
if Condition then
begin
blah blah
end;
while Condition do
begin
blah blah
end;
It troubled me for the long time, but I managed not to make
a (illogical to me) switch.
And, now, when I know I'm not alone,
but accompanied with guys from Borland like Anders Ohlsson.
I'll never surrender.
To tell the truth, I use it more radically than he does,
i use begin end for cases with single line in between,
so I have no worries if I add second line in the block later.
So I use
if Condition then begin
Writeln( 'YESSS!!!!' );
end;
and he use
if Condition then
Writeln( 'YESSS!!!!' );
Is some efficiency penalty here for me?
I use this way begin..end blocks too. I´m not from Borland but i know
that we are more than you, Anders Ohlsson and me.
I have a difference with you. If a begin..end block is not needed i
don´t use it, so i code:
if condition then
sentence;
--
JAM - Relájate y disfruta...
Hrvoje, The only penalty, that I can see is having to explain it.
Rgds, JohnH
I would write it as so
for i := 1 to Limit do begin
blah blah
For j := 1 to 10 do Begin
more blah
End;
end;
each to their own!!
Mat
Mat, I prefer the same, including the alternating UC/LC to
help identify matching begins and ends. Regards, JohnH
Me too!! I started about a year ago because I was taught using the
conventional style. The main reason I started was to reduce the
line count so I had a better picture of how many actual lines are
being compiled. I liked it so much, I just kept doing it.
>
> if Condition then begin
> blah blah
> end;
I also started doing multiple if statements this way:
if Condition1 then begin
blah
blah
end else if Condition2 then begin
blah
blah
end else if ...
--
Woody (TMW)
----------------------
If you try to fail, and succeed, which have you done?
George Carlin
> Very pleased to see that Anders Ohlsson himself
> uses coding style similar to mine,
> and different from coding style guides and VCL source.
I do not use the same coding style as you. It's only a matter of
"readability". If you find it more easy to read it your way, then go for it.
I find it more easy to read it my way, so I go for my way.
There's no good/bad coding standard. There's a Borland official way, there's
a Microsoft official way, there's a Clinton official way... every one have
their own particularities and differences. I put three spaces for
indentation, when Borland seams to put two.
There's some people who fight about the "right" coding standard. Is there a
"Ultimate Coding Standard" for everyone? I don't think so. It's coding
standard. It's like saying that your favorite color is not the best one.
I wrote these lines because I had a fight one time with a guy about coding
standard. He told me that he's way was better, I told him that my way was,
for me, the better way. The fight last for months and that post remind me
the fight.
So, to answer your question:
> if Condition then begin
> Writeln( 'YESSS!!!!' );
> end;
> Is some efficiency penalty here for me?
I don't think so. The begin...end block only indicates which line are part
of the condition. If you convert your way and the other way to Assembler,
you'll see that it generate the same amount of code.
for example
CMP 0,0 (The condition... which is true in this case)
JZ :cond_yes (If the condition is true, then go to label :cond_yes)
:cond_yes
your_lines
return
So, your way or the other way produce the same amount of code.
This is according to me. I maybe wrong on all what I said.
HTH
VB
I do it the same way because it seems easier to read when the END
signals the end of its own indent level. As you say, though, to each his
own.
Regards, Glynn
First, let me say that you and everyone else can code however you
want, and I don't care. I only post here to offer _my_ view......;-)
I use a begin..end block everywhere, even for single statements
if SomeCondition then
begin
Exit;
end;
and
if SomeCondition then
begin
DoThingA;
end else
begin
DoThingB;
end;
The reason that I do this is that if I want to add a line of code to
the block , I don't create a bug by going from
if SomeCondition then
DoSomething;
to
if SomeCondition then
DoSomething;
DoSomethingElse;
as opposed to what it really should be
if SomeCondition then
begin
DoSomething;
DoSomethingElse;
end;
Also, this makes debugging easier. For instance, it is hard to debug
if SomeComplicatedCondition then DoThingA else DoThingB;
as it is not always clear what the flow is.
In addition, I really rely on having my begin...end pairs line up.
One of the first things I do when I install Delphi is add these two
code templates
begin
|
end;
and
begin
|
end else
begin
end;
Lining up the begin end statements is key, and I never mind if they
get nested, as to me, nested begin...end's are always very clear.
Of course, this is entirely personal, and one should code however one
wants.
Nick Hodges
Lemanix Corporation
How to ask questions of techies --
http://www.tuxedo.org/~esr/faqs/smart-questions.html
> I downloaded VCL scanner with source.
> Very pleased to see that Anders Ohlsson himself
> uses coding style similar to mine,
> and different from coding style guides and VCL source.
>
> We (Anders Ohlsson and humble me) use
>
> for i := 1 to Limit do begin
> blah blah
> end;
I think it is horrible, but to each his own. I normally use a code
formatter that formats to my favourite style, if I must read other
people's code.
--
Rudy Velthuis (TeamB)
My writing style has evolved over the last 29 years. When I find a better
way... and I define better as making the code easier to read, easier to
understand and easier to modify without errors... I change what I write and
how I write it.
I have to.
Using a "poor" style is just too inefficient. A poor style is one that
promotes errors, or encourages misreading or is too difficult to understand.
I know because I learned the hard way. I made mistakes. I misread code. I
fixed unbroken code. I screwed up.
But I learned. And I am still learning.
The following RTF formatted paper shows where I was four years ago. In my
current style, I've changed some of the details, but the major thrust
remains the same.
http://www.ghg.net/chairouchu/CSCI3132/lectures/format01.rtf
Steve C McConnell's "Code Complete" is an excellent book and makes many of
the same points:
http://www.amazon.com/exec/obidos/ASIN/1556154844/qid=1014747848/sr=1-1/ref=
sr_1_1/002-4883652-2081647
Best wishes always...
Keith S. Brown
> Is some efficiency penalty here for me?
Can't say that I like your coding style, but to each his/her own. As to any
speed penalties for begin/end, there are none a run-time. Begin/End only
affect compilation they produce no compiled code.
I like this style too, since you can see more of your code on a
screen at a time.
But the availability of code re-formatters makes the question no
more important than which color settings you pick for your editor, IMHO.
It's just personal preference, and it's easy to change.
-Craig
--
Craig Stuntz (TeamB) · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase WebLog: http://delphi.weblogs.com
InterBase PLANalyzer (Free IB optimization tool):
http://delphi.weblogs.com/IBPLANalyzer
Can you post good free customizable code formatter URL?
HTH
--
Grant
> I like this formatter -- very customizable. Freeware.
> http://www.dow.wau.nl/aew/delforexp.html
I would have posted the same. I also use DelForExp.
--
Rudy Velthuis (TeamB)
I disagree. There are wrong color settings and there are *right* color
settings. It is the most important thing in the world and I am on a crusade
to change everyone elses color settings to the ones I use. (toungue in
cheek)
I found something cool in color settings. I can change the comment color to
WHITE on WHITE and then I don't have to look at everybody's stupid out of
date comments anymore.
I wish people would not worry about hanging begins and instead would
concentrate on good naming conventions so I could read their code with out
having comments cluttering everything up. Comments belong as notes in your
object models, use cases, activity diagrams, etc... get them the hell out
of my code!!! aggg!!! <g>
Cheers mate,
Mark
I don't even like to read code written this way.
And I love TABs. I do use this to ident anything, like this:
function Foo () : integer
var
i : integer;
j : integer;
begin
i := 0;
j := 3;
if (j > i) then
begin
(...)
end
else
begin
(...)
end;
while (i > j) do
begin
(...)
end;
end;
Note that I use TAB instead or " " + " " even on the beggining of the lines.
Maybe because I started coding with C. I woul love to use parenthesis on
Pascal for statements.
The only thing I didn't find a way to express are the try, except, finally
statements.
Rafael
Another thing that I do not like if "For" instead of "for", "Else", "If",
etc.
"Hrvoje Brozović" <hrvoje....@public.srce.hr> escreveu na mensagem
news:3c7bb75e_2@dnews...
Delfor is a god send. I wish someone offered one for JavaScript too.
I keep ending up with "d" everywhere in my JavaScript from hitting CTRL+D in
the hopes of getting it to format automatically. :)
> for i := 1 to Limit do begin
> blah blah
> end;
I don't know if it's your case, but when I started with Delphi I used to
do this a lot, because my first language was VB and this way my code seamed
a lot more like my familiar VB code (from MSs Standard VB code and the book
I used to learn VB), however, I learned Delphi from the help files, so I
eventualy changed to hundred percent VCL-like code, with two lines spacing
and everything.
Felipe
>>We (Anders Ohlsson and humble me) use
>>
>>for i := 1 to Limit do begin
>> blah blah
>>end;
>
> I think it is horrible, but to each his own. I normally use a code
> formatter that formats to my favourite style, if I must read other
> people's code.
>
I like that one better. I am so influenced by Code Complete, by Steve
McConnell, so I wrote it like that. It is an excellent book, I
recommend it.
But I don't follow all Steve's advice for one line statement, I don't
use begin...end
so I use
for i := 1 to 2 do
blablabla...
because I am tired of nested begin and end that I do not need that make
it hard to read, I really don't like something like this
begin
if ... then
for ...
if ...
case...
end;
end;
end;
end;
end;
these nested end that I couldn't stand.
> Hrvoje --
> In addition, I really rely on having my begin...end pairs line up.
>
> Lining up the begin end statements is key, and I never mind if they
> get nested, as to me, nested begin...end's are always very clear.
I prefer 'begin' at the end of statement so:
if ..... then begin
end <-- I know who's end is this, this one belongs to if
for .... do begin
end; <-- belongs to for
just like
case
end; <- very obvious belongs to case
> Of course, this is entirely personal, and one should code however one
> wants.
Completely agree. We are not starting 'war' here. But when I went to
university, I met somebody that didn't use whitespace at all. I hate it.
Wien.
We used to have that problem, but we worked around it by using the
"VCL comment style."
However, stripping out all comments regardless of relevance is something
like killing ants with a "thermo-nuclear device" -- you get a wee bit of
collateral damage.
Good comments provide the context in which the code resides; increase the
comprehension of the reader and describe the intent of the code. That
information, should in my opinion, reside with the code. For example,
imagine that you maintain legacy code where all that information -- if it
ever existed -- was separated at birth and now lost (and all code is legacy
code if you wait long enough).
Bad comments are like good politicians; they mislead, misdirect and confuse.
You might find the following power point presentation (How to write
Absolutely Terrible Software) amusing:
http://www.ghg.net/chairouchu/CSCI3132/lectures/TerribleSoftware.ppt
Good luck and best wishes
Keith S. Brown
"Mark Lauter" <markl...@hotmail.com> wrote in message
news:3c7bf4cf$1_1@dnews...
<SNIP>
My thoughts exactly.
--
Iman
`fast-forwards to a dark future, where people who couldn't be trusted to
transform Optimus Prime from a semi into a robot are suddenly skewering
their palms on soldering irons.' - Penny Arcade
Reminds me of when I was working on "well worn"
(i.e. extensively maintained) code originally written
by an owner of a company. His comments were
left in out of misguided respect by the maintenance
programmers, even though the comments no longer
applied.
-- on second thought, perhaps the programmers left
the erroneous comments in, because they were
following the adage: "If it works don't mess with it."
and not my Delphi style
if ( Condition ) {
blah
}
> I prefer 'begin' at the end of statement so:
>
> if ..... then begin
> end <-- I know who's end is this, this one belongs to if
The problem I have with this style is that with three exceptions (case,
try/finally, try/except) end has nothing whatsoever to do with the statement
> The problem I have with this style is that with three exceptions (case,
> try/finally, try/except) end has nothing whatsoever to do with the statement
> it is matched to. It should, IMO, be matched/aligned with the begin that it
> logically closes. IMO its wrong thinking to match "end with if, for, with,
> or while".
Yea... maybe it is not perfect, but it is more readable to me. And I
think exactly the opposite of your thought. 'end' means the end of 'if'
or 'while' or 'for'.
As long it is work for me and it conform to the coding standard in my
team, it does not really matter. ;)
Again, I am influenced by Code Complete and evolving coding standard in
my group. I used to use 'end' paired with 'begin', but the coding
standard in my group forced me to change, and I like it better since then.
> Yea... maybe it is not perfect, but it is more readable to me. And I
> think exactly the opposite of your thought. 'end' means the end of 'if'
> or 'while' or 'for'.
End definitely does not mean end of if, while, or for. See the Object Pascal
(or is it 'Delphi' these days?) Reference manual.
> As long it is work for me and it conform to the coding standard in my
> team, it does not really matter. ;)
Very true.
> if SomeCondition then
> begin
> DoThingA;
> end else
> begin
> DoThingB;
> end;
Nick, I respect you greatly, but to my eyes (trained on the K&R C book,
the Sun Java coding standards, and the observation that reading code is
easier when you can see more of it on the screen), I find this a waste
of vertical space. You have 7 lines of text, and only three of them
have anything by syntactic noise. Of the vertical space I have to
spare, I prefer to spend it on separating sections of code in a
semantically meaningful way with white space
Recently, I've found that the Python way (a subject of many flame wars)
is elegantly terse:
if SomeCondition:
DoThingA
else:
DoThingB
... nearly zero noise, making it much easier to see the code.
Speaking of seeing the code, I also think it's silly that some code
editors default to making the keywords bold, so you end up with a whole
bunch of bold syntactic noise, and non-bold real code.
Within a single project, I think it's important to establish a shared
coding style. Across projects, I agree with the others who have said
"to each his own".
--
[ Kyle Cordes * ky...@kylecordes.com * http://kylecordes.com ]
[ Consulting, Training, and Software development tips and ]
[ techniques: Java, Delphi, ASTA, BDE Alternatives Guide, ]
[ JB Open Tools, EJB, Web applications, methodologies, etc. ]
> I downloaded VCL scanner with source.
> Very pleased to see that Anders Ohlsson himself
> uses coding style similar to mine,
> and different from coding style guides and VCL source.
>
> We (Anders Ohlsson and humble me) use
>
> for i := 1 to Limit do begin
> blah blah
> end;
I do it this way also. I mentioned some related things on this page:
http://kylecordes.com/story-116-object-pascal-improvements.html
> > WHITE on WHITE and then I don't have to look at everybody's stupid
out of
> > date comments anymore.
>
> We used to have that problem, but we worked around it by using the
> "VCL comment style."
+1, Funny
:-)
procedure II(var I_I : int64; _II : boolean); const II_ =
$8000000000000000; var __I : boolean; begin if _II then
begin __I := odd(I_I); I_I := (I_I div 2) and not
II_; if __I then I_I := I_I or II_; end else begin __I
:= I_I and II_ = II_; I_I := I_I * 2; if __I then inc(I_I)
;end end;
Chris
"Hrvoje Brozović" <hrvoje....@public.srce.hr> wrote in message
news:3c7bb75e_2@dnews...
> I downloaded VCL scanner with source.
> Very pleased to see that Anders Ohlsson himself
> uses coding style similar to mine,
> and different from coding style guides and VCL source.
>
> We (Anders Ohlsson and humble me) use
>
> for i := 1 to Limit do begin
> blah blah
> end;
>
> if Condition then begin
> blah blah
> end;
>
> while Condition do begin
> blah blah
> end;
>
> Coding guides recommends and VCL source use
>
> for i := 1 to Limit do
> begin
> blah blah
> end;
>
> if Condition then
> begin
> blah blah
> end;
>
> while Condition do
> begin
> blah blah
> end;
>
but that and the number of spaces you indent by are a personal / team
preference.
Doing
If Condition Then Statement Else Statement is capital offence as far
as I'm concerned, after all it's not like were are running out of gaps
in between line numbers.
If I start having a nesting level beyond four I tend to to redesign
the code.
And I always use Begin & Ends to avoid this fella which I've seen
several times and it drives me mad.
If Condition1 Then
If Condition2 Then
Statement
Else
Statement;
It can be made to work but anyone who relies on it should be toted on
Beelzebub's fork until the universe ends.
Please keep spamming my email account. I don't mind because I don't read it.
"Cure Spammers for ever, Shoot the b*st**ds" Tony
Go on admit it.
>if Condition then begin
> blah blah
>end;
What if there also an "else"? Consistently with the approach above,
one would use:
if Condition then begin
blah blah
end
else begin
bloh bloh
end;
Now, having the first "end" and the "else" at the same indent level is
mixing apples and oranges. That's why I believe the only consistent
approach is
If Condition then
begin
DoSomething;
end;
even for 1 only statement in the block. Then, when an "else" appears,
consistency is mantained:
If Condition then
begin
DoSomething;
end
else
begin
DoAnotherThing;
end;
That being said, I do as you & Ohlsoon when there's no "else", may be
because I still hope Delphi would eventually change to a Modula-like
syntax... (I know it's Utopia, but dreaming is free).
Manuel Algora
cen...@wanadoo.es
aaah, Python. If only Boa-constructor was usable.
No. I use:
if Condition then begin
balh;
end else begin
blah;
end;
BEGIN is allways last word in firts line of block
I'm trying to hide BEGIN because I see no reason
why they even exist in the first place.
DO in FOR and WHILE and THEN in IF is enougth
for parser to know where block begins.
But, there it is, we can't ged rig of it.
Look at REPEAT .. UNTIL.
It's inconsistant. But, all that said, I like OP very
much compared to C.
On the other hand, I miss ++, --, +=,
if ( some_var = SomeFunction( SomeParams ))
and things like that. But, again, I like OP very much.
And this newest thing, language called Delphi.
Ok. But to say it's always been called that way.
Unbelievable. I'm having great hopes and fears for
Borland in the same time.
Like my roots too. First the Spectrum (BASIC and assembler), then a
IBM286 (i started then with standard Pascal and a bit of COBOL), and 4
years ago Clipper (the hell) and Delphi.
I keep my way in begin..end since Pascal.
--
JAM - Relįjate y disfruta...
I used to format code to conserve vertical space. Funny thing is, I find
HORIZONTAL space more pricey these days. What with class instance and
property names often being ten feet long, combine that with levels of
indentation... so I recently switched to the style Nick described.
> Speaking of seeing the code, I also think it's silly that some code
> editors default to making the keywords bold, so you end up with a whole
> bunch of bold syntactic noise, and non-bold real code.
I don't find bold noisy. I think it helps separate it from the other
elements of the code -- especially if color is used as well. The only thing
black and bold in my code editor are the reserved keywords. Visual
differentiation is the key for me -- I like to see strings, numbers,
identifiers and symbols distinctly, so I assign them each a different color.
But, of course, to each his own....
-Jeff Hamblin
I consider /end/ to be the end of a block, not really any
particular statement. Having it on the same indent space as the
beginning of that block makes more sense to me. However, if I were
to start putting begins on their own line, I would indent the
matching end the same as the begin. Fortunately, though, I got out
of that habit last year. :)
Woody (TMW)
But I beleive
Break;
belongs to the surrounding loop structure
I suggest indenting to the column of
for, while, repeat.
In the same way
Exit;
belongs to the surrounding
procedure, function, ..
and
Halt;
belongs to the surrounding
program, unit, library,
.
For example
function TfrmMain.ExcludeFile(const FileName: String): Boolean;
....
begin // function
........
if n > 0 then begin
....
for i:=1 to InfoNum do // loop
if ..... then begin
Result := ....
if Result then
Break; // loop
end
; // loop
FreeMem(Buf,n);
if ... then
.....
Exit; // function
else
end; // function
I share your dream <g>.
Could we have it when we shift from "Object Pascal" to "Delphi"?
If he can write that way, he surely don't need any debugging :-)
> if Result then
> Break; // loop
> end
> ; // loop
> FreeMem(Buf,n);
> if ... then
> .....
> Exit; // function
> else
> end; // function
Do you also regard regular function calls as external to the routine?
Why don't you also indent them with the rest of the functions (i.e. at
position 1)?
--
Rudy Velthuis (TeamB)
He, he, he... When I first read the actual name of the language was
Delphi, my first thought was: "Once it is Delphi, it can eventually be
made into anything other than Pascal". Wonder if Borland has thought
the same...
Mind, I like the concepts behind Pascal, but syntax can be improved.
Just as Wirth himself discovered.
Manuel Algora
cen...@wanadoo.es
>No. I use:
>
>if Condition then begin
> balh;
>end else begin
> blah;
>end;
I don't like this approach (which seems to enjoy some popularity),
because it doesn't follow concept/indent pairs consistency.
There's an execution block for "if", another one for "else". Those are
the primary levels. Thus:
If ...
else ...
Then there's the begin/end pair, which should be on the same (same
concept) level of indentation, but a different one from if/else
(different concept than if/else) (But more on this below). Thus:
If ...
begin
..
end
else
begin
,,,
end;
With your approach, you hide the "else", by putting it after an "end"
-which is the worst feature of that coding style, IMO- and alter
completely the concepts/indentation consistency.
>BEGIN is allways last word in firts line of block
>
>I'm trying to hide BEGIN because I see no reason
>why they even exist in the first place.
>DO in FOR and WHILE and THEN in IF is enougth
>for parser to know where block begins.
>
>But, there it is, we can't ged rig of it.
>
>Look at REPEAT .. UNTIL.
>It's inconsistant.
With this I agree completely. While begin/end encompass a statements
block, not every encompassing needs be done this way.
Now, if you remove the Begin, you would go for the Endif/EndFor
(Next)/EndWhile (Wend), instead of End, as these then match the
starting of the structure rather than a Begin -same as Until matches
Repeat. Then these "ENDs" would go at the same indentation level as
If/For/While.
Manuel Algora
cen...@wanadoo.es
I use the cpu window, of course !
{;-)
Chris
BTW, I remember, many years ago, TurboPower or Blaise computing (I think)
had an application that could reformat a source file according to the
"rectangular style" (also replacing the identifiers by unique strings of "I"
and "_"). Doing so, it was possible to distribute the source code (for the
turbo pascal compiler) in a form that (almost) made any modification
impossible!
> I do it the same way because it seems easier to read when the END
> signals the end of its own indent level.
Remarkable.
I like this too, and I thought I was a bit idiosycratic, as I've never
before today seen anyone else support it. Now they are all coming out of the
woodwork!
-Iain.
Never. But from my point of view
Break; Continue;
Exit;
Halt;
are not used like regular 'procedures' .
They are used more like 'statements' e.g. FOR..; WHILE..;
If
Break; Continue;
are not surrounded by a looping statement then an error
'BREAK or CONTINUE outside of loop' is generated.
How can I write a procedure with such behaviour ?
Volker
Me too, but it actually *is* less readable for other people, especailly
begginers where they just want to see:
<header>
<block>
and not:
<header><bl..
..ock>
:-))