Bad programming style

Moderator: Rathinagiri

fprijatelj
Posts: 14
Joined: Wed Mar 10, 2010 4:09 pm

Bad programming style

Post 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
Ricci
Posts: 255
Joined: Thu Nov 19, 2009 2:23 pm

Re: Bad programming style

Post by Ricci »

Franček, everyone is invited to improve the code !

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

Ricci
mrduck
Posts: 497
Joined: Fri Sep 10, 2010 5:22 pm

Re: Bad programming style

Post by mrduck »

May I ask you to not call it "bad" ? Can we call it "not proper" ? "not following the guidelines" ?
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Bad programming style

Post 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
Viva INTERNATIONAL HMG :D
mrduck
Posts: 497
Joined: Fri Sep 10, 2010 5:22 pm

Re: Bad programming style

Post 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
fprijatelj
Posts: 14
Joined: Wed Mar 10, 2010 4:09 pm

Re: Bad programming style

Post 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
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Bad programming style

Post 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
Viva INTERNATIONAL HMG :D
mrduck
Posts: 497
Joined: Fri Sep 10, 2010 5:22 pm

Re: Bad programming style

Post 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
fprijatelj
Posts: 14
Joined: Wed Mar 10, 2010 4:09 pm

Re: Bad programming style

Post 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
Ricci
Posts: 255
Joined: Thu Nov 19, 2009 2:23 pm

Re: Bad programming style

Post by Ricci »

Franček, it´s unnecessary to write the same 3 times, we did understand you very well.
Post Reply