/    Sign up×
Community /Pin to ProfileBookmark

Folks,

I am reading on “Coding Standards”. This thread will be based on that.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 14.2021 — Folkies,

Don't mind me calling you "folkIEs"! Lol!

Reading this:

https://www.tutorialspoint.com/php/php_coding_standard.htm

Based on that, it seems I shouldn't write the IFs like this:
<i>
</i>if()

But give one space like so ...
<i>
</i>if ()


Did I understand correctly ? If so, feeling weird writing like that. I never came across anyone doing that. Not even came across like this in tutorials!
Copy linkTweet thisAlerts:
@developer_webauthorApr 14.2021 — Ah!

But I shouldn't leave space (like in the IF and it's braces) when writing the function!

Not do this:
<i>
</i>function new ()

I never really did it like that anyway. Used to do it like this:
<i>
</i>function new ($param1,$param2)
{
code
}

However, that tutorial says I should leave spaces after the commas in the params. That I learnt just now.

Should write like this:
<i>
</i>function new ($param1, $param2)
{
code
}


Do you people follow these standard coding practices, too ? If so, then I reading a good tutorial. Yes ?

https://www.tutorialspoint.com/php/php_coding_standard.htm

Seems like this of mine is not standard coding practice:
<i>
</i>function function_4($new=NULL)


Has to have spaces like so, tutorial says:
<i>
</i>function function_4($new = NULL)


Not writing like following, if I remember correctly NogDog said I shouldn't write NULL like that. I actually didn't but I saw others doing it and got infected by their bad or error practicing habit.
<i>
</i>function function_4($new = 'NULL')

I think I saw this wrong practice at stackoverflow. Not sure. Questioners writing like that. Didn't see the answerers correcting the questioners. I am glad, NogDog corrected me. If NogDog was over there, people would learn better than they are doing right now over there. If it was there that I saw this error practice going on undetected.
Copy linkTweet thisAlerts:
@developer_webauthorApr 14.2021 — Folks,

Ok. Reading this:

https://www.tutorialspoint.com/php/php_coding_standard.htm

Variable Names −

**Use all lower case letters
Use '_' as the word separator.
Global variables should be prepended with a 'g'.
_Global constants should be all caps with '_' separators.
Static variables may be prepended with 's'.**_


Can someone show me example code of last two lines ? About Global Constants and Static Variables.

I'm not too familiar with them. Forgotten them.
Copy linkTweet thisAlerts:
@developer_webauthorApr 14.2021 — Folks,

I don't understand this of following, so can someone be kind enough to show code samples ...

Make Functions Reentrant − Functions should not keep static variables that prevent a function from being reentrant.

Alignment of Declaration Blocks − Block of declarations should be aligned.

One Statement Per Line − There should be only one statement per line unless the statements are very closely related.

Short Methods or Functions − Methods should limit themselves to a single page of code.


**There could be many more points which should be considered while writing your PHP program. Over all intention should be to be consistent throughout of the code programming and it will be possible only when you will follow any coding standard. You can device your own standard if you like something different.**

https://www.tutorialspoint.com/php/php_coding_standard.htm

This last part is a contradictory in the tutorial:

"**You can device your own standard if you like something different.**"

At first they said the coding standard was created so it is easy for others to understand your code. And now they contradict themselves saying you can create your own standard if you don't like the standard in place that all php developers use. Isn't this ridiculous ?
Copy linkTweet thisAlerts:
@NogDogApr 14.2021 — PHP itself doesn't care about those kinds of styling -- they're only a convenience for the developers to aid in having a consistent format to make reading and maintaining the code easier. As such, there is not one "correct" coding standard, so whether you use a space or not is purely subjective and has no impact on now the script works (same with indenting with spaces or tabs, whether the { is at the end of a line or the start of a new line, etc.).

That being said, when working with a team of developers, it's good to agree on what your standards will be on such things, so that the code base has a consistent look/feel.
Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — @NogDog#1630334

>>has no impact on _**n**_ow the script works<<

Thanks. But you actually meant:

"has no impact on **h**ow the script works".

I know it doesn't matter to php. Else, I would've seen errors for 5 yrs now. Curious, how much of these coding standards you abide by that are mentioned in that link ?

Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — @Sempervivum,

Do you mind answering:

https://www.webdeveloper.com/d/393680-coding-standards/4

https://www.webdeveloper.com/d/393680-coding-standards/5
Copy linkTweet thisAlerts:
@VITSUSAApr 15.2021 — @developer_web#1630318 Coding standards are very important because it help to ensure safety, security, and reliability. A coding standard gives a uniform appearance to the codes written by different engineers. It improves readability, and maintainability of the code and it reduces complexity also as well as It helps in code reuse and helps to detect errors easily.
Copy linkTweet thisAlerts:
@inktApr 15.2021 — I'll elaborate on this by saying a team might have its own coding standards, or personal prefrence in code styling. For instance, our team prepends the variable's type to its name.

$strWord = "bar";

$intNumber = 1;

$blnOption = true;

$arrNumbers = array(1, 2, 3);

etc...

I our team uses camel case rather than underscore case. I hate ifs without curlies. All my code is very well documented. Our team uses underscores for file names. eg : foo_bar.php. I use variable names that explain what they contain, the person reading or adding onto my code is always appretiative.
Copy linkTweet thisAlerts:
@SempervivumApr 15.2021 — @developer_web#1630369

Regarding #4: I don't like underlines in variable names as they break fluent typing. Instead I prefer the camel case style:

`thisIsAGlobalVar`

Or, whenever possible, in name, id, or class attributes in HTML, I prefer "-".
Copy linkTweet thisAlerts:
@developer_webauthorApr 16.2021 — @Sempervivum#1630413

I prefer that too. But I use _ over sEmPeRvIvUm.

Why they call it "camel case" ? "camel" ?

Anyway, I bought this new keyboard. Whenever I type an opening one be it a single quote or double or opening bracket of any type it auto types the closing one. How so ? Is Windows 10 doing it or my physical keyboard ?
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...