Page 1 of 2

Bad programming style

Posted: Tue Nov 08, 2011 1:07 pm
by fprijatelj
Hi

In HMG4, there are more than 400 lines of code like this:

Code: Select all


IF lValue == .T.
   doSomething ...
ENDIF

For example:

IF ::lCreated == .T.
    RETURN NIL
ENDIF
 
IF PCOUNT() == 0
    ::lFontBold := IIF( hb_IsNil( [::lFontBold ) == .T., ::oQtFont:bold(), ::lFontBold )
This is a bad programming style !

Shorter and quicker code is:

Code: Select all

   
IF ::lCreated 
    RETURN NIL
ENDIF

IF PCOUNT() == 0
    ::lFontBold := IIF( hb_IsNil( ::lFontBold ) , ::oQtFont:bold(), ::lFontBold )
Regards
Franček

Re: Bad programming style

Posted: Tue Nov 08, 2011 3:17 pm
by Ricci
Franček, everyone is invited to improve the code !

Make it more efficient and send it to Luigi or Francesco.

Ricci

Re: Bad programming style

Posted: Tue Nov 08, 2011 3:23 pm
by mrduck
May I ask you to not call it "bad" ? Can we call it "not proper" ? "not following the guidelines" ?

Re: Bad programming style

Posted: Tue Nov 08, 2011 3:46 pm
by esgici
I agree with Franček

"Not proper" and "not following the guidelines" so, "bad".

Moreover I don't like also

Code: Select all

IF <something" 
    RETURN NIL
ENDIF
style.

Rule is: every procedure / function must have only one exit ( return ).

IMO correct way is:

Code: Select all

.
LOCAL xRVal 
.
.
.
IF .NOT. <something"   // or better : !<something"
    .
    .
    .
ENDIF

RETURN  xRVal 
If someone ask : "who is ruler ( who put the rules ) ?" answer is : "the life and experience".

Coding style guidelines and rules are for ability of easy read, easy understand.

Regards / saludos

--

Esgici

Re: Bad programming style

Posted: Tue Nov 08, 2011 4:20 pm
by mrduck
Escigi, I totally disagree with you.

Coding styles usually create a big flame, so I stop here.

There should be a message from Pritpal stating the hbQt coding style guidelines, so perhaps we should check if HMG4 adheres to that coding or not, and then if everyone agrees, apply them...

It is true that it's better if there is only one exit point, in some languages it is mandatory... not in harbour, and it may be considered another coding style...
esgici wrote:I agree with Franček

"Not proper" and "not following the guidelines" so, "bad".

Moreover I don't like also

Code: Select all

IF <something" 
    RETURN NIL
ENDIF
style.

Rule is: every procedure / function must have only one exit ( return ).

IMO correct way is:

Code: Select all

.
LOCAL xRVal 
.
.
.
IF .NOT. <something"   // or better : !<something"
    .
    .
    .
ENDIF

RETURN  xRVal 
If someone ask : "who is ruler ( who put the rules ) ?" answer is : "the life and experience".

Coding style guidelines and rules are for ability of easy read, easy understand.

Regards / saludos

--

Esgici

Re: Bad programming style

Posted: Tue Nov 08, 2011 4:49 pm
by fprijatelj
Hi

Maybe I wrongly named it "style".

It has nothing with a style of programming
For example in C++

Style 1:
--------

if (a > b ) {
}

or

Style2:
---------

if (a > b)
{
}

It' s about well known concept in any language ( documented in many books),
that syntax of if statement is :

IF <logicalExpression>
.....
ENDIF

For example:

i=j+5
IF ( j > 10) .... // don't write (j > 10) == .T.

We don't write

IF (j > 10) == .T. // even if it works

So if we have logical variable:

i= a >3
IF i // don't write i == .T.
...

In HMG4 there are more than 400 "errors" of this type (bigger source, bigger code,
slower execution)
I didn't want to offend anybody.
In almost any book about a programing (and I have read a lot of them )
in any language (C,C++, Python, Ruby,PHP,Pascal,Java ...) , there is
a warning about this bad habit (not syntactic or semantic error).

In IF statement (and in other conditional statements ) we have conditional expression.
And variable is !!!!!! the simplest conditional expresion

So:

IF logicalVariable instead of IF logicalVariable == .T.




Regards
Franček

Re: Bad programming style

Posted: Tue Nov 08, 2011 5:14 pm
by esgici
mrduck wrote:I totally disagree with you.
Coding styles usually create a big flame, so I stop here.
Hi Francesco

I disagree with you not totally, but only partially and personally ;)

As we are about "style", this is only personal point of view, not restrictive to everybody.

That means we haven't big or small, any flame; nor a spark.

I'm follow up and respect your valuable efforts for improvement of HMG. We ( you and me and everybody ) may have different pows about coding style, it is'nt ?

Saludos / regards

--

Esgici

Re: Bad programming style

Posted: Tue Nov 08, 2011 5:19 pm
by mrduck
I was saying that: coding style is a personal matter. Pritpal gave us some rules and we may try to comply with them, just to have more coherent source code.

Francesco

Re: Bad programming style

Posted: Tue Nov 08, 2011 5:25 pm
by fprijatelj
Hi

Nothing about style

If you write:

IF (A>3) // and not : IF (((A>3) == .T.) == .T.) == .T. ......

Why you write:

IF V == .T. // V is variable with logical value

instead od

IF V

Regards
Franček

Re: Bad programming style

Posted: Tue Nov 08, 2011 5:43 pm
by Ricci
Franček, it´s unnecessary to write the same 3 times, we did understand you very well.