473,903 Members | 5,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignments in conditions should be avoided

code green
1,726 Recognized Expert Top Contributor
I have just switched from phpdesigner to Netbeans as my script editor.
Netbeans is criticising my code with this warning
Possible accidental assignment, assignments in conditions should be avoided
What is your opinion of the warning when doing this for example?
Expand|Select|Wrap|Line Numbers
  1.  if($data = $file($filepath))
The alternative is
Expand|Select|Wrap|Line Numbers
  1. $data = $file($filepath);
  2. if($data)
which seems less professional.
Oct 29 '09 #1
16 10197
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
@code green
I don't know why it complains but.....

My organization insist me to write code in this way.
Expand|Select|Wrap|Line Numbers
  1. $data = $file($filepath);
  2. if($data)
  3.  
If i club more statements in a condition like

Expand|Select|Wrap|Line Numbers
  1. if($data = $file($filepath))
  2.  
i will get review comments.

Regards
Dheeraj Joshi
Oct 29 '09 #2
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
I dont know about that problem, but my organization has some coding standards, as per that.

If i try to write code as

Expand|Select|Wrap|Line Numbers
  1. if($data = $file($filepath))
  2.  
my managers gives review comments to this code saying it to change as

Expand|Select|Wrap|Line Numbers
  1. $data = $file($filepath);
  2. if($data)
  3.  
Regards
Dheeraj Joshi

Regards
Dheeraj Joshi
Oct 29 '09 #3
code green
1,726 Recognized Expert Top Contributor
I have had a Google and it seems the reasoning behind this is to avoid the schoolboy error of confusing
Expand|Select|Wrap|Line Numbers
  1. if($data = $file($filepath)) 
and
Expand|Select|Wrap|Line Numbers
  1. if($data == $file($filepath)) 
Are script editors now assuming the lowest common denomitor of programming skill?
Oct 29 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
don’t know about that… but admittedly, if someone other than you looks at the statement
Expand|Select|Wrap|Line Numbers
  1. if ($var = function($param))
(s)he might not be sure, whether it is intended or a mistake.
Oct 29 '09 #5
Markus
6,050 Recognized Expert Expert
@code green
This is discouraged in most languages and mainly because of readability, I think. TBH, I could glance over that code and think that what I saw was a comparison check. To avoid this ambiguity, I'd do it like:

Expand|Select|Wrap|Line Numbers
  1. if (($data = somefunc()) != 0) { ...
  2.  
Oct 29 '09 #6
code green
1,726 Recognized Expert Top Contributor
So it seems to be discouraged for maintainability .
Hmm, I have never made the comparator/assignment error, even when reading.
But I like to comply with best practice protocol and would hate future coders criticising my legacy code.
I quite like Markus' method.
Maybe I should listen to Netbeans and modify where required
Oct 29 '09 #7
Markus
6,050 Recognized Expert Expert
I wonder how the following code is evaluated: if ($data = somefunc())?

Is the value of somefunc() assigned to $data and then $data's value is checked, or is the assignment checked (does the assignment fail)?

I'm going to ask on the PHP dev mailing list.
Oct 29 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
I don’t think an assignment can fail… it’s usually expressions that fail.
Oct 29 '09 #9
code green
1,726 Recognized Expert Top Contributor
I wonder how the following code is evaluated: if ($data = somefunc())
Logic tells me that the most inner bracket command is carried out first,
then working outwards.
So the return value of somefunc() is assigned to $data even if this value is FALSE.
Then the if() condition is evaluated.

If PHP was strictly typed then $data could not be a boolean or say an array.
So does loosely typed allow potential bugs to creep in
Oct 29 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1447
by: Jacek Generowicz | last post by:
I am dealing with an application which reads in configurations by calling (a wrapper around) execfile. Any configuration file may itself execfile other configuration files in the same manner. I would like to create a log containing all global assignments made in these files. Comparing the globals dictionaries before and after is not quite enough, as a) this misses multiple assignments to the same name, within a single file,
9
2628
by: Mark Twombley | last post by:
Hi, I'm just getting back into C++ and had a question about the best practice for assigning error numbers. I have been working in VB for sometime now and there you would start assigning error number at vbObjectError + count. Is there a similar practice in C++ or is it just coder preference. Thanks -- Mark Twombley
3
2122
by: bearophileHUGS | last post by:
The current version of ShedSkin (http://shedskin.sourceforge.net/ experimental Python to C++ compiler) is rather alpha, and the development work is focused on debugging and implementing some more basic Python functionality. But hopefully in future versions more work will be spent to make the resulting C++ programs as fast as possible. One of the many possible details that may be improved, is the compiling to C++ of the Python "parallel"...
21
1917
by: Paul Steckler | last post by:
Here's some code that's giving me differing results, depending on the compiler. typedef foo { int A,B; } FOO; int main() {
5
2535
by: Uday Deo | last post by:
Hi everyone, I am looping through 4 nested loops and I would like to break in the inner most loop on certain condition and get the control on the 2 nd loop instead of 3rd loop. Here is briefly what I am trying to accomplish. //Loop 1 For each lp_1 in Loop1 // Certain conditions
8
1426
by: Fredrik Tolf | last post by:
Hi list! I'm relatively new to Python, and one thing I can't seem to get over is the lack of in-expression assignments, as present in many other languages. I'd really like to know how Python regulars solve the problems posed by that. For example, I recently wanted to do this: if callable(f = getattr(self, "cmd_" + name)):
0
947
by: Simon Burton | last post by:
I've been doing a little c programming again (ouch!) and it's just hit me why python does not allow assignment inside expressions (as in c): because it is absolutely essential that all assignments are as visible as possible. In python the assignment is the declaration; when declarations are as cheap as this it's hard not to fall to the temptation to re-use a variable unknowingly. I am constantly scanning the line of assignments to see...
17
3467
by: Brian Blais | last post by:
Hello, I have a couple of classes where I teach introductory programming using Python. What I would love to have is for the students to go through a lot of very small programs, to learn the basic programming structure. Things like, return the maximum in a list, making lists with certain patterns, very simple string parsing, etc. Unfortunately, it takes a lot of time to grade such things by hand, so I would like to automate it as much...
28
15446
MMcCarthy
by: MMcCarthy | last post by:
Policies below superceded by FAQ Post Course Work Questions and Answers. ADMIN
0
9999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11285
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10878
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10986
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4727
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.